From 68f9b52c71fd11c3268854270ae30d63f1189b3b Mon Sep 17 00:00:00 2001 From: Divya Jain Date: Fri, 23 May 2025 00:02:04 +0530 Subject: [PATCH 1/2] patchutil: add ability to add footers to commit message of a format-patch --- patchutil/patchutil.go | 6 + patchutil/patchutil_test.go | 265 ++++++++++++++++++++++++++++++++++++ 2 files changed, 271 insertions(+) diff --git a/patchutil/patchutil.go b/patchutil/patchutil.go index 6920f97..4f1386d 100644 --- a/patchutil/patchutil.go +++ b/patchutil/patchutil.go @@ -316,3 +316,9 @@ func AsNiceDiff(patch, targetBranch string) types.NiceDiff { return nd } + +// add commit message footers to a format-patch +func AddCommitMessageFooters(patch string, footers string) string { + re := regexp.MustCompile(`\n(Subject: )([\s\S]*?)(---\n|\ndiff --git )`) + return re.ReplaceAllString(patch, "\n${1}${2}\n"+footers+"\n${3}") +} diff --git a/patchutil/patchutil_test.go b/patchutil/patchutil_test.go index d0d8c1f..a2fae31 100644 --- a/patchutil/patchutil_test.go +++ b/patchutil/patchutil_test.go @@ -322,3 +322,268 @@ content }) } } + +func TestAddCommmitMessageFooters(t *testing.T) { + tests := []struct { + name string + input string + footers string + expected string + }{ + { + name: "Single patch", + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:01:00 +0300 +Subject: [PATCH] Example patch + +more message body + +--- + file.txt | 2 +- + 1 file changed, 1 insertions(+), 1 deletions(-) + +diff --git a/file.txt b/file.txt +index 123456..789012 100644 +--- a/file.txt ++++ b/file.txt +@@ -1 +1 @@ +-old content ++new content +-- +2.48.1`, + footers: `Pull-Id: Author +Merge-Request: 123`, + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:01:00 +0300 +Subject: [PATCH] Example patch + +more message body + + +Pull-Id: Author +Merge-Request: 123 +--- + file.txt | 2 +- + 1 file changed, 1 insertions(+), 1 deletions(-) + +diff --git a/file.txt b/file.txt +index 123456..789012 100644 +--- a/file.txt ++++ b/file.txt +@@ -1 +1 @@ +-old content ++new content +-- +2.48.1`, + }, + { + name: "Two patches with --no-stat", + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:01:00 +0300 +Subject: [PATCH 1/2] First patch + +more message body + +--- + file.txt | 2 +- + 1 file changed, 1 insertions(+), 1 deletions(-) + +diff --git a/file1.txt b/file1.txt +index 123456..789012 100644 +--- a/file1.txt ++++ b/file1.txt +@@ -1 +1 @@ +-old content ++new content +-- +2.48.1 +From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:03:11 +0300 +Subject: [PATCH 2/2] Second patch + +more message body + +--- + file.txt | 2 +- + 1 file changed, 1 insertions(+), 1 deletions(-) + +diff --git a/file2.txt b/file2.txt +index abcdef..ghijkl 100644 +--- a/file2.txt ++++ b/file2.txt +@@ -1 +1 @@ +-foo bar ++baz qux +-- +2.48.1`, + footers: `Pull-Id: Author +Merge-Request: 123`, + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:01:00 +0300 +Subject: [PATCH 1/2] First patch + +more message body + + +Pull-Id: Author +Merge-Request: 123 +--- + file.txt | 2 +- + 1 file changed, 1 insertions(+), 1 deletions(-) + +diff --git a/file1.txt b/file1.txt +index 123456..789012 100644 +--- a/file1.txt ++++ b/file1.txt +@@ -1 +1 @@ +-old content ++new content +-- +2.48.1 +From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:03:11 +0300 +Subject: [PATCH 2/2] Second patch + +more message body + + +Pull-Id: Author +Merge-Request: 123 +--- + file.txt | 2 +- + 1 file changed, 1 insertions(+), 1 deletions(-) + +diff --git a/file2.txt b/file2.txt +index abcdef..ghijkl 100644 +--- a/file2.txt ++++ b/file2.txt +@@ -1 +1 @@ +-foo bar ++baz qux +-- +2.48.1`, + }, + { + name: "Single patch with no stat", + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:01:00 +0300 +Subject: [PATCH] Example patch + +diff --git a/file.txt b/file.txt +index 123456..789012 100644 +--- a/file.txt ++++ b/file.txt +@@ -1 +1 @@ +-old content ++new content +-- +2.48.1`, + footers: `Pull-Id: Author +Merge-Request: 123`, + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:01:00 +0300 +Subject: [PATCH] Example patch + +Pull-Id: Author +Merge-Request: 123 + +diff --git a/file.txt b/file.txt +index 123456..789012 100644 +--- a/file.txt ++++ b/file.txt +@@ -1 +1 @@ +-old content ++new content +-- +2.48.1`, + }, + { + name: "Two patches with no stat", + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:01:00 +0300 +Subject: [PATCH 1/2] First patch + +with long message body + +diff --git a/file1.txt b/file1.txt +index 123456..789012 100644 +--- a/file1.txt ++++ b/file1.txt +@@ -1 +1 @@ +-old content ++new content +-- +2.48.1 +From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:03:11 +0300 +Subject: [PATCH 2/2] Second patch + +diff --git a/file2.txt b/file2.txt +index abcdef..ghijkl 100644 +--- a/file2.txt ++++ b/file2.txt +@@ -1 +1 @@ +-foo bar ++baz qux +-- +2.48.1`, + footers: `Pull-Id: Author +Merge-Request: 123`, + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:01:00 +0300 +Subject: [PATCH 1/2] First patch + +with long message body + +Pull-Id: Author +Merge-Request: 123 + +diff --git a/file1.txt b/file1.txt +index 123456..789012 100644 +--- a/file1.txt ++++ b/file1.txt +@@ -1 +1 @@ +-old content ++new content +-- +2.48.1 +From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 +From: Author +Date: Wed, 16 Apr 2025 11:03:11 +0300 +Subject: [PATCH 2/2] Second patch + +Pull-Id: Author +Merge-Request: 123 + +diff --git a/file2.txt b/file2.txt +index abcdef..ghijkl 100644 +--- a/file2.txt ++++ b/file2.txt +@@ -1 +1 @@ +-foo bar ++baz qux +-- +2.48.1`, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := AddCommitMessageFooters(tt.input, tt.footers) + if result != tt.expected { + t.Errorf("Got:\n========\n%v\n========\nExpected:\n========\n%v\n========\n", result, tt.expected) + } + }) + } +} -- 2.47.2 From 3cd2c301a56b28c436a580fa549bafe3ba0ea167 Mon Sep 17 00:00:00 2001 From: Divya Jain Date: Thu, 15 May 2025 23:26:00 +0530 Subject: [PATCH 2/2] appview: state: add Pull-id and Merged-by commit footers --- appview/pulls/pulls.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/appview/pulls/pulls.go b/appview/pulls/pulls.go index ab80aff..9494c6f 100644 --- a/appview/pulls/pulls.go +++ b/appview/pulls/pulls.go @@ -1915,8 +1915,18 @@ func (s *Pulls) MergePull(w http.ResponseWriter, r *http.Request) { return } + actor := s.oauth.GetUser(r) // no need to check for nil as this is an authenticated request + + footers := strings.Join([]string{ + fmt.Sprintf("Pull-id: %s", pull.PullAt()), + fmt.Sprintf("Merged-by: %s", actor.Did), + }, "\n") + + pullBody := pull.Body + "\n\n" + footers + patch = patchutil.AddCommitMessageFooters(patch, footers) + // Merge the pull request - resp, err := ksClient.Merge([]byte(patch), f.OwnerDid(), f.RepoName, pull.TargetBranch, pull.Title, pull.Body, ident.Handle.String(), email.Address) + resp, err := ksClient.Merge([]byte(patch), f.OwnerDid(), f.RepoName, pull.TargetBranch, pull.Title, pullBody, ident.Handle.String(), email.Address) if err != nil { log.Printf("failed to merge pull request: %s", err) s.pages.Notice(w, "pull-merge-error", "Failed to merge pull request. Try again later.") -- 2.47.2