diff --git a/main.go b/main.go index d870e756..261ce81d 100644 --- a/main.go +++ b/main.go @@ -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.") @@ -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() } diff --git a/repository/options.go b/repository/options.go index dde98396..da8d49ce 100644 --- a/repository/options.go +++ b/repository/options.go @@ -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 @@ -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 { @@ -92,6 +102,10 @@ 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 } @@ -99,3 +113,12 @@ func (o *GitHubOptions) setDefaultValues(git GitOptions) { 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 + } +} diff --git a/repository/pull_request.go b/repository/pull_request.go index 69b20839..91631534 100644 --- a/repository/pull_request.go +++ b/repository/pull_request.go @@ -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 { diff --git a/repository/strategy_append.go b/repository/strategy_append.go index 5b85e312..d4b09aeb 100644 --- a/repository/strategy_append.go +++ b/repository/strategy_append.go @@ -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 { @@ -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 { diff --git a/repository/strategy_reset.go b/repository/strategy_reset.go index a6ee4bec..3112d971 100644 --- a/repository/strategy_reset.go +++ b/repository/strategy_reset.go @@ -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 {