Skip to content

Commit 4e3d16a

Browse files
Merge pull request #291 from mergestat/issue-pr-labels
Add `labels` column to `github_repo_issues` and `github_repo_prs` tables
2 parents 8737396 + 1b0783b commit 4e3d16a

File tree

6 files changed

+179
-121
lines changed

6 files changed

+179
-121
lines changed

extensions/internal/github/fixtures/TestRepoIssues.yaml

Lines changed: 80 additions & 56 deletions
Large diffs are not rendered by default.

extensions/internal/github/fixtures/TestRepoPRs.yaml

Lines changed: 59 additions & 59 deletions
Large diffs are not rendered by default.

extensions/internal/github/repo_issues.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"io"
78
"time"
@@ -33,7 +34,10 @@ type issue struct {
3334
IsReadByViewer bool
3435
Labels struct {
3536
TotalCount int
36-
}
37+
Nodes []struct {
38+
Name string
39+
}
40+
} `graphql:"labels(first: 15)"`
3741
LastEditedAt githubv4.DateTime
3842
Locked bool
3943
Milestone struct {
@@ -157,6 +161,18 @@ func (i *iterIssues) Column(ctx vtab.Context, c int) error {
157161
ctx.ResultInt(t1f0(current.Node.IncludesCreatedEdit))
158162
case "label_count":
159163
ctx.ResultInt(current.Node.Labels.TotalCount)
164+
case "labels":
165+
labels := make([]string, len(current.Node.Labels.Nodes))
166+
for l, label := range current.Node.Labels.Nodes {
167+
labels[l] = label.Name
168+
}
169+
js, err := json.Marshal(labels)
170+
if err != nil {
171+
i.logger().Err(err).Msgf("could not marshal issue labels")
172+
ctx.ResultNull()
173+
} else {
174+
ctx.ResultText(string(js))
175+
}
160176
case "last_edited_at":
161177
t := current.Node.LastEditedAt
162178
if t.IsZero() {
@@ -255,6 +271,7 @@ var issuesCols = []vtab.Column{
255271
{Name: "editor_login", Type: "TEXT"},
256272
{Name: "includes_created_edit", Type: "BOOLEAN"},
257273
{Name: "label_count", Type: "INT"},
274+
{Name: "labels", Type: "JSON"},
258275
{Name: "last_edited_at", Type: "DATETIME"},
259276
{Name: "locked", Type: "BOOLEAN"},
260277
{Name: "milestone_count", Type: "INT"},

extensions/internal/github/repo_issues_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func TestRepoIssues(t *testing.T) {
2323
t.Fatalf("failed to retrieve row contents: %v", err.Error())
2424
}
2525

26-
if colCount != 22 {
27-
t.Fatalf("expected 22 columns, got: %d", colCount)
26+
if colCount != 23 {
27+
t.Fatalf("expected 23 columns, got: %d", colCount)
2828
}
2929

3030
if len(content) != 10 {

extensions/internal/github/repo_prs.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"context"
5+
"encoding/json"
56
"io"
67
"time"
78

@@ -54,7 +55,10 @@ type pullRequest struct {
5455
IsDraft bool
5556
Labels struct {
5657
TotalCount int
57-
}
58+
Nodes []struct {
59+
Name string
60+
}
61+
} `graphql:"labels(first: 15)"`
5862
LastEditedAt githubv4.DateTime
5963
Locked bool
6064
MaintainerCanModify bool
@@ -205,6 +209,18 @@ func (i *iterPRs) Column(ctx vtab.Context, c int) error {
205209
ctx.ResultInt(t1f0(current.IsDraft))
206210
case "label_count":
207211
ctx.ResultInt(current.Labels.TotalCount)
212+
case "labels":
213+
labels := make([]string, len(current.Labels.Nodes))
214+
for l, label := range current.Labels.Nodes {
215+
labels[l] = label.Name
216+
}
217+
js, err := json.Marshal(labels)
218+
if err != nil {
219+
i.logger().Err(err).Msgf("could not marshal PR labels")
220+
ctx.ResultNull()
221+
} else {
222+
ctx.ResultText(string(js))
223+
}
208224
case "last_edited_at":
209225
t := current.LastEditedAt
210226
if t.IsZero() {
@@ -335,6 +351,7 @@ var prCols = []vtab.Column{
335351
{Name: "head_repository_name", Type: "TEXT"},
336352
{Name: "is_draft", Type: "BOOLEAN"},
337353
{Name: "label_count", Type: "INT"},
354+
{Name: "labels", Type: "JSON"},
338355
{Name: "last_edited_at", Type: "DATETIME"},
339356
{Name: "locked", Type: "BOOLEAN"},
340357
{Name: "maintainer_can_modify", Type: "BOOLEAN"},

extensions/internal/github/repo_prs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func TestRepoPRs(t *testing.T) {
2323
t.Fatalf("failed to retrieve row contents: %v", err.Error())
2424
}
2525

26-
if colCount != 39 {
27-
t.Fatalf("expected 39 columns, got: %d", colCount)
26+
if colCount != 40 {
27+
t.Fatalf("expected 40 columns, got: %d", colCount)
2828
}
2929

3030
if len(content) != 10 {

0 commit comments

Comments
 (0)