Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load labels in GetPullRequest #285

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,15 @@ func (m *GithubClient) GetPullRequest(prNumber, commitRef string) (*PullRequest,
PullRequest struct {
PullRequestObject
Commits struct {
Edges []struct {
Node struct {
Commit CommitObject
}
Nodes []struct {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tweak was to standardise on using Nodes rather than Edges, as we're only concerned with the data itself...

https://johnkavanagh.co.uk/writing/gatsby-and-graphql-nodes-vs-edges/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Commit CommitObject
}
} `graphql:"commits(last:$commitsLast)"`
Labels struct {
Nodes []struct {
LabelObject
}
} `graphql:"labels(first:$labelsFirst)"`
} `graphql:"pullRequest(number:$prNumber)"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}
Expand All @@ -302,25 +305,39 @@ func (m *GithubClient) GetPullRequest(prNumber, commitRef string) (*PullRequest,
"repositoryName": githubv4.String(m.Repository),
"prNumber": githubv4.Int(pr),
"commitsLast": githubv4.Int(100),
"labelsFirst": githubv4.Int(100),
}

// TODO: Pagination - in case someone pushes > 100 commits before the build has time to start :p
if err := m.V4.Query(context.TODO(), &query, vars); err != nil {
return nil, err
}

for _, c := range query.Repository.PullRequest.Commits.Edges {
if c.Node.Commit.OID == commitRef {
// Return as soon as we find the correct ref.
return &PullRequest{
pullRequest := PullRequest{}
commitRefFound := false

for _, c := range query.Repository.PullRequest.Commits.Nodes {
if c.Commit.OID == commitRef {
commitRefFound = true
pullRequest = PullRequest{
PullRequestObject: query.Repository.PullRequest.PullRequestObject,
Tip: c.Node.Commit,
}, nil
Tip: c.Commit,
}
// Return as soon as we find the correct ref.
break
}
}

// Return an error if the commit was not found
return nil, fmt.Errorf("commit with ref '%s' does not exist", commitRef)
if !commitRefFound {
// Return an error if the commit was not found
return nil, fmt.Errorf("commit with ref '%s' does not exist", commitRef)
}

for _, l := range query.Repository.PullRequest.Labels.Nodes {
pullRequest.Labels = append(pullRequest.Labels, l.LabelObject)
}

return &pullRequest, nil
}

// UpdateCommitStatus for a given commit (not supported by V4 API).
Expand Down
7 changes: 7 additions & 0 deletions in.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
)

// Get (business logic)
Expand Down Expand Up @@ -53,6 +54,12 @@ func Get(request GetRequest, github Github, git Git, outputDir string) (*GetResp
metadata.Add("author_email", pull.Tip.Author.Email)
metadata.Add("state", string(pull.State))

labels := []string{}
for _, label := range pull.Labels {
labels = append(labels, label.Name)
}
metadata.Add("labels", strings.Join(labels, ","))

// Write version and metadata for reuse in PUT
path := filepath.Join(outputDir, ".git", "resource")
if err := os.MkdirAll(path, os.ModePerm); err != nil {
Expand Down
26 changes: 14 additions & 12 deletions in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

func TestGet(t *testing.T) {
prLabels := []string{"label 1", "label 2"}

tests := []struct {
description string
Expand All @@ -42,9 +43,9 @@ func TestGet(t *testing.T) {
State: githubv4.PullRequestStateOpen,
},
parameters: resource.GetParameters{},
pullRequest: createTestPR(1, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels, false, githubv4.PullRequestStateOpen),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z","approved_review_count":"0","state":"OPEN"}`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"state","value":"OPEN"}]`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"labels","value":"label 1,label 2"},{"name":"state","value":"OPEN"}]`,
},
{
description: "get supports unlocking with git crypt",
Expand All @@ -61,9 +62,9 @@ func TestGet(t *testing.T) {
State: githubv4.PullRequestStateOpen,
},
parameters: resource.GetParameters{},
pullRequest: createTestPR(1, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels, false, githubv4.PullRequestStateOpen),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z","approved_review_count":"0","state":"OPEN"}`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"state","value":"OPEN"}]`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"labels","value":"label 1,label 2"},{"name":"state","value":"OPEN"}]`,
},
{
description: "get supports rebasing",
Expand All @@ -81,9 +82,9 @@ func TestGet(t *testing.T) {
parameters: resource.GetParameters{
IntegrationTool: "rebase",
},
pullRequest: createTestPR(1, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels, false, githubv4.PullRequestStateOpen),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z","approved_review_count":"0","state":"OPEN"}`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"state","value":"OPEN"}]`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"labels","value":"label 1,label 2"},{"name":"state","value":"OPEN"}]`,
},
{
description: "get supports checkout",
Expand All @@ -101,9 +102,9 @@ func TestGet(t *testing.T) {
parameters: resource.GetParameters{
IntegrationTool: "checkout",
},
pullRequest: createTestPR(1, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels, false, githubv4.PullRequestStateOpen),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z","approved_review_count":"0","state":"OPEN"}`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"state","value":"OPEN"}]`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"labels","value":"label 1,label 2"},{"name":"state","value":"OPEN"}]`,
},
{
description: "get supports git_depth",
Expand All @@ -121,9 +122,9 @@ func TestGet(t *testing.T) {
parameters: resource.GetParameters{
GitDepth: 2,
},
pullRequest: createTestPR(1, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels, false, githubv4.PullRequestStateOpen),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z","approved_review_count":"0","state":"OPEN"}`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"state","value":"OPEN"}]`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"labels","value":"label 1,label 2"},{"name":"state","value":"OPEN"}]`,
},
{
description: "get supports list_changed_files",
Expand All @@ -141,7 +142,7 @@ func TestGet(t *testing.T) {
parameters: resource.GetParameters{
ListChangedFiles: true,
},
pullRequest: createTestPR(1, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels, false, githubv4.PullRequestStateOpen),
files: []resource.ChangedFileObject{
{
Path: "README.md",
Expand All @@ -151,7 +152,7 @@ func TestGet(t *testing.T) {
},
},
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z","approved_review_count":"0","state":"OPEN"}`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"state","value":"OPEN"}]`,
metadataString: `[{"name":"pr","value":"1"},{"name":"title","value":"pr1 title"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"},{"name":"author_email","value":"user@example.com"},{"name":"labels","value":"label 1,label 2"},{"name":"state","value":"OPEN"}]`,
filesString: "README.md\nOther.md\n",
},
}
Expand Down Expand Up @@ -197,6 +198,7 @@ func TestGet(t *testing.T) {
"author": "login1",
"author_email": "user@example.com",
"title": "pr1 title",
"labels": "label 1,label 2",
}

for filename, expected := range files {
Expand Down