From d2c608827a24ecfa77225c7ea72693de21a71d74 Mon Sep 17 00:00:00 2001 From: Divya Jain Date: Thu, 15 May 2025 23:26:00 +0530 Subject: [PATCH] appview: state: add Pull-id and Merged-by commit footers --- appview/state/pull.go | 15 +- patchutil/patchutil.go | 5 + patchutil/patchutil_test.go | 265 ++++++++++++++++++++++++++++++++++++ 3 files changed, 283 insertions(+), 2 deletions(-) diff --git a/appview/state/pull.go b/appview/state/pull.go index 6bb2ac2..20fadbe 100644 --- a/appview/state/pull.go +++ b/appview/state/pull.go @@ -9,6 +9,7 @@ import ( "log" "net/http" "strconv" + "strings" "time" "tangled.sh/tangled.sh/core/api/tangled" @@ -295,7 +296,6 @@ func (s *State) RepoPullPatch(w http.ResponseWriter, r *http.Request) { Submission: pull.Submissions[roundIdInt], Diff: &diff, }) - } func (s *State) RepoPullInterdiff(w http.ResponseWriter, r *http.Request) { @@ -1534,6 +1534,17 @@ func (s *State) MergePull(w http.ResponseWriter, r *http.Request) { log.Printf("failed to get primary email: %s", err) } + 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.AddCommitFooters(pull.LatestPatch(), footers) + ksClient, err := knotclient.NewSignedClient(f.Knot, secret, s.config.Core.Dev) if err != nil { log.Printf("failed to create signed client for %s: %s", f.Knot, err) @@ -1542,7 +1553,7 @@ func (s *State) MergePull(w http.ResponseWriter, r *http.Request) { } // Merge the pull request - resp, err := ksClient.Merge([]byte(pull.LatestPatch()), 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.") diff --git a/patchutil/patchutil.go b/patchutil/patchutil.go index a0a94dd..44b1551 100644 --- a/patchutil/patchutil.go +++ b/patchutil/patchutil.go @@ -194,3 +194,8 @@ func Unified(oldText, oldFile, newText, newFile string) (string, error) { return string(output), nil } + +func AddCommitFooters(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..04b4745 100644 --- a/patchutil/patchutil_test.go +++ b/patchutil/patchutil_test.go @@ -322,3 +322,268 @@ content }) } } + +func TestAddCommmitFooters(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 := AddCommitFooters(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