summaryrefslogtreecommitdiffstats
path: root/services/agit
diff options
context:
space:
mode:
authorGergely Nagy <forgejo@gergo.csillger.hu>2024-02-14 00:35:38 +0100
committerGergely Nagy <forgejo@gergo.csillger.hu>2024-02-14 21:17:55 +0100
commit259ad5d61493590732cc9fcc16158fc00ebdcdae (patch)
tree696cdfbcfb7d881b89bc7d5b4cb4a97628b51b2c /services/agit
parentMerge pull request '[BUG] Workaround borked Git version' (#2335) from gusted/... (diff)
downloadforgejo-259ad5d61493590732cc9fcc16158fc00ebdcdae.tar.xz
forgejo-259ad5d61493590732cc9fcc16158fc00ebdcdae.zip
agit: Automatically fill in the description
If no `-o description=` is provided, fill it in automatically from the first commit, just like title. Also allow filling in either, and specifying them independently. This means that `git push origin HEAD:refs/for/main/my-local-branch` will fill in the PR title, *and* the description, without having to specify additional parameters. The description is the first commit's message without the first two lines (the title and a newline, as customary). Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
Diffstat (limited to 'services/agit')
-rw-r--r--services/agit/agit.go28
1 files changed, 17 insertions, 11 deletions
diff --git a/services/agit/agit.go b/services/agit/agit.go
index bc68372570..98367ab973 100644
--- a/services/agit/agit.go
+++ b/services/agit/agit.go
@@ -38,6 +38,9 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
_, forcePush = opts.GitPushOptions["force-push"]
objectFormat, _ := gitRepo.GetObjectFormat()
+ title, hasTitle := opts.GitPushOptions["title"]
+ description, hasDesc := opts.GitPushOptions["description"]
+
for i := range opts.OldCommitIDs {
if opts.NewCommitIDs[i] == objectFormat.EmptyObjectID().String() {
results = append(results, private.HookProcReceiveRefResult{
@@ -102,18 +105,21 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
return nil, fmt.Errorf("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %w", ownerName, repoName, err)
}
- // create a new pull request
- if len(title) == 0 {
- var has bool
- title, has = opts.GitPushOptions["title"]
- if !has || len(title) == 0 {
- commit, err := gitRepo.GetCommit(opts.NewCommitIDs[i])
- if err != nil {
- return nil, fmt.Errorf("Failed to get commit %s in repository: %s/%s Error: %w", opts.NewCommitIDs[i], ownerName, repoName, err)
- }
- title = strings.Split(commit.CommitMessage, "\n")[0]
+ // automatically fill out the title and the description from the first commit.
+ shouldGetCommit := len(title) == 0 || len(description) == 0
+
+ var commit *git.Commit
+ if shouldGetCommit {
+ commit, err = gitRepo.GetCommit(opts.NewCommitIDs[i])
+ if err != nil {
+ return nil, fmt.Errorf("Failed to get commit %s in repository: %s/%s Error: %w", opts.NewCommitIDs[i], ownerName, repoName, err)
}
- description = opts.GitPushOptions["description"]
+ }
+ if !hasTitle || len(title) == 0 {
+ title = strings.Split(commit.CommitMessage, "\n")[0]
+ }
+ if !hasDesc || len(description) == 0 {
+ _, description, _ = strings.Cut(commit.CommitMessage, "\n\n")
}
pusher, err := user_model.GetUserByID(ctx, opts.UserID)