Skip to content

Commit

Permalink
Merge pull request #8 from dailymotion/pr-update-mode
Browse files Browse the repository at this point in the history
feat: custom update mode for PR title/body
  • Loading branch information
vbehar authored Apr 20, 2020
2 parents 4a69363 + a28b7c4 commit b1a80ea
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 15 deletions.
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func init() {

// pull-request flags
pflag.StringVar(&options.GitHub.PullRequest.Title, "pr-title", "", "")
pflag.StringVar(&options.GitHub.PullRequest.TitleUpdateOperation, "pr-title-update-operation", "", "operation when updating the PR's title: ignore (keep old value), replace, prepend or append. Default is: ignore for append strategy, replace for reset strategy.")
pflag.StringVar(&options.GitHub.PullRequest.Body, "pr-body", "", "")
pflag.StringVar(&options.GitHub.PullRequest.BodyFile, "pr-body-file", "", "")
pflag.StringVar(&options.GitHub.PullRequest.BodyUpdateOperation, "pr-body-update-operation", "", "operation when updating the PR's body: ignore (keep old value), replace, prepend or append. Default is: ignore for append strategy, replace for reset strategy.")
pflag.StringArrayVar(&options.GitHub.PullRequest.Comments, "pr-comment", []string{}, "")
pflag.StringSliceVar(&options.GitHub.PullRequest.Labels, "pr-labels", []string{"octopilot-update"}, "List of labels set on the pull requests, and used to find existing pull requests to update.")
pflag.StringVar(&options.GitHub.PullRequest.BaseBranch, "pr-base-branch", "master", "Name of the branch used as a base when creating pull requests.")
Expand Down Expand Up @@ -194,12 +197,17 @@ func temporaryDirectory() string {

func defaultCommitFooter() string {
footer := new(strings.Builder)
footer.WriteString(fmt.Sprintf("This is an automatic commit generated by [OctoPilot](https://github.com/dailymotion/octopilot) [v%[1]s](https://github.com/dailymotion/octopilot/releases/tag/v%[1]s)", buildVersion))
footer.WriteString("Generated by [OctoPilot](https://github.com/dailymotion/octopilot)")
if buildVersion == "dev" {
footer.WriteString(" (dev version)")
} else {
footer.WriteString(fmt.Sprintf(" [v%[1]s](https://github.com/dailymotion/octopilot/releases/tag/v%[1]s)", buildVersion))
}
if repoURL := git.CurrentRepositoryURL(); len(repoURL) > 0 {
footer.WriteString(fmt.Sprintf("\nRunning from repository %s", repoURL))
footer.WriteString(fmt.Sprintf(" from %s", repoURL))
} else if currentDir, err := os.Getwd(); err == nil {
dirName := filepath.Base(currentDir)
footer.WriteString(fmt.Sprintf("\nRunning from %s", dirName))
footer.WriteString(fmt.Sprintf(" from %s", dirName))
}
return footer.String()
}
Expand Down
37 changes: 30 additions & 7 deletions repository/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"github.com/dailymotion/octopilot/update"
)

const (
IgnoreUpdateOperation = "ignore"
ReplaceUpdateOperation = "replace"
PrependUpdateOperation = "prepend"
AppendUpdateOperation = "append"
)

type UpdateOptions struct {
DryRun bool
KeepFiles bool
Expand Down Expand Up @@ -36,13 +43,16 @@ type GitHubOptions struct {
}

type PullRequestOptions struct {
Labels []string
BaseBranch string
Title string
Body string
Comments []string
Draft bool
Merge PullRequestMergeOptions
Labels []string
BaseBranch string
Title string
TitleUpdateOperation string
Body string
BodyFile string
BodyUpdateOperation string
Comments []string
Draft bool
Merge PullRequestMergeOptions
}

type PullRequestMergeOptions struct {
Expand Down Expand Up @@ -92,10 +102,23 @@ func (o *GitHubOptions) setDefaultValues(git GitOptions) {
if len(o.PullRequest.Title) == 0 {
o.PullRequest.Title = git.CommitTitle
}
if len(o.PullRequest.Body) == 0 && len(o.PullRequest.BodyFile) > 0 {
data, _ := ioutil.ReadFile(o.PullRequest.BodyFile)
o.PullRequest.Body = string(data)
}
if len(o.PullRequest.Body) == 0 {
o.PullRequest.Body = git.CommitBody
}
if len(git.CommitFooter) > 0 {
o.PullRequest.Body += fmt.Sprintf("\n\n-- \n%s", git.CommitFooter)
}
}

func (o *GitHubOptions) setDefaultUpdateOperation(defaultUpdateOperation string) {
if len(o.PullRequest.TitleUpdateOperation) == 0 {
o.PullRequest.TitleUpdateOperation = defaultUpdateOperation
}
if len(o.PullRequest.BodyUpdateOperation) == 0 {
o.PullRequest.BodyUpdateOperation = defaultUpdateOperation
}
}
30 changes: 26 additions & 4 deletions repository/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,34 @@ func (r Repository) updatePullRequest(ctx context.Context, options GitHubOptions
)

if len(options.PullRequest.Title) > 0 {
pr.Title = github.String(options.PullRequest.Title)
needUpdate = true
switch options.PullRequest.TitleUpdateOperation {
case IgnoreUpdateOperation:
// nothing to do
case ReplaceUpdateOperation:
pr.Title = github.String(options.PullRequest.Title)
needUpdate = true
case PrependUpdateOperation:
pr.Title = github.String(fmt.Sprintf("%s %s", options.PullRequest.Title, pr.GetTitle()))
needUpdate = true
case AppendUpdateOperation:
pr.Title = github.String(fmt.Sprintf("%s %s", pr.GetTitle(), options.PullRequest.Title))
needUpdate = true
}
}
if len(options.PullRequest.Body) > 0 {
pr.Body = github.String(options.PullRequest.Body)
needUpdate = true
switch options.PullRequest.BodyUpdateOperation {
case IgnoreUpdateOperation:
// nothing to do
case ReplaceUpdateOperation:
pr.Body = github.String(options.PullRequest.Body)
needUpdate = true
case PrependUpdateOperation:
pr.Body = github.String(fmt.Sprintf("%s\n\n%s", options.PullRequest.Body, pr.GetBody()))
needUpdate = true
case AppendUpdateOperation:
pr.Body = github.String(fmt.Sprintf("%s\n\n%s", pr.GetBody(), options.PullRequest.Body))
needUpdate = true
}
}

if needUpdate {
Expand Down
3 changes: 2 additions & 1 deletion repository/strategy_append.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func (s *AppendStrategy) Run(ctx context.Context) (bool, *github.PullRequest, er
}

s.Options.Git.setDefaultValues(s.Updaters)
s.Options.GitHub.setDefaultValues(s.Options.Git)
s.Options.GitHub.setDefaultUpdateOperation(IgnoreUpdateOperation)

changesCommitted, err := commitChanges(ctx, gitRepo, s.Options)
if err != nil {
Expand All @@ -79,7 +81,6 @@ func (s *AppendStrategy) Run(ctx context.Context) (bool, *github.PullRequest, er
if existingPR != nil {
pr, err = s.Repository.updatePullRequest(ctx, s.Options.GitHub, existingPR)
} else {
s.Options.GitHub.setDefaultValues(s.Options.Git)
pr, err = s.Repository.createPullRequest(ctx, s.Options.GitHub, branchName)
}
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions repository/strategy_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (s *ResetStrategy) Run(ctx context.Context) (bool, *github.PullRequest, err

s.Options.Git.setDefaultValues(s.Updaters)
s.Options.GitHub.setDefaultValues(s.Options.Git)
s.Options.GitHub.setDefaultUpdateOperation(ReplaceUpdateOperation)

changesCommitted, err := commitChanges(ctx, gitRepo, s.Options)
if err != nil {
Expand Down

0 comments on commit b1a80ea

Please sign in to comment.