Skip to content

Commit

Permalink
Merge pull request #12 from CarlJi/rebase
Browse files Browse the repository at this point in the history
feat: add Rebase suggestion lint
  • Loading branch information
CarlJi authored Jan 10, 2024
2 parents 28c981b + c123325 commit 31371e6
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 170 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Please note that cr-bot is still in its early stages, and we have plans to integ

Lastly, we welcome any contributions to the project! Your bug reports, feature suggestions, and active participation in the development process are greatly appreciated. Together, we can make cr-bot even better.

![Alt text](docs/static/image.png)
![Alt text](docs/static/image.png)
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewConfig(conf string) (Config, error) {
return c, nil
}

func (c Config) Linters(org, repo string) map[string]Linter {
func (c Config) CustomLinterConfigs(org, repo string) map[string]Linter {
if repoConfig, ok := c[org+"/"+repo]; ok {
return repoConfig
}
Expand Down
19 changes: 12 additions & 7 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
qbox: # github organization name
staticcheck: # linters
enable: true
command: staticcheck # linter command, if not set, will use the linter name registered in init stage
args: ["./..."]
run_if_changed: [".go$"] # only run if changed files match the regex

# This is the default config file, you can override it by creating a config.yaml in the same directory
# by default, all linters are enabled if you don't specify. You can disable them by setting enable to false
# example1: disable staticcheck for org
# qbox:
# staticcheck:
# enable: false
#
# example2: disable staticcheck for repo
# qbox/kodo:
# staticcheck:
# enable: false
#
qbox/kodo: # github repository name, which will override the settings in qbox org
staticcheck: # repo specific settings will override the settings in qbox org
enable: true
Expand Down
31 changes: 16 additions & 15 deletions pulls.go → github.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
Copyright 2023 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.
See the License for the specific language governing permissions and
limitations under the License.
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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
Expand All @@ -22,7 +23,7 @@ import (
"strings"
"time"

"github.com/cr-bot/linters"
"github.com/cr-bot/internal/linters"
"github.com/google/go-github/v57/github"
"github.com/qiniu/x/log"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ func (s *Server) ListPullRequestsFiles(ctx context.Context, owner string, repo s
}
}

func (s *Server) PostCommentsWithRetry(ctx context.Context, owner string, repo string, number int, comments []*github.PullRequestComment) error {
func (s *Server) PostPullReviewCommentsWithRetry(ctx context.Context, owner string, repo string, number int, comments []*github.PullRequestComment) error {
var existedComments []*github.PullRequestComment
err := retryWithBackoff(ctx, func() error {
originalComments, resp, err := s.gc.PullRequests.ListComments(ctx, owner, repo, number, nil)
Expand Down
27 changes: 14 additions & 13 deletions hunk.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
Copyright 2023 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.
See the License for the specific language governing permissions and
limitations under the License.
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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
Expand Down
27 changes: 14 additions & 13 deletions hunk_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
Copyright 2023 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.
See the License for the specific language governing permissions and
limitations under the License.
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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
Expand Down
83 changes: 83 additions & 0 deletions internal/linters/git-flow/rebase-suggestion/rebase-suggestion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rebase_suggestion

import (
"context"
"fmt"
"regexp"

"github.com/cr-bot/config"
"github.com/cr-bot/internal/linters"
"github.com/google/go-github/v57/github"
"github.com/qiniu/x/log"
)

var lintName = "rebase-suggestion"

func init() {
// register linter
linters.RegisterCommentHandler(lintName, rebaseSuggestionHandler)
}

func rebaseSuggestionHandler(linterConfig config.Linter, agent linters.Agent, event github.PullRequestEvent) error {
opts := &github.ListOptions{}
commits, response, err := agent.GitHubClient().PullRequests.ListCommits(context.Background(), event.GetRepo().GetOwner().GetLogin(), event.GetRepo().GetName(), event.GetNumber(), opts)
if err != nil {
return err
}

if response.StatusCode != 200 {
log.Errorf("list commits failed: %v", response)
return fmt.Errorf("list commits failed: %v", response.Body)
}

comment := checkCommitMessage(commits)
if len(comment) == 0 {
return nil
}
c, resp, err := agent.GitHubClient().Issues.CreateComment(context.Background(), event.GetRepo().GetOwner().GetLogin(), event.GetRepo().GetName(), event.GetNumber(), &github.IssueComment{
Body: &comment,
})
if err != nil {
return err
}

if resp.StatusCode != 201 {
log.Errorf("create comment failed: %v", resp)
return fmt.Errorf("create comment failed: %v", resp.Body)
}

log.Infof("create comment success: %v", c)

return nil
}

func checkCommitMessage(commits []*github.RepositoryCommit) string {
pattern := `^Merge (.*) into (.*)$`
reg := regexp.MustCompile(pattern)

for _, commit := range commits {
if commit.Commit != nil && commit.Commit.Message != nil {
if reg.MatchString(*commit.Commit.Message) {
return "please rebase your PR"
}
}
}

return ""
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
Copyright 2023 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.
See the License for the specific language governing permissions and
limitations under the License.
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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package staticcheck

import (
Expand All @@ -23,15 +24,18 @@ import (
"strings"

"github.com/cr-bot/config"
"github.com/cr-bot/linters"
"github.com/cr-bot/internal/linters"
"github.com/google/go-github/v57/github"
"github.com/qiniu/x/log"
)

var lintName = "staticcheck"

func init() {
linters.RegisterLinter("staticcheck", staticcheckHandler)
linters.RegisterCodeReviewHandler(lintName, staticcheckHandler)
}

func staticcheckHandler(linterConfig config.Linter) (map[string][]linters.LinterOutput, error) {
func staticcheckHandler(linterConfig config.Linter, agent linters.Agent, event github.PullRequestEvent) (map[string][]linters.LinterOutput, error) {
executor, err := NewStaticcheckExecutor(linterConfig.WorkDir)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
/*
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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package staticcheck

import (
"testing"

"github.com/cr-bot/linters"
"github.com/cr-bot/internal/linters"
)

func TestFormatStaticcheckLine(t *testing.T) {
Expand Down
Loading

0 comments on commit 31371e6

Please sign in to comment.