Skip to content

Commit

Permalink
improve github pullrequest title
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Dec 15, 2021
1 parent 57852a4 commit fc43209
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
14 changes: 14 additions & 0 deletions internal/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Change struct {

type Changes []Change

const gitHubMaxPullRequestTitleLength = 256

func (c Change) Identifier() string {
return c.File + "#" + c.Trace.ToString() + "#" + c.NewValue
}
Expand Down Expand Up @@ -51,6 +53,18 @@ func (c Change) Message() string {
return fmt.Sprintf("Update %s:%s from %s to %s", c.File, c.Trace.ToString(), c.OldValue, c.NewValue)
}

func (cs Changes) Title() string {
updates := []string{}
for _, change := range cs {
updates = append(updates, fmt.Sprintf("%s:%s", change.ResourceName, change.NewVersion))
}
result := fmt.Sprintf("Update %s", strings.Join(updates, ", "))
if len(result) > gitHubMaxPullRequestTitleLength {
return result[0:gitHubMaxPullRequestTitleLength]
}
return result
}

func (cs Changes) Message() string {
lines := []string{}
if len(cs) > 1 {
Expand Down
53 changes: 31 additions & 22 deletions internal/change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,39 @@ import (
"github.com/stretchr/testify/assert"
)

var c1 = Change{
RegistryName: "my-registry",
ResourceName: "my-resource",
OldVersion: "1.0.0",
NewVersion: "2.0.0",
OldValue: "my-image:1.0.0",
NewValue: "my-image:2.0.0",
File: "folder/file.yaml",
Trace: []interface{}{"spec", 0, "image"},
}

var c2 = Change{
RegistryName: "my-registry2",
ResourceName: "my-resource2",
OldVersion: "3.0.0",
NewVersion: "4.0.0",
OldValue: "my-image2:3.0.0",
NewValue: "my-image2:4.0.0",
File: "folder/file2.yaml",
Trace: []interface{}{"spec", 0, "image2"},
}

func TestChangeIdentifier(t *testing.T) {
c := Change{
RegistryName: "my-registry",
ResourceName: "my-resource",
OldVersion: "1.0.0",
NewVersion: "2.0.0",
OldValue: "my-image:1.0.0",
NewValue: "my-image:2.0.0",
File: "folder/file.yaml",
Trace: []interface{}{"spec", 0, "image"},
}
assert.Equal(t, "folder/file.yaml#spec.0.image#my-image:2.0.0", c.Identifier())
assert.Equal(t, "folder/file.yaml#spec.0.image#my-image:2.0.0", c1.Identifier())
assert.Equal(t, "folder/file2.yaml#spec.0.image2#my-image2:4.0.0", c2.Identifier())
}

func TestChangesTitle(t *testing.T) {
assert.Equal(t, "Update my-resource:2.0.0", Changes{c1}.Title())
assert.Equal(t, "Update my-resource:2.0.0, my-resource2:4.0.0", Changes{c1, c2}.Title())
}

func TestChangesBranch(t *testing.T) {
cs := Changes{Change{
RegistryName: "my-registry",
ResourceName: "my-resource",
OldVersion: "1.0.0",
NewVersion: "2.0.0",
OldValue: "my-image:1.0.0",
NewValue: "my-image:2.0.0",
File: "folder/file.yaml",
Trace: []interface{}{"spec", 0, "image"},
}}
assert.Equal(t, "git-ops-update/b192fbd55a666b30", cs.Branch("git-ops-update"))
assert.Equal(t, "git-ops-update/b192fbd55a666b30", Changes{c1}.Branch("git-ops-update"))
assert.Equal(t, "git-ops-update/925d44a4f1703462", Changes{c1, c2}.Branch("git-ops-update"))
}
2 changes: 1 addition & 1 deletion internal/git_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ func (p GitHubGitProvider) Request(dir string, changes Changes, callbacks ...fun
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: p.AccessToken})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
pullTitle := "Update"
pullBase := string(baseBranch.Name())
pullHead := string(targetBranch)
pullTitle := changes.Title()
pullBody := changes.Message()
_, res, err := client.PullRequests.Create(context.Background(), *ownerName, *repoName, &github.NewPullRequest{
Title: &pullTitle,
Expand Down

0 comments on commit fc43209

Please sign in to comment.