Skip to content

Commit

Permalink
Merge pull request #291 from mergestat/issue-pr-labels
Browse files Browse the repository at this point in the history
Add `labels` column to `github_repo_issues` and `github_repo_prs` tables
  • Loading branch information
patrickdevivo authored Jun 20, 2022
2 parents 8737396 + 1b0783b commit 4e3d16a
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 121 deletions.
136 changes: 80 additions & 56 deletions extensions/internal/github/fixtures/TestRepoIssues.yaml

Large diffs are not rendered by default.

118 changes: 59 additions & 59 deletions extensions/internal/github/fixtures/TestRepoPRs.yaml

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion extensions/internal/github/repo_issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"
"encoding/json"
"fmt"
"io"
"time"
Expand Down Expand Up @@ -33,7 +34,10 @@ type issue struct {
IsReadByViewer bool
Labels struct {
TotalCount int
}
Nodes []struct {
Name string
}
} `graphql:"labels(first: 15)"`
LastEditedAt githubv4.DateTime
Locked bool
Milestone struct {
Expand Down Expand Up @@ -157,6 +161,18 @@ func (i *iterIssues) Column(ctx vtab.Context, c int) error {
ctx.ResultInt(t1f0(current.Node.IncludesCreatedEdit))
case "label_count":
ctx.ResultInt(current.Node.Labels.TotalCount)
case "labels":
labels := make([]string, len(current.Node.Labels.Nodes))
for l, label := range current.Node.Labels.Nodes {
labels[l] = label.Name
}
js, err := json.Marshal(labels)
if err != nil {
i.logger().Err(err).Msgf("could not marshal issue labels")
ctx.ResultNull()
} else {
ctx.ResultText(string(js))
}
case "last_edited_at":
t := current.Node.LastEditedAt
if t.IsZero() {
Expand Down Expand Up @@ -255,6 +271,7 @@ var issuesCols = []vtab.Column{
{Name: "editor_login", Type: "TEXT"},
{Name: "includes_created_edit", Type: "BOOLEAN"},
{Name: "label_count", Type: "INT"},
{Name: "labels", Type: "JSON"},
{Name: "last_edited_at", Type: "DATETIME"},
{Name: "locked", Type: "BOOLEAN"},
{Name: "milestone_count", Type: "INT"},
Expand Down
4 changes: 2 additions & 2 deletions extensions/internal/github/repo_issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestRepoIssues(t *testing.T) {
t.Fatalf("failed to retrieve row contents: %v", err.Error())
}

if colCount != 22 {
t.Fatalf("expected 22 columns, got: %d", colCount)
if colCount != 23 {
t.Fatalf("expected 23 columns, got: %d", colCount)
}

if len(content) != 10 {
Expand Down
19 changes: 18 additions & 1 deletion extensions/internal/github/repo_prs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"
"encoding/json"
"io"
"time"

Expand Down Expand Up @@ -54,7 +55,10 @@ type pullRequest struct {
IsDraft bool
Labels struct {
TotalCount int
}
Nodes []struct {
Name string
}
} `graphql:"labels(first: 15)"`
LastEditedAt githubv4.DateTime
Locked bool
MaintainerCanModify bool
Expand Down Expand Up @@ -205,6 +209,18 @@ func (i *iterPRs) Column(ctx vtab.Context, c int) error {
ctx.ResultInt(t1f0(current.IsDraft))
case "label_count":
ctx.ResultInt(current.Labels.TotalCount)
case "labels":
labels := make([]string, len(current.Labels.Nodes))
for l, label := range current.Labels.Nodes {
labels[l] = label.Name
}
js, err := json.Marshal(labels)
if err != nil {
i.logger().Err(err).Msgf("could not marshal PR labels")
ctx.ResultNull()
} else {
ctx.ResultText(string(js))
}
case "last_edited_at":
t := current.LastEditedAt
if t.IsZero() {
Expand Down Expand Up @@ -335,6 +351,7 @@ var prCols = []vtab.Column{
{Name: "head_repository_name", Type: "TEXT"},
{Name: "is_draft", Type: "BOOLEAN"},
{Name: "label_count", Type: "INT"},
{Name: "labels", Type: "JSON"},
{Name: "last_edited_at", Type: "DATETIME"},
{Name: "locked", Type: "BOOLEAN"},
{Name: "maintainer_can_modify", Type: "BOOLEAN"},
Expand Down
4 changes: 2 additions & 2 deletions extensions/internal/github/repo_prs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestRepoPRs(t *testing.T) {
t.Fatalf("failed to retrieve row contents: %v", err.Error())
}

if colCount != 39 {
t.Fatalf("expected 39 columns, got: %d", colCount)
if colCount != 40 {
t.Fatalf("expected 40 columns, got: %d", colCount)
}

if len(content) != 10 {
Expand Down

0 comments on commit 4e3d16a

Please sign in to comment.