Skip to content

Commit 78cff7e

Browse files
authored
Update github_license table to use GraphQL API closes #236 (#237)
1 parent 1002b4a commit 78cff7e

File tree

2 files changed

+97
-56
lines changed

2 files changed

+97
-56
lines changed

docs/tables/github_license.md

+45
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,48 @@ group by
3939
order by
4040
num_repos desc;
4141
```
42+
43+
### View conditions for a specific license
44+
45+
```sql
46+
select
47+
name,
48+
key,
49+
c ->> 'Key' as condition,
50+
c ->> 'Description' as condition_desc
51+
from
52+
github_license,
53+
jsonb_array_elements(conditions) as c
54+
where
55+
key = 'gpl-3.0';
56+
```
57+
58+
### View limitations for a specific license
59+
60+
```sql
61+
select
62+
name,
63+
key,
64+
l ->> 'Key' as limitation,
65+
l ->> 'Description' as limitation_desc
66+
from
67+
github_license,
68+
jsonb_array_elements(limitations) as l
69+
where
70+
key = 'gpl-3.0';
71+
```
72+
73+
### View permissions for a specific license
74+
75+
```sql
76+
select
77+
name,
78+
key,
79+
p ->> 'Key' as permission,
80+
p ->> 'Description' as permission_desc
81+
from
82+
github_license,
83+
jsonb_array_elements(permissions) as p
84+
where
85+
key = 'gpl-3.0';
86+
```

github/table_github_license.go

+52-56
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package github
22

33
import (
44
"context"
5-
6-
"github.com/google/go-github/v48/github"
5+
"github.com/shurcooL/githubv4"
6+
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
77

88
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
99
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
10-
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
1110
)
1211

1312
//// TABLE DEFINITION
@@ -27,53 +26,67 @@ func tableGitHubLicense() *plugin.Table {
2726
Columns: []*plugin.Column{
2827

2928
// Top columns
30-
{Name: "spdx_id", Description: "The Software Package Data Exchange (SPDX) id of the license.", Type: proto.ColumnType_STRING, Transform: transform.FromField("SPDXID")},
29+
{Name: "spdx_id", Description: "The Software Package Data Exchange (SPDX) id of the license.", Type: proto.ColumnType_STRING, Transform: transform.FromField("SpdxId")},
3130
{Name: "name", Description: "The name of the license.", Type: proto.ColumnType_STRING},
32-
{Name: "html_url", Description: "The HTML URL of the license.", Type: proto.ColumnType_STRING, Hydrate: tableGitHubLicenseGetData},
31+
{Name: "html_url", Description: "The HTML URL of the license.", Type: proto.ColumnType_STRING, Transform: transform.FromField("Url")},
3332

3433
// The body is huge and of limited value, exclude it for now
3534
// {Name: "body", Type: proto.ColumnType_STRING, Hydrate: tableGitHubLicenseGetData},
36-
{Name: "conditions", Description: "An array of license conditions (include-copyright,disclose-source, etc).", Type: proto.ColumnType_JSON, Hydrate: tableGitHubLicenseGetData},
37-
{Name: "description", Description: "The license description.", Type: proto.ColumnType_STRING, Hydrate: tableGitHubLicenseGetData},
38-
{Name: "featured", Description: "If true, the license is 'featured' in the GitHub UI.", Type: proto.ColumnType_BOOL, Hydrate: tableGitHubLicenseGetData},
39-
{Name: "implementation", Description: "Implementation instructions for the license.", Type: proto.ColumnType_STRING, Hydrate: tableGitHubLicenseGetData},
35+
{Name: "conditions", Description: "An array of license conditions (include-copyright,disclose-source, etc).", Type: proto.ColumnType_JSON},
36+
{Name: "description", Description: "The license description.", Type: proto.ColumnType_STRING},
37+
{Name: "featured", Description: "If true, the license is 'featured' in the GitHub UI.", Type: proto.ColumnType_BOOL},
38+
{Name: "implementation", Description: "Implementation instructions for the license.", Type: proto.ColumnType_STRING},
4039
{Name: "key", Description: "The unique key of the license.", Type: proto.ColumnType_STRING},
41-
{Name: "limitations", Description: "An array of limitations for the license (trademark-use, liability,warranty, etc).", Type: proto.ColumnType_JSON, Hydrate: tableGitHubLicenseGetData},
42-
{Name: "permissions", Description: "An array of permissions for the license (private-use, commercial-use,modifications, etc).", Type: proto.ColumnType_JSON, Hydrate: tableGitHubLicenseGetData},
43-
{Name: "url", Description: "The API url of the license.", Type: proto.ColumnType_STRING},
40+
{Name: "limitations", Description: "An array of limitations for the license (trademark-use, liability,warranty, etc).", Type: proto.ColumnType_JSON},
41+
{Name: "permissions", Description: "An array of permissions for the license (private-use, commercial-use,modifications, etc).", Type: proto.ColumnType_JSON},
42+
{Name: "nickname", Description: "The customary short name of the license.", Type: proto.ColumnType_STRING},
43+
{Name: "pseudo_license", Description: "Indicates if the license is a pseudo-license placeholder (e.g. other, no-license).", Type: proto.ColumnType_BOOL},
4444
},
4545
}
4646
}
4747

48-
//// LIST FUNCTION
48+
type licenseRule struct {
49+
Key string
50+
Label string
51+
Description string
52+
}
4953

50-
func tableGitHubLicenseList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
51-
client := connect(ctx, d)
54+
type license struct {
55+
SpdxId string
56+
Name string
57+
Key string
58+
Conditions []licenseRule
59+
Description string
60+
Featured bool
61+
Implementation string
62+
Limitations []licenseRule
63+
Permissions []licenseRule
64+
Url string
65+
Nickname string
66+
PseudoLicense bool
67+
}
5268

53-
type ListPageResponse struct {
54-
licenses []*github.License
55-
resp *github.Response
56-
}
69+
var listLicensesQuery struct {
70+
Licenses []license `graphql:"licenses"`
71+
}
5772

58-
listPage := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
59-
licenses, resp, err := client.Licenses.List(ctx)
60-
return ListPageResponse{
61-
licenses: licenses,
62-
resp: resp,
63-
}, err
64-
}
73+
var getLicenseQuery struct {
74+
License license `graphql:"license(key: $key)"`
75+
}
6576

66-
listPageResponse, err := retryHydrate(ctx, d, h, listPage)
77+
//// LIST FUNCTION
6778

68-
listResponse := listPageResponse.(ListPageResponse)
69-
licenses := listResponse.licenses
79+
func tableGitHubLicenseList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
80+
client := connectV4(ctx, d)
7081

82+
err := client.Query(ctx, &listLicensesQuery, nil)
7183
if err != nil {
84+
plugin.Logger(ctx).Error("github_license", "api_error", err)
7285
return nil, err
7386
}
7487

75-
for _, i := range licenses {
76-
d.StreamListItem(ctx, i)
88+
for _, license := range listLicensesQuery.Licenses {
89+
d.StreamListItem(ctx, license)
7790

7891
// Context can be cancelled due to manual cancellation or the limit has been hit
7992
if d.RowsRemaining(ctx) == 0 {
@@ -84,44 +97,27 @@ func tableGitHubLicenseList(ctx context.Context, d *plugin.QueryData, h *plugin.
8497
return nil, nil
8598
}
8699

87-
//// HYDRATE FUNCTIONS
100+
//// GET FUNCTION
88101

89102
func tableGitHubLicenseGetData(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
90-
var key string
91-
if h.Item != nil {
92-
item := h.Item.(*github.License)
93-
key = *item.Key
94-
} else {
95-
key = d.EqualsQuals["key"].GetStringValue()
96-
}
103+
key := d.EqualsQuals["key"].GetStringValue()
97104

98105
// Return nil, if no input provided
99106
if key == "" {
100107
return nil, nil
101108
}
102109

103-
client := connect(ctx, d)
104-
105-
type GetResponse struct {
106-
license *github.License
107-
resp *github.Response
108-
}
109-
110-
getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
111-
license, resp, err := client.Licenses.Get(ctx, key)
112-
return GetResponse{
113-
license: license,
114-
resp: resp,
115-
}, err
110+
variables := map[string]interface{}{
111+
"key": githubv4.String(key),
116112
}
117113

118-
getResponse, err := retryHydrate(ctx, d, h, getDetails)
114+
client := connectV4(ctx, d)
119115

116+
err := client.Query(ctx, &getLicenseQuery, variables)
120117
if err != nil {
118+
plugin.Logger(ctx).Error("github_license", "api_error", err)
121119
return nil, err
122120
}
123-
getResp := getResponse.(GetResponse)
124-
license := getResp.license
125121

126-
return license, nil
122+
return getLicenseQuery.License, nil
127123
}

0 commit comments

Comments
 (0)