Skip to content

Commit

Permalink
Merge pull request #190 from mgrachev/feature/get-pull-request-id
Browse files Browse the repository at this point in the history
Get Pull Request ID by branch name or commit hash
  • Loading branch information
haya14busa committed Sep 14, 2019
2 parents ec762c1 + 3f5ca42 commit 4361d2a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 27 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -457,6 +457,13 @@ $ REVIEWDOG_GITHUB_API_TOKEN="<token>"
$ REVIEWDOG_GITLAB_API_TOKEN="<token>"
```

If a CI service doesn't provide information such as Pull Request ID - reviewdog can guess it by branch name and commit SHA.
Just pass the flag `guess`:

```shell
$ reviewdog -conf=.reviewdog.yml -reporter=github-pr-check -guess
```

#### Jenkins with Github pull request builder plugin
- [GitHub pull request builder plugin - Jenkins - Jenkins Wiki](https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin)

Expand Down
102 changes: 75 additions & 27 deletions cmd/reviewdog/main.go
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"os/exec"
"sort"
"strings"
"text/tabwriter"

"golang.org/x/net/context" // "context"
Expand All @@ -38,29 +39,31 @@ const usageMessage = "" +
`

type option struct {
version bool
diffCmd string
diffStrip int
efms strslice
f string // errorformat name
list bool // list supported errorformat name
name string // tool name which is used in comment
ci string
conf string
reporter string
version bool
diffCmd string
diffStrip int
efms strslice
f string // errorformat name
list bool // list supported errorformat name
name string // tool name which is used in comment
ci string
conf string
reporter string
guessPullRequest bool
}

// flags doc
const (
diffCmdDoc = `diff command (e.g. "git diff"). diff flag is ignored if you pass "ci" flag`
diffStripDoc = "strip NUM leading components from diff file names (equivalent to 'patch -p') (default is 1 for git diff)"
efmsDoc = `list of errorformat (https://github.com/reviewdog/errorformat)`
fDoc = `format name (run -list to see supported format name) for input. It's also used as tool name in review comment if -name is empty`
listDoc = `list supported pre-defined format names which can be used as -f arg`
nameDoc = `tool name in review comment. -f is used as tool name if -name is empty`
ciDoc = `[deprecated] reviewdog automatically get necessary data. See also -reporter for migration`
confDoc = `config file path`
reporterDoc = `reporter of reviewdog results. (local, github-pr-check, github-pr-review, gitlab-mr-discussion, gitlab-mr-commit)
diffCmdDoc = `diff command (e.g. "git diff"). diff flag is ignored if you pass "ci" flag`
diffStripDoc = "strip NUM leading components from diff file names (equivalent to 'patch -p') (default is 1 for git diff)"
efmsDoc = `list of errorformat (https://github.com/reviewdog/errorformat)`
fDoc = `format name (run -list to see supported format name) for input. It's also used as tool name in review comment if -name is empty`
listDoc = `list supported pre-defined format names which can be used as -f arg`
nameDoc = `tool name in review comment. -f is used as tool name if -name is empty`
ciDoc = `[deprecated] reviewdog automatically get necessary data. See also -reporter for migration`
confDoc = `config file path`
guessPullRequestDoc = `guess Pull Request ID by branch name and commit SHA`
reporterDoc = `reporter of reviewdog results. (local, github-pr-check, github-pr-review, gitlab-mr-discussion, gitlab-mr-commit)
"local" (default)
Report results to stdout.
Expand Down Expand Up @@ -135,6 +138,7 @@ func init() {
flag.StringVar(&opt.ci, "ci", "", ciDoc)
flag.StringVar(&opt.conf, "conf", "", confDoc)
flag.StringVar(&opt.reporter, "reporter", "local", reporterDoc)
flag.BoolVar(&opt.guessPullRequest, "guess", false, guessPullRequestDoc)
}

func usage() {
Expand Down Expand Up @@ -195,7 +199,7 @@ See -reporter flag for migration and set -reporter="github-pr-review" or -report
fmt.Fprintln(os.Stderr, "REVIEWDOG_GITHUB_API_TOKEN is not set")
return nil
}
gs, isPR, err := githubService(ctx)
gs, isPR, err := githubService(ctx, opt)
if err != nil {
return err
}
Expand Down Expand Up @@ -319,7 +323,7 @@ func insecureSkipVerify() bool {
return os.Getenv("REVIEWDOG_INSECURE_SKIP_VERIFY") == "true"
}

func githubService(ctx context.Context) (gs *githubservice.GitHubPullRequest, isPR bool, err error) {
func githubService(ctx context.Context, opt *option) (gs *githubservice.GitHubPullRequest, isPR bool, err error) {
token, err := nonEmptyEnv("REVIEWDOG_GITHUB_API_TOKEN")
if err != nil {
return nil, isPR, err
Expand All @@ -328,21 +332,65 @@ func githubService(ctx context.Context) (gs *githubservice.GitHubPullRequest, is
if err != nil {
return nil, isPR, err
}
// TODO: support commit build
if !isPR {
return nil, isPR, nil
}

client, err := githubClient(ctx, token)
if err != nil {
return nil, isPR, err
}

if !isPR {
if !opt.guessPullRequest {
return nil, false, nil
}

if g.Branch == "" && g.SHA == "" {
return nil, false, nil
}

prID, err := getPullRequestIDByBranchOrCommit(ctx, client, g)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil, false, nil
}
g.PullRequest = prID
}

gs, err = githubservice.NewGitHubPullRequest(client, g.Owner, g.Repo, g.PullRequest, g.SHA)
if err != nil {
return nil, isPR, err
return nil, false, err
}
return gs, true, nil
}

func getPullRequestIDByBranchOrCommit(ctx context.Context, client *github.Client, info *cienv.BuildInfo) (int, error) {
options := &github.SearchOptions{
Sort: "updated",
Order: "desc",
}

query := []string{
"type:pr",
"state:open",
fmt.Sprintf("repo:%s/%s", info.Owner, info.Repo),
}
return gs, isPR, nil
if info.Branch != "" {
query = append(query, fmt.Sprintf("head:%s", info.Branch))
}
if info.SHA != "" {
query = append(query, info.SHA)
}

preparedQuery := strings.Join(query, " ")
pullRequests, _, err := client.Search.Issues(ctx, preparedQuery, options)
if err != nil {
return 0, err
}

if *pullRequests.Total == 0 {
return 0, fmt.Errorf("reviewdog: PullRequest not found, query: %s", preparedQuery)
}

return *pullRequests.Issues[0].Number, nil
}

func githubClient(ctx context.Context, token string) (*github.Client, error) {
Expand Down

0 comments on commit 4361d2a

Please sign in to comment.