Skip to content

Commit

Permalink
[MISC] Add customized commit message support to PR (#1126)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesHarness authored and Harness committed Mar 21, 2024
1 parent c2a943b commit fd2f3c1
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions app/api/controller/pullreq/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pullreq
import (
"context"
"fmt"
"strings"
"time"

apiauth "github.com/harness/gitness/app/api/auth"
Expand All @@ -40,6 +41,8 @@ import (
type MergeInput struct {
Method enum.MergeMethod `json:"method"`
SourceSHA string `json:"source_sha"`
Title string `json:"title"`
Message string `json:"message"`
BypassRules bool `json:"bypass_rules"`
DryRun bool `json:"dry_run"`
}
Expand All @@ -62,6 +65,14 @@ func (in *MergeInput) sanitize() error {
in.Method = method
}

// cleanup title / message (NOTE: git doesn't support white space only)
in.Title = strings.TrimSpace(in.Title)
in.Message = strings.TrimSpace(in.Message)

if in.Method == enum.MergeMethodRebase && (in.Title != "" || in.Message != "") {
return usererror.BadRequest("rebase doesn't support customizing commit title and message")
}

return nil
}

Expand Down Expand Up @@ -304,15 +315,16 @@ func (c *Controller) Merge(
committer = identityFromPrincipalInfo(*session.Principal.ToPrincipalInfo())
}

var mergeTitle string

switch in.Method {
case enum.MergeMethodMerge:
mergeTitle = fmt.Sprintf("Merge branch '%s' of %s (#%d)", pr.SourceBranch, sourceRepo.Path, pr.Number)
case enum.MergeMethodSquash:
mergeTitle = fmt.Sprintf("%s (#%d)", pr.Title, pr.Number)
case enum.MergeMethodRebase:
mergeTitle = "" // Not used.
// backfill commit title if none provided
if in.Title == "" {
switch in.Method {
case enum.MergeMethodMerge:
in.Title = fmt.Sprintf("Merge branch '%s' of %s (#%d)", pr.SourceBranch, sourceRepo.Path, pr.Number)
case enum.MergeMethodSquash:
in.Title = fmt.Sprintf("%s (#%d)", pr.Title, pr.Number)
case enum.MergeMethodRebase:
// Not used.
}
}

// create merge commit(s)
Expand All @@ -325,8 +337,8 @@ func (c *Controller) Merge(
BaseBranch: pr.TargetBranch,
HeadRepoUID: sourceRepo.GitUID,
HeadBranch: pr.SourceBranch,
Title: mergeTitle,
Message: "",
Title: in.Title,
Message: in.Message,
Committer: committer,
CommitterDate: &now,
Author: author,
Expand Down

0 comments on commit fd2f3c1

Please sign in to comment.