diff --git a/hunk.go b/hunk.go index 7ff5db98..dbf8238a 100644 --- a/hunk.go +++ b/hunk.go @@ -1,12 +1,12 @@ /* Copyright 2024 Qiniu Cloud (qiniu.com). - + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - + http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/hunk_test.go b/hunk_test.go index 40b704f9..e0b81c99 100644 --- a/hunk_test.go +++ b/hunk_test.go @@ -1,12 +1,12 @@ /* Copyright 2024 Qiniu Cloud (qiniu.com). - + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - + http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/internal/linters/git-flow/rebase-suggestion/rebase_suggestion.go b/internal/linters/git-flow/rebase-suggestion/rebase_suggestion.go index f83cbf04..8641de19 100644 --- a/internal/linters/git-flow/rebase-suggestion/rebase_suggestion.go +++ b/internal/linters/git-flow/rebase-suggestion/rebase_suggestion.go @@ -42,6 +42,7 @@ func rebaseSuggestionHandler(log *xlog.Logger, linterConfig config.Linter, agent org = event.GetRepo().GetOwner().GetLogin() repo = event.GetRepo().GetName() number = event.GetNumber() + author = event.GetSender().GetLogin() ) preFilterCommits, err := listMatchedCommitMessages(context.Background(), agent, org, repo, number) @@ -54,17 +55,17 @@ func rebaseSuggestionHandler(log *xlog.Logger, linterConfig config.Linter, agent return err } - return handle(context.Background(), log, agent, org, repo, number, preFilterCommits, existedComments) + return handle(context.Background(), log, agent, org, repo, author, number, preFilterCommits, existedComments) } -var rebaseSuggestionFlag = "**[REBASE SUGGESTION]**" +const rebaseSuggestionFlag = "REBASE SUGGESTION" -var rebaseSuggestionTmpl = rebaseSuggestionFlag + ` This PR has commit message like: -{{range .}} +var rebaseSuggestionTmpl = ` **[{{.Flag}}]** Hi @{{.Author}}, your PR has commit messages like: +{{range .TargetCommits}} > {{.}} {{end}} -Which seems insignificant, recommend to use ` + "`git rebase `" + `to reorganize your PR. +Which seems insignificant, recommend to use` + ` **`+"`"+`git rebase /`+"`"+`** `+ `to reorganize your PR.
@@ -75,19 +76,31 @@ If you have any questions about this comment, feel free to raise an issue here:
` -func handle(ctx context.Context, log *xlog.Logger, agent linters.Agent, org, repo string, number int, prefilterCommits []*github.RepositoryCommit, existedComments []*github.IssueComment) error { +type RebaseSuggestion struct { + Author string + Flag string + TargetCommits []string +} + +func handle(ctx context.Context, log *xlog.Logger, agent linters.Agent, org, repo, author string, number int, prefilterCommits []*github.RepositoryCommit, existedComments []*github.IssueComment) error { var commitMessages []string for _, commit := range prefilterCommits { commitMessages = append(commitMessages, *commit.Commit.Message) } + var rebaseSuggestion = RebaseSuggestion{ + Author: author, + Flag: rebaseSuggestionFlag, + TargetCommits: commitMessages, + } + tmpl, err := template.New("rebase-suggestion").Parse(rebaseSuggestionTmpl) if err != nil { return err } var buf bytes.Buffer - if err = tmpl.Execute(&buf, commitMessages); err != nil { + if err = tmpl.Execute(&buf, rebaseSuggestion); err != nil { return err } diff --git a/internal/linters/git-flow/rebase-suggestion/rebase_suggestion_test.go b/internal/linters/git-flow/rebase-suggestion/rebase_suggestion_test.go index 296fb73c..e8c6dade 100644 --- a/internal/linters/git-flow/rebase-suggestion/rebase_suggestion_test.go +++ b/internal/linters/git-flow/rebase-suggestion/rebase_suggestion_test.go @@ -13,7 +13,12 @@ func TestRebaseSuggestionTmpl(t *testing.T) { } var buf bytes.Buffer - err = tmpl.Execute(&buf, []string{"commit1", "commit2"}) + var rebaseSuggestion = RebaseSuggestion{ + Author: "author", + Flag: rebaseSuggestionFlag, + TargetCommits: []string{"commit1", "commit2"}, + } + err = tmpl.Execute(&buf, rebaseSuggestion) if err != nil { t.Fatal(err) }