Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rebase-suggestion): provide author information to increase visibility for comments #41

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.GetPullRequest().GetUser().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
Loading