Skip to content

Commit

Permalink
Write PR labels to .git/resource/labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Yarotsky committed Oct 16, 2020
1 parent c41729d commit 1204093
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
18 changes: 18 additions & 0 deletions in.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func Get(request GetRequest, github Github, git Git, outputDir string) (*GetResp
metadata.Add("author", pull.Tip.Author.User.Login)
metadata.Add("author_email", pull.Tip.Author.Email)

labelsJSON, err := serializeLabels(pull)
if err != nil {
return nil, fmt.Errorf("failed to serialize pull request labels: %s", err)
}
metadata.Add("labels", labelsJSON)

// 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 Expand Up @@ -128,6 +134,18 @@ func Get(request GetRequest, github Github, git Git, outputDir string) (*GetResp
}, nil
}

func serializeLabels(p *PullRequest) (string, error) {
labels := []string{}
for _, label := range p.Labels {
labels = append(labels, label.Name)
}
serializedLabels, err := json.Marshal(labels)
if err != nil {
return "", err
}
return string(serializedLabels), nil
}

// GetParameters ...
type GetParameters struct {
SkipDownload bool `json:"skip_download"`
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{"my-label"}

tests := []struct {
description string
Expand All @@ -40,9 +41,9 @@ func TestGet(t *testing.T) {
CommittedDate: time.Time{},
},
parameters: resource.GetParameters{},
pullRequest: createTestPR(1, "master", false, false, 0, nil),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`,
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"}]`,
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":"[\"my-label\"]"}]`,
},
{
description: "get supports unlocking with git crypt",
Expand All @@ -57,9 +58,9 @@ func TestGet(t *testing.T) {
CommittedDate: time.Time{},
},
parameters: resource.GetParameters{},
pullRequest: createTestPR(1, "master", false, false, 0, nil),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`,
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"}]`,
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":"[\"my-label\"]"}]`,
},
{
description: "get supports rebasing",
Expand All @@ -75,9 +76,9 @@ func TestGet(t *testing.T) {
parameters: resource.GetParameters{
IntegrationTool: "rebase",
},
pullRequest: createTestPR(1, "master", false, false, 0, nil),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`,
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"}]`,
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":"[\"my-label\"]"}]`,
},
{
description: "get supports checkout",
Expand All @@ -93,9 +94,9 @@ func TestGet(t *testing.T) {
parameters: resource.GetParameters{
IntegrationTool: "checkout",
},
pullRequest: createTestPR(1, "master", false, false, 0, nil),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`,
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"}]`,
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":"[\"my-label\"]"}]`,
},
{
description: "get supports git_depth",
Expand All @@ -111,9 +112,9 @@ func TestGet(t *testing.T) {
parameters: resource.GetParameters{
GitDepth: 2,
},
pullRequest: createTestPR(1, "master", false, false, 0, nil),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`,
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"}]`,
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":"[\"my-label\"]"}]`,
},
{
description: "get supports list_changed_files",
Expand All @@ -129,7 +130,7 @@ func TestGet(t *testing.T) {
parameters: resource.GetParameters{
ListChangedFiles: true,
},
pullRequest: createTestPR(1, "master", false, false, 0, nil),
pullRequest: createTestPR(1, "master", false, false, 0, prLabels),
files: []resource.ChangedFileObject{
{
Path: "README.md",
Expand All @@ -139,7 +140,7 @@ func TestGet(t *testing.T) {
},
},
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`,
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"}]`,
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":"[\"my-label\"]"}]`,
filesString: "README.md\nOther.md\n",
},
}
Expand Down Expand Up @@ -185,6 +186,7 @@ func TestGet(t *testing.T) {
"author": "login1",
"author_email": "user@example.com",
"title": "pr1 title",
"labels": `["my-label"]`,
}

for filename, expected := range files {
Expand Down

0 comments on commit 1204093

Please sign in to comment.