Skip to content

Commit 64c0dc5

Browse files
authored
Allow to set pull request body when defined (#137)
1 parent 00b155e commit 64c0dc5

File tree

8 files changed

+47
-21
lines changed

8 files changed

+47
-21
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ if [ "${GIT_BRANCH_NAME}" == "master"]; then
412412
--git_server github \
413413
--release_branch master \
414414
--gitops_pr_into master \
415+
--gitops_pr_title "This is my pull request title" \
416+
--gitops_pr_body "This is my pull request body message" \
415417
--branch_name ${GIT_BRANCH_NAME} \
416418
--git_commit ${GIT_COMMIT_ID} \
417419
fi
@@ -488,6 +490,8 @@ if [ "${RELEASE_BRANCH}" == "release/team"]; then
488490
--release_branch ${RELEASE_BRANCH} \
489491
--deployment_branch_suffix=${RELEASE_BRANCH_SUFFIX} \
490492
--gitops_pr_into master \
493+
--gitops_pr_title "This is my pull request title" \
494+
--gitops_pr_body "This is my pull request body message" \
491495
--branch_name ${GIT_BRANCH_NAME} \
492496
--git_commit ${GIT_COMMIT_ID} \
493497
fi

gitops/git/bitbucket/bitbucket.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ type pullrequest struct {
6363
}
6464

6565
// CreatePR creates a pull request using branch names from and to
66-
func CreatePR(from, to, title string) error {
66+
func CreatePR(from, to, title, body string) error {
6767
repo := repository{
6868
Slug: "repo",
6969
Project: project{"TM"},
7070
}
7171
prReq := pullrequest{
7272
Title: title,
73-
Description: title,
73+
Description: body,
7474
State: "OPEN",
7575
Open: true,
7676
Closed: false,
@@ -101,8 +101,8 @@ func CreatePR(from, to, title string) error {
101101
}
102102
log.Printf("bitbucket api response: %s", resp.Status)
103103
defer resp.Body.Close()
104-
body, err := ioutil.ReadAll(resp.Body)
105-
log.Print("bitbucket response: ", string(body))
104+
responseBody, err := ioutil.ReadAll(resp.Body)
105+
log.Print("bitbucket response: ", string(responseBody))
106106
// 201 created
107107
// 409 already exists
108108
if resp.StatusCode == 201 {

gitops/git/bitbucket/bitbucket_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestCreatePRRemote(t *testing.T) {
2525
pass := "*************"
2626
bitbucketUser = &user
2727
bitbucketPassword = &pass
28-
err := CreatePR("deploy/test1", "feature/AP-0000", "test")
28+
err := CreatePR("deploy/test1", "feature/AP-0000", "test", "hello world")
2929
if err != nil {
3030
t.Error("Unexpected error from server: ", err)
3131
}
@@ -43,14 +43,14 @@ func TestCreatePRNew(t *testing.T) {
4343
oldendpoint := *apiEndpoint
4444
defer func() { *apiEndpoint = oldendpoint }()
4545
*apiEndpoint = ts.URL
46-
err := CreatePR("deploy/test1", "feature/AP-0000", "test")
46+
err := CreatePR("deploy/test1", "feature/AP-0000", "test", "hello world")
4747
if err != nil {
4848
t.Error("Unexpected error from server: ", err)
4949
}
5050
if srverr != nil {
5151
t.Error("Unexpected error: ", srverr)
5252
}
53-
expectedreq := `{"title":"test","description":"test","state":"OPEN","open":true,"closed":false,"fromRef":{"id":"refs/heads/deploy/test1","repository":{"slug":"repo","project":{"key":"TM"}}},"toRef":{"id":"refs/heads/feature/AP-0000","repository":{"slug":"repo","project":{"key":"TM"}}},"locked":false}`
53+
expectedreq := `{"title":"test","description":"hello world","state":"OPEN","open":true,"closed":false,"fromRef":{"id":"refs/heads/deploy/test1","repository":{"slug":"repo","project":{"key":"TM"}}},"toRef":{"id":"refs/heads/feature/AP-0000","repository":{"slug":"repo","project":{"key":"TM"}}},"locked":false}`
5454
if string(buf) != expectedreq {
5555
t.Error("Unexpected request body: ", string(buf))
5656
}

gitops/git/github/github.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020
githubEnterpriseHost = flag.String("github_enterprise_host", "", "The host name of the private enterprise github, e.g. git.corp.adobe.com")
2121
)
2222

23-
func CreatePR(from, to, title string) error {
23+
func CreatePR(from, to, title, body string) error {
2424
if *repoOwner == "" {
2525
return errors.New("github_repo_owner must be set")
2626
}
@@ -55,7 +55,7 @@ func CreatePR(from, to, title string) error {
5555
Title: &title,
5656
Head: &from,
5757
Base: &to,
58-
Body: &title,
58+
Body: &body,
5959
Issue: nil,
6060
MaintainerCanModify: new(bool),
6161
Draft: new(bool),
@@ -74,11 +74,11 @@ func CreatePR(from, to, title string) error {
7474

7575
// All other github responses
7676
defer resp.Body.Close()
77-
body, readingErr := ioutil.ReadAll(resp.Body)
77+
responseBody, readingErr := ioutil.ReadAll(resp.Body)
7878
if readingErr != nil {
7979
log.Println("cannot read response body")
8080
} else {
81-
log.Println("github response: ", string(body))
81+
log.Println("github response: ", string(responseBody))
8282
}
8383

8484
return err

gitops/git/gitlab/gitlab.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
accessToken = flag.String("gitlab_access_token", os.Getenv("GITLAB_TOKEN"), "the access token to authenticate requests")
1818
)
1919

20-
func CreatePR(from, to, title string) error {
20+
func CreatePR(from, to, title, body string) error {
2121
if *accessToken == "" {
2222
return errors.New("gitlab_access_token must be set")
2323
}
@@ -57,11 +57,11 @@ func CreatePR(from, to, title string) error {
5757

5858
// All other gitlab responses
5959
defer resp.Body.Close()
60-
body, readingErr := ioutil.ReadAll(resp.Body)
60+
responseBody, readingErr := ioutil.ReadAll(resp.Body)
6161
if readingErr != nil {
6262
log.Println("cannot read response body")
6363
} else {
64-
log.Println("gitlab response: ", string(body))
64+
log.Println("gitlab response: ", string(responseBody))
6565
}
6666

6767
return err

gitops/git/gitlab/gitlab_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func TestCreatePRRemote(t *testing.T) {
1212
from string
1313
to string
1414
title string
15+
body string
1516
}
1617
tests := []struct {
1718
repo string
@@ -27,20 +28,31 @@ func TestCreatePRRemote(t *testing.T) {
2728
},
2829
wantErr: false,
2930
},
31+
{
32+
repo: "cotocisternas/rules_gitops_gitlab_test",
33+
args: args{
34+
from: "feature/gitlab-test",
35+
to: "master",
36+
title: "test_gitlab",
37+
body: "hello world",
38+
},
39+
wantErr: false,
40+
},
3041
{
3142
repo: "petabytecl/subgroup_rules_gitops_gitlab_test/rules_gitops_gitlab_test",
3243
args: args{
3344
from: "feature/gitlab-test",
3445
to: "master",
3546
title: "test_gitlab",
47+
body: "hello world",
3648
},
3749
wantErr: false,
3850
},
3951
}
4052
for _, tt := range tests {
4153
t.Run(tt.repo, func(t *testing.T) {
4254
repo = &tt.repo
43-
if err := CreatePR(tt.args.from, tt.args.to, tt.args.title); (err != nil) != tt.wantErr {
55+
if err := CreatePR(tt.args.from, tt.args.to, tt.args.title, tt.args.body); (err != nil) != tt.wantErr {
4456
t.Errorf("CreatePR() error = %v, wantErr %v", err, tt.wantErr)
4557
}
4658
})

gitops/git/server.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package git
22

33
type Server interface {
4-
CreatePR(from, to, title string) error
4+
CreatePR(from, to, title, body string) error
55
}
66

7-
type ServerFunc func(from, to, title string) error
7+
type ServerFunc func(from, to, title, body string) error
88

9-
func (f ServerFunc) CreatePR(from, to, title string) error {
10-
return f(from, to, title)
11-
}
9+
func (f ServerFunc) CreatePR(from, to, title, body string) error {
10+
if body == "" {
11+
body = title
12+
}
1213

14+
return f(from, to, title, body)
15+
}

gitops/prer/create_gitops_prs.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ var (
6161
target = flag.String("target", "//... except //experimental/...", "target to scan. Useful for debugging only")
6262
pushParallelism = flag.Int("push_parallelism", 5, "Number of image pushes to perform concurrently")
6363
prInto = flag.String("gitops_pr_into", "master", "use this branch as the source branch and target for deployment PR")
64+
prBody = flag.String("gitops_pr_body", "GitOps deployment <branch>", "a body message for deployment PR")
65+
prTitle = flag.String("gitops_pr_title", "GitOps deployment <branch>", "a title for deployment PR")
6466
branchName = flag.String("branch_name", "unknown", "Branch name to use in commit message")
6567
gitCommit = flag.String("git_commit", "unknown", "Git commit to use in commit message")
6668
deploymentBranchSuffix = flag.String("deployment_branch_suffix", "", "suffix to add to all deployment branch names")
@@ -249,7 +251,12 @@ func main() {
249251
log.Println("dry-run: skipping PR creation: branch ", branch, "into ", *prInto)
250252
continue
251253
}
252-
err := gitServer.CreatePR(branch, *prInto, fmt.Sprintf("GitOps deployment %s", branch))
254+
255+
if *prTitle == "" {
256+
*prTitle = fmt.Sprintf("GitOps deployment %s", branch)
257+
}
258+
259+
err := gitServer.CreatePR(branch, *prInto, *prTitle, *prBody)
253260
if err != nil {
254261
log.Fatal("unable to create PR: ", err)
255262
}

0 commit comments

Comments
 (0)