Skip to content

Commit

Permalink
feat(rebase-suggestion): provide author information to increase visib…
Browse files Browse the repository at this point in the history
…ility
  • Loading branch information
CarlJi committed Jan 26, 2024
1 parent ca7fe30 commit e95cf3c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
6 changes: 3 additions & 3 deletions hunk.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 3 additions & 3 deletions hunk_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
27 changes: 20 additions & 7 deletions internal/linters/git-flow/rebase-suggestion/rebase_suggestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 <upstream> <branch>`" + `to reorganize your PR.
Which seems insignificant, recommend to use` + ` **`+"`"+`git rebase <upstream>/<branch>`+"`"+`** `+ `to reorganize your PR.
<details>
Expand All @@ -75,19 +76,31 @@ If you have any questions about this comment, feel free to raise an issue here:
</details>
`

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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit e95cf3c

Please sign in to comment.