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

Added SkipDocsOnly config property #13

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added the commit hash to the details to make it clearer its not related to the PR as a whole
* Added support for the `do-not-merge/hold` label to block merging.
* Added `mc-bootstrap` required checks
* `SkipDocsOnly` repo config boolean for skipping CI when only Markdown files have changed.

### Changed

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A PR check run that ensures requirements are met before allowing PRs to be merge
- Allow skipping these checks by placing a `skip/ci` label on the PR
- Allow blocking PR merge by adding a `do-not-merge/hold` label on the PR
- Force a recheck by adding a comment to a PR containing the trigger `/recheck`
- Configure PR Checks as not required when a PR only contains markdown changes by setting the `skipDocsOnly` config property

## How it works

Expand Down
18 changes: 18 additions & 0 deletions cmd/gatekeeper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ func main() {
}
}

if repoConfig.SkipDocsOnly {
fmt.Println("`SkipDocsOnly` is set to true, checking for changed file types")
files, err := gh.GetFiles()
if err != nil {
fmt.Println("Failed to get PRs files")
panic(err)
}
for _, file := range files {
fileName := strings.ToLower(file.GetFilename())
if !strings.HasSuffix(fileName, ".md") {
fmt.Printf("Non-markdown file found ('%s'), not skipping\n", fileName)
break
}
}
result.SkipCI = true
result.AddMessage("ℹ️ Pull Requests only contains Markdown changes and `skipDocsOnly` is set - **ignoring other requirements**")
}

// Check labels on the PR for overriding behaviour
for _, label := range pullRequest.Labels {
switch strings.ToLower(*label.Name) {
Expand Down
1 change: 1 addition & 0 deletions internal/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Repos map[string]Repo

type Repo struct {
RequiredChecks []string `json:"requiredChecks"`
SkipDocsOnly bool `json:"skipDocsOnly"`
}

func LoadConfig() (*Conf, error) {
Expand Down
27 changes: 27 additions & 0 deletions internal/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,30 @@ func (c *Client) GetCheck(checkName string) (*github.CheckRun, error) {
}
return checks.CheckRuns[0], nil
}

func (c *Client) GetFiles() ([]*github.CommitFile, error) {
prNumber, err := strconv.Atoi(c.PR)
if err != nil {
return nil, err
}

opts := &github.ListOptions{
PerPage: 100,
}

var allFiles []*github.CommitFile
for {
files, resp, err := c.PullRequests.ListFiles(c.Ctx, owner, c.Repo, prNumber, opts)
if err != nil {
return nil, err
}
allFiles = append(allFiles, files...)

if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}

return allFiles, nil
}