Skip to content

Commit 0f580cf

Browse files
authored
Merge pull request #21635 from mpherman2/gerrit_add_upstream
Fix Broken gerrit support for autobumper
2 parents 7f9edc8 + 08eafce commit 0f580cf

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

prow/cmd/generic-autobumper/bumper/bumper.go

+20-6
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ type Gerrit struct {
136136
AutobumpPRIdentifier string `yaml:"autobumpPRIdentifier"`
137137
// Gerrit CR Author. Only Required if using gerrit
138138
Author string `yaml:"author"`
139+
// Email account associated with gerrit author. Only required if using gerrit.
140+
Email string `yaml:"email"`
139141
// The path to the Gerrit httpcookie file. Only Required if using gerrit
140142
CookieFile string `yaml:"cookieFile"`
141143
// The path to the hosted Gerrit repo
@@ -375,7 +377,7 @@ func Run(o *Options) error {
375377
}
376378
msg := makeGerritCommit(o.Prefixes, versions, o.Gerrit.AutobumpPRIdentifier, changeId)
377379
// TODO(mpherman): Add reviewers to CreateCR
378-
if err := createCR(msg, "master", changeId, o.Gerrit.HostRepo, o.Gerrit.CookieFile, nil, nil, stdout, stderr); err != nil {
380+
if err := createCR(msg, "master", changeId, o.Gerrit.HostRepo, o.Gerrit.CookieFile, o.Gerrit.Author, o.Gerrit.Email, nil, nil, stdout, stderr); err != nil {
379381
return fmt.Errorf("Failled to create the CR: %w", err)
380382
}
381383
}
@@ -724,7 +726,7 @@ func MakeGitCommit(remote, remoteBranch, name, email string, prefixes []Prefix,
724726
}
725727

726728
func makeGerritCommit(prefixes []Prefix, versions map[string][]string, commitTag, changeId string) string {
727-
return fmt.Sprintf("[%s] %s \n\nChange-Id: %s", commitTag, makeCommitSummary(prefixes, versions), changeId)
729+
return fmt.Sprintf("%s\n\n[%s]\n\nChange-Id: %s", makeCommitSummary(prefixes, versions), commitTag, changeId)
728730
}
729731

730732
// GitCommitAndPush runs a sequence of git commands to commit.
@@ -981,8 +983,8 @@ func GerritNoOpChange(changeID, hostRepo string) (bool, error) {
981983
var garbageBuf bytes.Buffer
982984
var outBuf bytes.Buffer
983985
// Fetch current pending CRs
984-
if err := Call(&garbageBuf, &garbageBuf, gitCmd, "fetch", "origin", "+refs/changes/*:refs/remotes/origin/changes/*"); err != nil {
985-
return false, fmt.Errorf("unable to fetch origin changes: %v -- \nOUTPUT: %s", err, garbageBuf.String())
986+
if err := Call(&garbageBuf, &garbageBuf, gitCmd, "fetch", "upstream", "+refs/changes/*:refs/remotes/upstream/changes/*"); err != nil {
987+
return false, fmt.Errorf("unable to fetch upstream changes: %v -- \nOUTPUT: %s", err, garbageBuf.String())
986988
}
987989
// Get PR with same ChangeID for this bump
988990
if err := Call(&outBuf, &garbageBuf, gitCmd, "log", "--all", fmt.Sprintf("--grep=Change-Id: %s", changeID), "-1", "--format=%H"); err != nil {
@@ -1004,7 +1006,19 @@ func GerritNoOpChange(changeID, hostRepo string) (bool, error) {
10041006

10051007
}
10061008

1007-
func createCR(msg, branch, changeID, hostRepo, cookieFile string, reviewers, cc []string, stdout, stderr io.Writer) error {
1009+
func createCR(msg, branch, changeID, hostRepo, cookieFile, author, email string, reviewers, cc []string, stdout, stderr io.Writer) error {
1010+
if err := Call(stdout, stderr, gitCmd, "config", "http.cookiefile", cookieFile); err != nil {
1011+
return fmt.Errorf("unable to load cookiefile: %v", err)
1012+
}
1013+
if err := Call(stdout, stderr, gitCmd, "config", "user.name", author); err != nil {
1014+
return fmt.Errorf("unable to set username: %v", err)
1015+
}
1016+
if err := Call(stdout, stderr, gitCmd, "config", "user.email", email); err != nil {
1017+
return fmt.Errorf("unable to set password: %v", err)
1018+
}
1019+
if err := Call(stdout, stderr, gitCmd, "remote", "add", "upstream", hostRepo); err != nil {
1020+
return fmt.Errorf("unable to add upstream remote: %v", err)
1021+
}
10081022
noOp, err := GerritNoOpChange(changeID, hostRepo)
10091023
if err != nil {
10101024
return fmt.Errorf("error diffing previous bump: %v", err)
@@ -1018,7 +1032,7 @@ func createCR(msg, branch, changeID, hostRepo, cookieFile string, reviewers, cc
10181032
if err := Call(stdout, stderr, gitCmd, "commit", "-a", "-v", "-m", msg); err != nil {
10191033
return fmt.Errorf("unable to commit: %v", err)
10201034
}
1021-
if err := Call(stdout, stderr, gitCmd, "push", "origin", pushRef); err != nil {
1035+
if err := Call(stdout, stderr, gitCmd, "push", "upstream", pushRef); err != nil {
10221036
return fmt.Errorf("unable to push: %v", err)
10231037
}
10241038
return nil

0 commit comments

Comments
 (0)