Skip to content

Commit

Permalink
Merge branch 'Trane9991/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Oct 24, 2020
2 parents f2d165d + c06001b commit 1b98aeb
Show file tree
Hide file tree
Showing 25 changed files with 6,170 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ runner:
- "%f:%l:%c:%m"
level: warning
staticcheck:
cmd: staticcheck $(go list ./... | grep -v /vendor/)
cmd: staticcheck $(go list ./... | grep -v /vendor/ | grep -v /bitbucket/openapi)
errorformat:
- "%f:%l:%c: %m"
misspell:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#699](https://github.com/reviewdog/reviewdog/pull/699) Support diff input format (`-f=diff`). Useful for suggested changes.
- [#700](https://github.com/reviewdog/reviewdog/pull/700) Support to show code(rule), code URL and severity in GitHub and GitLab reporters.
- Introduced [reviewdog/action-setup](https://github.com/reviewdog/action-setup) GitHub Action which installs reviewdog easily including nightly release.
- [#769](https://github.com/reviewdog/reviewdog/pull/769) Integration with [Bitbucket Code Insights](https://support.atlassian.com/bitbucket-cloud/docs/code-insights/) and [Bitbucket Pipelines](https://bitbucket.org/product/ru/features/pipelines)
- ...

### :bug: Fixes
Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ by diff.
* [Reporter: GitHub PullRequest review comment (-reporter=github-pr-review)](#reporter-github-pullrequest-review-comment--reportergithub-pr-review)
* [Reporter: GitLab MergeRequest discussions (-reporter=gitlab-mr-discussion)](#reporter-gitlab-mergerequest-discussions--reportergitlab-mr-discussion)
* [Reporter: GitLab MergeRequest commit (-reporter=gitlab-mr-commit)](#reporter-gitlab-mergerequest-commit--reportergitlab-mr-commit)
* [Reporter: Bitbucket Code Insights Reports (-reporter=bitbucket-code-report)](#reporter-bitbucket-code-insights-reports--reporterbitbucket-code-report)
- [Supported CI services](#supported-ci-services)
* [GitHub Actions](#github-actions)
* [Travis CI](#travis-ci)
* [Circle CI](#circle-ci)
* [GitLab CI](#gitlab-ci)
* [Bitbucket Pipelines](#bitbucket-pipelines)
* [Common (Jenkins, local, etc...)](#common-jenkins-local-etc)
+ [Jenkins with Github pull request builder plugin](#jenkins-with-github-pull-request-builder-plugin)
- [Exit codes](#exit-codes)
Expand Down Expand Up @@ -435,6 +437,32 @@ $ export GERRIT_ADDRESS=http://<gerrit-host>:<gerrit-port>
$ reviewdog -reporter=gerrit-change-review
```

### Reporter: Bitbucket Code Insights Reports (-reporter=bitbucket-code-report)

[![bitbucket-code-report](https://user-images.githubusercontent.com/9948629/96770123-c138d600-13e8-11eb-8e46-250b4bb393bd.png)](https://bitbucket.org/Trane9991/reviewdog-example/pull-requests/1)
[![bitbucket-code-annotations](https://user-images.githubusercontent.com/9948629/97054896-5e813f00-158e-11eb-9ad7-f8d75489b8ba.png)](https://bitbucket.org/Trane9991/reviewdog-example/pull-requests/1)

bitbucket-code-report generates the annotated
[Bitbucket Code Insights](https://support.atlassian.com/bitbucket-cloud/docs/code-insights/) report.

For now, only the `no-filter` mode supported, so the whole project is scanned on every run.
Reports are stored per commit and can be viewed per commit from Bitbucket Pipelines UI or
in Pull Request. In the Pull Request UI affected code lines will be annotated in the diff,
as well as you will be able to filter the annotations by **This pull request** or **All**.

If running from [Bitbucket Pipelines](#bitbucket-pipelines), no additional configuration is needed (even credentials).
If running locally or from some other CI system you would need to provide Bitbucket API credentials:

- For Basic Auth you need to set following env variables:
`BITBUCKET_USER` and `BITBUCKET_PASSWORD`
- For AccessToken Auth you need to set `BITBUCKET_ACCESS_TOKEN`

```shell
$ export BITBUCKET_USER="my_user"
$ export BITBUCKET_PASSWORD="my_password"
$ reviewdog -reporter=bitbucket-code-report
```

## Supported CI services

### [GitHub Actions](https://github.com/features/actions)
Expand Down Expand Up @@ -656,6 +684,24 @@ reviewdog:
- reviewdog -reporter=gitlab-mr-commit
```

### Bitbucket Pipelines

No additional configuration is needed.

#### bitbucket-pipelines.yml sample

```yaml
pipelines:
default:
- step:
name: Reviewdog
image: golangci/golangci-lint:v1.31-alpine
script:
- wget -O - -q https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh |
sh -s -- -b $(go env GOPATH)/bin
- golangci-lint run --out-format=line-number ./... | reviewdog -f=golangci-lint -reporter=bitbucket-code-report
```

### Common (Jenkins, local, etc...)

You can use reviewdog to post review comments from anywhere with following
Expand Down Expand Up @@ -747,10 +793,12 @@ so reviewdog will use [Check annotation](https://developer.github.com/v3/checks/
| **`gitlab-mr-discussion`** | OK | OK | OK | Partially Supported [2] |
| **`gitlab-mr-commit`** | OK | Partially Supported [2] | Partially Supported [2] | Partially Supported [2] |
| **`gerrit-change-review`** | OK | OK? [3] | OK? [3] | Partially Supported? [2][3] |
| **`bitbucket-code-report`** | NO [4] | NO [4] | NO [4] | OK |

- [1] Report results which is outside diff context with Check annotation as fallback if it's running in GitHub actions instead of Review API (comments). All results will be reported to console as well.
- [2] Report results which is outside diff file to console.
- [3] It should work, but not verified yet.
- [4] Not implemented at the moment

## Debugging

Expand Down
9 changes: 9 additions & 0 deletions cienv/bitbucket_pipelines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cienv

import "os"

// IsInBitbucketPipeline returns true if reviewdog is running in Bitbucket Pipelines.
func IsInBitbucketPipeline() bool {
// https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#Default-variables
return os.Getenv("BITBUCKET_PIPELINE_UUID") != ""
}
6 changes: 6 additions & 0 deletions cienv/cienv.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func GetBuildInfo() (prInfo *BuildInfo, isPR bool, err error) {
owner, repo := getOwnerAndRepoFromSlug([]string{
"TRAVIS_REPO_SLUG",
"DRONE_REPO", // drone<=0.4
"BITBUCKET_REPO_FULL_NAME",
})
if owner == "" {
owner = getOneEnvValue([]string{
Expand Down Expand Up @@ -74,6 +75,7 @@ func GetBuildInfo() (prInfo *BuildInfo, isPR bool, err error) {
"CIRCLE_SHA1",
"DRONE_COMMIT",
"CI_COMMIT_SHA", // GitLab CI
"BITBUCKET_COMMIT",
})
if sha == "" {
return nil, false, errors.New("cannot get commit SHA from environment variable. Set CI_COMMIT?")
Expand All @@ -84,6 +86,9 @@ func GetBuildInfo() (prInfo *BuildInfo, isPR bool, err error) {
"TRAVIS_PULL_REQUEST_BRANCH",
"CIRCLE_BRANCH",
"DRONE_COMMIT_BRANCH",
// present only if PR pipeline
"BITBUCKET_PR_DESTINATION_BRANCH",
"BITBUCKET_BRANCH",
})

pr := getPullRequestNum()
Expand Down Expand Up @@ -134,6 +139,7 @@ func getPullRequestNum() int {
"DRONE_PULL_REQUEST",
// GitLab CI MergeTrains
"CI_MERGE_REQUEST_IID",
"BITBUCKET_PR_ID",
}
// regexp.MustCompile() in func intentionally because this func is called
// once for one run.
Expand Down
98 changes: 92 additions & 6 deletions cmd/reviewdog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
Expand All @@ -30,6 +31,8 @@ import (
"github.com/reviewdog/reviewdog/filter"
"github.com/reviewdog/reviewdog/parser"
"github.com/reviewdog/reviewdog/project"
bbservice "github.com/reviewdog/reviewdog/service/bitbucket"
bitbucket "github.com/reviewdog/reviewdog/service/bitbucket/openapi"
gerritservice "github.com/reviewdog/reviewdog/service/gerrit"
githubservice "github.com/reviewdog/reviewdog/service/github"
"github.com/reviewdog/reviewdog/service/github/githubutils"
Expand Down Expand Up @@ -151,13 +154,26 @@ const (
$ export GERRIT_REVISION_ID=ed318bf9a3c
$ export GERRIT_BRANCH=master
$ export GERRIT_ADDRESS=http://localhost:8080
"bitbucket-code-report"
Create Bitbucket Code Report via Code Insights
(https://confluence.atlassian.com/display/BITBUCKET/Code+insights).
You can set custom report name with:
If running as part of Bitbucket Pipelines no additional configurations is needed.
If running outside of Bitbucket Pipelines you need to provide git repo data
(see documentation below for local reporters) and BitBucket credentials:
- For Basic Auth you need to set following env variables:
BITBUCKET_USER and BITBUCKET_PASSWORD
- For AccessToken Auth you need to set BITBUCKET_ACCESS_TOKEN
Running on Bitbucket Server is not tested/supported yet.
For GitHub Enterprise and self hosted GitLab, set
REVIEWDOG_INSECURE_SKIP_VERIFY to skip verifying SSL (please use this at your own risk)
$ export REVIEWDOG_INSECURE_SKIP_VERIFY=true
For non-local reporters, reviewdog automatically get necessary data from
environment variable in CI service (GitHub Actions, Travis CI, Circle CI, drone.io, GitLab CI).
environment variable in CI service (GitHub Actions, Travis CI, Circle CI, drone.io, GitLab CI, Bitbucket Pipelines).
You can set necessary data with following environment variable manually if
you want (e.g. run reviewdog in Jenkins).
Expand Down Expand Up @@ -226,11 +242,18 @@ func run(r io.Reader, w io.Writer, opt *option) error {

// assume it's project based run when both -efm and -f are not specified
isProject := len(opt.efms) == 0 && opt.f == ""
var projectConf *project.Config

var cs reviewdog.CommentService
var ds reviewdog.DiffService

if isProject {
var err error
projectConf, err = projectConfig(opt.conf)
if err != nil {
return err
}

cs = reviewdog.NewUnifiedCommentWriter(w)
} else {
cs = reviewdog.NewRawCommentWriter(w)
Expand Down Expand Up @@ -324,6 +347,28 @@ github-pr-check reporter as a fallback.
return err
}
ds = d
case "bitbucket-code-report":
build, client, ct, err := bitbucketBuildWithClient(ctx)
if err != nil {
return err
}
ctx = ct

cs = bbservice.NewReportAnnotator(client,
build.Owner, build.Repo, build.SHA, getRunnersList(opt, projectConf))

if !(opt.filterMode == filter.ModeDefault || opt.filterMode == filter.ModeNoFilter) {
// by default scan whole project with out diff (filter.ModeNoFilter)
// Bitbucket pipelines doesn't give an easy way to know
// which commit run pipeline before so we can compare between them
// however once PR is opened, Bitbucket Reports UI will do automatic
// filtering of annotations dividing them in two groups:
// - This pull request (10)
// - All (50)
log.Printf("reviewdog: [bitbucket-code-report] supports only with filter.ModeNoFilter for now")
}
opt.filterMode = filter.ModeNoFilter
ds = &reviewdog.EmptyDiff{}
case "local":
if opt.diffCmd == "" && opt.filterMode == filter.ModeNoFilter {
ds = &reviewdog.EmptyDiff{}
Expand All @@ -337,11 +382,7 @@ github-pr-check reporter as a fallback.
}

if isProject {
conf, err := projectConfig(opt.conf)
if err != nil {
return err
}
return project.Run(ctx, conf, buildRunnersMap(opt.runners), cs, ds, opt.tee, opt.filterMode, opt.failOnError)
return project.Run(ctx, projectConf, buildRunnersMap(opt.runners), cs, ds, opt.tee, opt.filterMode, opt.failOnError)
}

p, err := newParserFromOpt(opt)
Expand Down Expand Up @@ -556,6 +597,28 @@ func gerritBuildWithClient() (*cienv.BuildInfo, *gerrit.Client, error) {
return buildInfo, client, nil
}

func bitbucketBuildWithClient(ctx context.Context) (*cienv.BuildInfo, *bitbucket.APIClient, context.Context, error) {
build, _, err := cienv.GetBuildInfo()
if err != nil {
return nil, nil, ctx, err
}

bbUser := os.Getenv("BITBUCKET_USER")
bbPass := os.Getenv("BITBUCKET_PASSWORD")
bbAccessToken := os.Getenv("BITBUCKET_ACCESS_TOKEN")

if bbUser != "" && bbPass != "" {
ctx = bbservice.WithBasicAuth(ctx, bbUser, bbPass)
}

if bbAccessToken != "" {
ctx = bbservice.WithAccessToken(ctx, bbAccessToken)
}

client := bbservice.NewAPIClient(cienv.IsInBitbucketPipeline())
return build, client, ctx, nil
}

func fetchMergeRequestIDFromCommit(cli *gitlab.Client, projectID, sha string) (id int, err error) {
// https://docs.gitlab.com/ce/api/merge_requests.html#list-project-merge-requests
opt := &gitlab.ListProjectMergeRequestsOptions{
Expand Down Expand Up @@ -689,3 +752,26 @@ func buildRunnersMap(runners string) map[string]bool {
}
return m
}

func getRunnersList(opt *option, conf *project.Config) []string {
if len(opt.runners) > 0 { // if runners explicitly defined, use them
return strings.Split(opt.runners, ",")
}

if conf != nil { // if this is a Project run, and no explicitly provided runners
// if no runners explicitly provided
// get all runners from config
list := make([]string, 0, len(conf.Runner))
for runner := range conf.Runner {
list = append(list, runner)
}
return list
}

// if this is simple run, get the single tool name
if name := toolName(opt); name != "" {
return []string{name}
}

return []string{}
}

0 comments on commit 1b98aeb

Please sign in to comment.