Skip to content

Commit

Permalink
Allow to set pull request body when defined (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
eko authored Jul 5, 2023
1 parent 00b155e commit 64c0dc5
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 21 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ if [ "${GIT_BRANCH_NAME}" == "master"]; then
--git_server github \
--release_branch master \
--gitops_pr_into master \
--gitops_pr_title "This is my pull request title" \
--gitops_pr_body "This is my pull request body message" \
--branch_name ${GIT_BRANCH_NAME} \
--git_commit ${GIT_COMMIT_ID} \
fi
Expand Down Expand Up @@ -488,6 +490,8 @@ if [ "${RELEASE_BRANCH}" == "release/team"]; then
--release_branch ${RELEASE_BRANCH} \
--deployment_branch_suffix=${RELEASE_BRANCH_SUFFIX} \
--gitops_pr_into master \
--gitops_pr_title "This is my pull request title" \
--gitops_pr_body "This is my pull request body message" \
--branch_name ${GIT_BRANCH_NAME} \
--git_commit ${GIT_COMMIT_ID} \
fi
Expand Down
8 changes: 4 additions & 4 deletions gitops/git/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ type pullrequest struct {
}

// CreatePR creates a pull request using branch names from and to
func CreatePR(from, to, title string) error {
func CreatePR(from, to, title, body string) error {
repo := repository{
Slug: "repo",
Project: project{"TM"},
}
prReq := pullrequest{
Title: title,
Description: title,
Description: body,
State: "OPEN",
Open: true,
Closed: false,
Expand Down Expand Up @@ -101,8 +101,8 @@ func CreatePR(from, to, title string) error {
}
log.Printf("bitbucket api response: %s", resp.Status)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
log.Print("bitbucket response: ", string(body))
responseBody, err := ioutil.ReadAll(resp.Body)
log.Print("bitbucket response: ", string(responseBody))
// 201 created
// 409 already exists
if resp.StatusCode == 201 {
Expand Down
6 changes: 3 additions & 3 deletions gitops/git/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestCreatePRRemote(t *testing.T) {
pass := "*************"
bitbucketUser = &user
bitbucketPassword = &pass
err := CreatePR("deploy/test1", "feature/AP-0000", "test")
err := CreatePR("deploy/test1", "feature/AP-0000", "test", "hello world")
if err != nil {
t.Error("Unexpected error from server: ", err)
}
Expand All @@ -43,14 +43,14 @@ func TestCreatePRNew(t *testing.T) {
oldendpoint := *apiEndpoint
defer func() { *apiEndpoint = oldendpoint }()
*apiEndpoint = ts.URL
err := CreatePR("deploy/test1", "feature/AP-0000", "test")
err := CreatePR("deploy/test1", "feature/AP-0000", "test", "hello world")
if err != nil {
t.Error("Unexpected error from server: ", err)
}
if srverr != nil {
t.Error("Unexpected error: ", srverr)
}
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}`
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}`
if string(buf) != expectedreq {
t.Error("Unexpected request body: ", string(buf))
}
Expand Down
8 changes: 4 additions & 4 deletions gitops/git/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
githubEnterpriseHost = flag.String("github_enterprise_host", "", "The host name of the private enterprise github, e.g. git.corp.adobe.com")
)

func CreatePR(from, to, title string) error {
func CreatePR(from, to, title, body string) error {
if *repoOwner == "" {
return errors.New("github_repo_owner must be set")
}
Expand Down Expand Up @@ -55,7 +55,7 @@ func CreatePR(from, to, title string) error {
Title: &title,
Head: &from,
Base: &to,
Body: &title,
Body: &body,
Issue: nil,
MaintainerCanModify: new(bool),
Draft: new(bool),
Expand All @@ -74,11 +74,11 @@ func CreatePR(from, to, title string) error {

// All other github responses
defer resp.Body.Close()
body, readingErr := ioutil.ReadAll(resp.Body)
responseBody, readingErr := ioutil.ReadAll(resp.Body)
if readingErr != nil {
log.Println("cannot read response body")
} else {
log.Println("github response: ", string(body))
log.Println("github response: ", string(responseBody))
}

return err
Expand Down
6 changes: 3 additions & 3 deletions gitops/git/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
accessToken = flag.String("gitlab_access_token", os.Getenv("GITLAB_TOKEN"), "the access token to authenticate requests")
)

func CreatePR(from, to, title string) error {
func CreatePR(from, to, title, body string) error {
if *accessToken == "" {
return errors.New("gitlab_access_token must be set")
}
Expand Down Expand Up @@ -57,11 +57,11 @@ func CreatePR(from, to, title string) error {

// All other gitlab responses
defer resp.Body.Close()
body, readingErr := ioutil.ReadAll(resp.Body)
responseBody, readingErr := ioutil.ReadAll(resp.Body)
if readingErr != nil {
log.Println("cannot read response body")
} else {
log.Println("gitlab response: ", string(body))
log.Println("gitlab response: ", string(responseBody))
}

return err
Expand Down
14 changes: 13 additions & 1 deletion gitops/git/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestCreatePRRemote(t *testing.T) {
from string
to string
title string
body string
}
tests := []struct {
repo string
Expand All @@ -27,20 +28,31 @@ func TestCreatePRRemote(t *testing.T) {
},
wantErr: false,
},
{
repo: "cotocisternas/rules_gitops_gitlab_test",
args: args{
from: "feature/gitlab-test",
to: "master",
title: "test_gitlab",
body: "hello world",
},
wantErr: false,
},
{
repo: "petabytecl/subgroup_rules_gitops_gitlab_test/rules_gitops_gitlab_test",
args: args{
from: "feature/gitlab-test",
to: "master",
title: "test_gitlab",
body: "hello world",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.repo, func(t *testing.T) {
repo = &tt.repo
if err := CreatePR(tt.args.from, tt.args.to, tt.args.title); (err != nil) != tt.wantErr {
if err := CreatePR(tt.args.from, tt.args.to, tt.args.title, tt.args.body); (err != nil) != tt.wantErr {
t.Errorf("CreatePR() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
13 changes: 8 additions & 5 deletions gitops/git/server.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package git

type Server interface {
CreatePR(from, to, title string) error
CreatePR(from, to, title, body string) error
}

type ServerFunc func(from, to, title string) error
type ServerFunc func(from, to, title, body string) error

func (f ServerFunc) CreatePR(from, to, title string) error {
return f(from, to, title)
}
func (f ServerFunc) CreatePR(from, to, title, body string) error {
if body == "" {
body = title
}

return f(from, to, title, body)
}
9 changes: 8 additions & 1 deletion gitops/prer/create_gitops_prs.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ var (
target = flag.String("target", "//... except //experimental/...", "target to scan. Useful for debugging only")
pushParallelism = flag.Int("push_parallelism", 5, "Number of image pushes to perform concurrently")
prInto = flag.String("gitops_pr_into", "master", "use this branch as the source branch and target for deployment PR")
prBody = flag.String("gitops_pr_body", "GitOps deployment <branch>", "a body message for deployment PR")
prTitle = flag.String("gitops_pr_title", "GitOps deployment <branch>", "a title for deployment PR")
branchName = flag.String("branch_name", "unknown", "Branch name to use in commit message")
gitCommit = flag.String("git_commit", "unknown", "Git commit to use in commit message")
deploymentBranchSuffix = flag.String("deployment_branch_suffix", "", "suffix to add to all deployment branch names")
Expand Down Expand Up @@ -249,7 +251,12 @@ func main() {
log.Println("dry-run: skipping PR creation: branch ", branch, "into ", *prInto)
continue
}
err := gitServer.CreatePR(branch, *prInto, fmt.Sprintf("GitOps deployment %s", branch))

if *prTitle == "" {
*prTitle = fmt.Sprintf("GitOps deployment %s", branch)
}

err := gitServer.CreatePR(branch, *prInto, *prTitle, *prBody)
if err != nil {
log.Fatal("unable to create PR: ", err)
}
Expand Down

0 comments on commit 64c0dc5

Please sign in to comment.