2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2020-09-14 12:05:12 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
|
|
|
2022-08-02 08:26:26 +02:00
|
|
|
"github.com/google/go-github/v45/github"
|
2020-09-14 12:05:12 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-09-14 18:08:24 +02:00
|
|
|
type ghCheckBranchRepoService struct {
|
2020-09-14 12:05:12 +02:00
|
|
|
protection github.Protection
|
|
|
|
serviceError error
|
|
|
|
owner string
|
|
|
|
repo string
|
|
|
|
branch string
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:08:24 +02:00
|
|
|
func (g *ghCheckBranchRepoService) GetBranchProtection(ctx context.Context, owner, repo, branch string) (*github.Protection, *github.Response, error) {
|
2020-09-14 12:05:12 +02:00
|
|
|
g.owner = owner
|
|
|
|
g.repo = repo
|
|
|
|
g.branch = branch
|
|
|
|
|
|
|
|
return &g.protection, nil, g.serviceError
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunGithubCheckBranchProtection(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
|
|
|
t.Run("no checks active", func(t *testing.T) {
|
|
|
|
config := githubCheckBranchProtectionOptions{Branch: "testBranch", Owner: "testOrg", Repository: "testRepo"}
|
2020-09-14 18:08:24 +02:00
|
|
|
ghRepo := ghCheckBranchRepoService{}
|
2020-09-14 12:05:12 +02:00
|
|
|
err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, config.Branch, ghRepo.branch)
|
|
|
|
assert.Equal(t, config.Owner, ghRepo.owner)
|
|
|
|
assert.Equal(t, config.Repository, ghRepo.repo)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error calling GitHub", func(t *testing.T) {
|
|
|
|
config := githubCheckBranchProtectionOptions{Branch: "testBranch", Owner: "testOrg", Repository: "testRepo"}
|
2020-09-14 18:08:24 +02:00
|
|
|
ghRepo := ghCheckBranchRepoService{serviceError: fmt.Errorf("gh test error")}
|
2020-09-14 12:05:12 +02:00
|
|
|
err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo)
|
|
|
|
assert.EqualError(t, err, "failed to read branch protection information: gh test error")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("all checks ok", func(t *testing.T) {
|
|
|
|
config := githubCheckBranchProtectionOptions{
|
|
|
|
Branch: "testBranch",
|
|
|
|
Owner: "testOrg",
|
|
|
|
Repository: "testRepo",
|
|
|
|
RequiredChecks: []string{"check1", "check2"},
|
|
|
|
RequireEnforceAdmins: true,
|
|
|
|
RequiredApprovingReviewCount: 1,
|
|
|
|
}
|
2020-09-14 18:08:24 +02:00
|
|
|
ghRepo := ghCheckBranchRepoService{protection: github.Protection{
|
2020-09-14 12:05:12 +02:00
|
|
|
RequiredStatusChecks: &github.RequiredStatusChecks{Contexts: []string{"check0", "check1", "check2", "check3"}},
|
|
|
|
EnforceAdmins: &github.AdminEnforcement{Enabled: true},
|
|
|
|
RequiredPullRequestReviews: &github.PullRequestReviewsEnforcement{RequiredApprovingReviewCount: 1},
|
|
|
|
}}
|
|
|
|
err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, config.Branch, ghRepo.branch)
|
|
|
|
assert.Equal(t, config.Owner, ghRepo.owner)
|
|
|
|
assert.Equal(t, config.Repository, ghRepo.repo)
|
|
|
|
})
|
|
|
|
|
2020-09-15 17:50:55 +02:00
|
|
|
t.Run("no status checks", func(t *testing.T) {
|
|
|
|
config := githubCheckBranchProtectionOptions{
|
|
|
|
RequiredChecks: []string{"check1", "check2"},
|
|
|
|
}
|
|
|
|
ghRepo := ghCheckBranchRepoService{protection: github.Protection{}}
|
|
|
|
err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo)
|
|
|
|
assert.Contains(t, fmt.Sprint(err), "required status check 'check1' not found")
|
|
|
|
})
|
|
|
|
|
2020-09-14 12:05:12 +02:00
|
|
|
t.Run("status check missing", func(t *testing.T) {
|
|
|
|
config := githubCheckBranchProtectionOptions{
|
|
|
|
RequiredChecks: []string{"check1", "check2"},
|
|
|
|
}
|
2020-09-14 18:08:24 +02:00
|
|
|
ghRepo := ghCheckBranchRepoService{protection: github.Protection{
|
2020-09-14 12:05:12 +02:00
|
|
|
RequiredStatusChecks: &github.RequiredStatusChecks{Contexts: []string{"check0", "check1"}},
|
|
|
|
}}
|
|
|
|
err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo)
|
|
|
|
assert.Contains(t, fmt.Sprint(err), "required status check 'check2' not found")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("admin enforcement inactive", func(t *testing.T) {
|
|
|
|
config := githubCheckBranchProtectionOptions{
|
|
|
|
RequireEnforceAdmins: true,
|
|
|
|
}
|
2020-09-14 18:08:24 +02:00
|
|
|
ghRepo := ghCheckBranchRepoService{protection: github.Protection{
|
2020-09-14 12:05:12 +02:00
|
|
|
EnforceAdmins: &github.AdminEnforcement{Enabled: false},
|
|
|
|
}}
|
|
|
|
err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo)
|
|
|
|
assert.Contains(t, fmt.Sprint(err), "admins are not enforced")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not enough reviewers", func(t *testing.T) {
|
|
|
|
config := githubCheckBranchProtectionOptions{
|
|
|
|
RequiredApprovingReviewCount: 2,
|
|
|
|
}
|
2020-09-14 18:08:24 +02:00
|
|
|
ghRepo := ghCheckBranchRepoService{protection: github.Protection{
|
2020-09-14 12:05:12 +02:00
|
|
|
RequiredPullRequestReviews: &github.PullRequestReviewsEnforcement{RequiredApprovingReviewCount: 1},
|
|
|
|
}}
|
|
|
|
err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo)
|
|
|
|
assert.Contains(t, fmt.Sprint(err), "not enough mandatory reviewers")
|
|
|
|
})
|
|
|
|
}
|