1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/githubCheckBranchProtection.go
Oliver Nocon f1cfca2e76
Add step for GitHub branch protection check (#2010)
* add step for GitHub branch protection check

* add command to piper command

* remove unnecessary parameter

* Update resources/metadata/githubbranchprotection.yaml

* add groovy part

* update generation & go mod tidy

* update groovy tests

* fix bug with go-github version

Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com>
2020-09-11 15:28:43 +02:00

65 lines
2.3 KiB
Go

package cmd
import (
"context"
"fmt"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/google/go-github/v32/github"
"github.com/pkg/errors"
piperGithub "github.com/SAP/jenkins-library/pkg/github"
)
type githubRepositoriesService interface {
GetBranchProtection(ctx context.Context, owner, repo, branch string) (*github.Protection, *github.Response, error)
}
func githubCheckBranchProtection(config githubCheckBranchProtectionOptions, telemetryData *telemetry.CustomData) {
ctx, client, err := piperGithub.NewClient(config.Token, config.APIURL, "")
if err != nil {
log.Entry().WithError(err).Fatal("Failed to get GitHub client")
}
err = runGithubCheckBranchProtection(ctx, &config, telemetryData, client.Repositories)
if err != nil {
log.Entry().WithError(err).Fatal("GitHub branch protection check failed")
}
}
func runGithubCheckBranchProtection(ctx context.Context, config *githubCheckBranchProtectionOptions, telemetryData *telemetry.CustomData, ghRepositoriesService githubRepositoriesService) error {
ghProtection, _, err := ghRepositoriesService.GetBranchProtection(ctx, config.Owner, config.Repository, config.Branch)
if err != nil {
return errors.Wrap(err, "failed to read branch protection information")
}
// validate required status checks
for _, check := range config.RequiredChecks {
var found bool
for _, context := range ghProtection.GetRequiredStatusChecks().Contexts {
if check == context {
found = true
}
}
if !found {
return fmt.Errorf("required status check '%v' not found in branch protection configuration", check)
}
}
// validate that admins are enforced in checks
if config.RequireEnforceAdmins {
if !ghProtection.GetEnforceAdmins().Enabled {
return fmt.Errorf("admins are not enforced in branch protection configuration")
}
}
// validate number of mandatory reviewers
if config.RequiredApprovingReviewCount > 0 && ghProtection.GetRequiredPullRequestReviews().RequiredApprovingReviewCount < config.RequiredApprovingReviewCount {
return fmt.Errorf("not enough mandatory reviewers in branch protection configuration, expected at least %v, got %v", config.RequiredApprovingReviewCount, ghProtection.GetRequiredPullRequestReviews().RequiredApprovingReviewCount)
}
return nil
}