Skip to content

Commit

Permalink
Merge pull request #653 from reviewdog/fork-detect
Browse files Browse the repository at this point in the history
Fix detection logic to determin Pull Requests from forked repo
  • Loading branch information
haya14busa committed Jun 29, 2020
2 parents ce75a5e + af9288c commit b6d7542
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### :book: Release Note <!-- optional -->

### :rocket: Enhancements
- [#563](https://github.com/reviewdog/reviewdog/issues/563) Use `CI_API_V4_URL` environment variable when present.
- [#563](https://github.com/reviewdog/reviewdog/issues/563) Use `CI_API_V4_URL` environment variable when present.
- ...

### :bug: Fixes
- [#609](https://github.com/reviewdog/reviewdog/issues/609) reviewdog command will fail with unexpected tool's error for github-check/github-pr-check reporters as well. ([@haya14busa])
- [#603](https://github.com/reviewdog/reviewdog/issues/603) Fixed detection of Pull Requests from forked repo. ([@haya14busa])
- ...

### :rotating_light: Breaking changes
Expand Down
27 changes: 22 additions & 5 deletions cienv/github_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ type GitHubEvent struct {
} `json:"head_commit"`
}

type GitHubRepo struct {
Owner struct {
ID int64 `json:"id"`
}
}

type GitHubPullRequest struct {
Number int `json:"number"`
Head struct {
Sha string `json:"sha"`
Ref string `json:"ref"`
Repo struct {
Fork bool `json:"fork"`
} `json:"repo"`
Sha string `json:"sha"`
Ref string `json:"ref"`
Repo GitHubRepo `json:"repo"`
} `json:"head"`
Base struct {
Repo GitHubRepo `json:"repo"`
} `json:"base"`
}

// LoadGitHubEvent loads GitHubEvent if it's running in GitHub Actions.
Expand Down Expand Up @@ -94,3 +101,13 @@ func IsInGitHubAction() bool {
// https://help.github.com/en/articles/virtual-environments-for-github-actions#default-environment-variables
return os.Getenv("GITHUB_ACTION") != ""
}

// IsGitHubPRFromForkedRepo returns true if reviewdog is running in GitHub
// Actions and running for PullRequests from forked repository.
func IsGitHubPRFromForkedRepo() bool {
event, err := LoadGitHubEvent()
if err != nil {
return false
}
return event.PullRequest.Head.Repo.Owner.ID != event.PullRequest.Base.Repo.Owner.ID
}
11 changes: 1 addition & 10 deletions cmd/reviewdog/doghouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,10 @@ func checkResultToAnnotation(c *reviewdog.CheckResult, wd, gitRelWd string) *dog
// It returns true if reviewdog should exit with 1.
// e.g. At least one annotation result is in diff.
func reportResults(w io.Writer, filteredResultSet *reviewdog.FilteredResultMap) bool {
if filteredResultSet.Len() != 0 && isPRFromForkedRepo() {
if filteredResultSet.Len() != 0 && cienv.IsGitHubPRFromForkedRepo() {
fmt.Fprintln(w, `reviewdog: This is Pull-Request from forked repository.
GitHub token doesn't have write permission of Check API, so reviewdog will
report results via logging command [1].
[1]: https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-for-github-actions#logging-commands`)
}

Expand Down Expand Up @@ -252,11 +251,3 @@ report results via logging command [1].
}
return shouldFail
}

func isPRFromForkedRepo() bool {
event, err := cienv.LoadGitHubEvent()
if err != nil {
return false
}
return event.PullRequest.Head.Repo.Fork
}
3 changes: 1 addition & 2 deletions cmd/reviewdog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,11 @@ See -reporter flag for migration and set -reporter="github-pr-review" or -report
// instead of review comment because if it's PR from forked repository,
// GitHub token doesn't have write permission due to security concern and
// cannot post results via Review API.
if cienv.IsInGitHubAction() && isPRFromForkedRepo() {
if cienv.IsInGitHubAction() && cienv.IsGitHubPRFromForkedRepo() {
fmt.Fprintln(w, `reviewdog: This is Pull-Request from forked repository.
GitHub token doesn't have write permission of Review API, so reviewdog will
report results via logging command [1] and create annotations similar to
github-pr-check reporter as a fallback.
[1]: https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-for-github-actions#logging-commands`)
cs = githubutils.NewGitHubActionLogWriter(opt.level)
} else {
Expand Down

0 comments on commit b6d7542

Please sign in to comment.