From 79a8e981896dace95b91719f6665f39df88b007b Mon Sep 17 00:00:00 2001 From: Nicolas Zin Date: Fri, 21 Feb 2025 18:22:32 -0500 Subject: [PATCH] branch protections fixes --- CHANGELOG.md | 8 ++++++++ internal/engine/goliac_reconciliator.go | 23 ++++++++++++++--------- internal/engine/remote.go | 23 +++++++++++++---------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6846e0a..9f6e887 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## Goliac v0.15.1 + +- branch protections bugfixes + +## Goliac v0.15.0 + +- introducing branch protections + ## Goliac v0.14.1 - different security updates (golang/npm) diff --git a/internal/engine/goliac_reconciliator.go b/internal/engine/goliac_reconciliator.go index 4020c92..79f6b5b 100644 --- a/internal/engine/goliac_reconciliator.go +++ b/internal/engine/goliac_reconciliator.go @@ -524,7 +524,7 @@ func (r *GoliacReconciliatorImpl) reconciliateRepositories(ctx context.Context, branchprotections := make(map[string]*GithubBranchProtection) for _, bp := range lRepo.Spec.BranchProtections { branchprotection := GithubBranchProtection{ - DatabaseId: 0, + Id: "", Pattern: bp.Pattern, RequiresApprovingReviews: bp.RequiresApprovingReviews, RequiredApprovingReviewCount: bp.RequiredApprovingReviewCount, @@ -595,7 +595,7 @@ func (r *GoliacReconciliatorImpl) reconciliateRepositories(ctx context.Context, } onBranchProtectionChange := func(rulename string, lBp *GithubBranchProtection, rBp *GithubBranchProtection) { // UPDATE branchprotection - lBp.DatabaseId = rBp.DatabaseId + lBp.Id = rBp.Id r.UpdateRepositoryBranchProtection(ctx, dryrun, reponame, lBp) } CompareEntities(lRepo.BranchProtections, rRepo.BranchProtections, compareBranchProtections, onBranchProtectionAdded, onBranchProtectionRemoved, onBranchProtectionChange) @@ -749,8 +749,10 @@ func compareBranchProtections(bpname string, lbp *GithubBranchProtection, rbp *G if lbp.RequiresApprovingReviews != rbp.RequiresApprovingReviews { return false } - if lbp.RequiredApprovingReviewCount != rbp.RequiredApprovingReviewCount { - return false + if lbp.RequiresApprovingReviews { + if lbp.RequiredApprovingReviewCount != rbp.RequiredApprovingReviewCount { + return false + } } if lbp.DismissesStaleReviews != rbp.DismissesStaleReviews { return false @@ -764,11 +766,14 @@ func compareBranchProtections(bpname string, lbp *GithubBranchProtection, rbp *G if lbp.RequiresStatusChecks != rbp.RequiresStatusChecks { return false } - if lbp.RequiresStrictStatusChecks != rbp.RequiresStrictStatusChecks { - return false - } - if res, _, _ := entity.StringArrayEquivalent(lbp.RequiredStatusCheckContexts, rbp.RequiredStatusCheckContexts); !res { - return false + if lbp.RequiresStatusChecks { + if lbp.RequiresStrictStatusChecks != rbp.RequiresStrictStatusChecks { + return false + } + if res, _, _ := entity.StringArrayEquivalent(lbp.RequiredStatusCheckContexts, rbp.RequiredStatusCheckContexts); !res { + fmt.Println("false") + return false + } } if lbp.RequiresConversationResolution != rbp.RequiresConversationResolution { return false diff --git a/internal/engine/remote.go b/internal/engine/remote.go index 84a2f79..5b77627 100644 --- a/internal/engine/remote.go +++ b/internal/engine/remote.go @@ -553,7 +553,7 @@ query listAllReposInOrg($orgLogin: String!, $endCursor: String) { } branchProtectionRules(first:50) { nodes{ - databaseId + id pattern requiresApprovingReviews requiredApprovingReviewCount @@ -582,7 +582,7 @@ query listAllReposInOrg($orgLogin: String!, $endCursor: String) { ` type GithubBranchProtection struct { - DatabaseId int + Id string Pattern string RequiresApprovingReviews bool RequiredApprovingReviewCount int @@ -1837,7 +1837,7 @@ mutation createBranchProtectionRule( repositoryId: $repositoryId, pattern: $pattern, requiresApprovingReviews: $requiresApprovingReviews, - requiredApprovingReviewCount: $requriedApprovingReviewCount, + requiredApprovingReviewCount: $requiredApprovingReviewCount, dismissesStaleReviews: $dismissesStaleReviews, requiresCodeOwnerReviews: $requiresCodeOwnerReviews, requireLastPushApproval: $requireLastPushApproval, @@ -1851,7 +1851,7 @@ mutation createBranchProtectionRule( allowsDeletions: $allowsDeletions }) { branchProtectionRule { - databaseId + id } } }` @@ -1860,7 +1860,7 @@ type GraphqlBranchProtectionRuleCreationResponse struct { Data struct { CreateBranchProtectionRule struct { BranchProtectionRule struct { - DatabaseId int + Id string } } } @@ -1889,7 +1889,7 @@ func (g *GoliacRemoteImpl) AddRepositoryBranchProtection(ctx context.Context, dr ctx, createBranchProtectionRule, map[string]interface{}{ - "repositoryId": repo.Id, + "repositoryId": repo.RefId, "pattern": branchprotection.Pattern, "requiresApprovingReviews": branchprotection.RequiresApprovingReviews, "requiredApprovingReviewCount": branchprotection.RequiredApprovingReviewCount, @@ -1916,7 +1916,7 @@ func (g *GoliacRemoteImpl) AddRepositoryBranchProtection(ctx context.Context, dr logrus.Errorf("failed to add branch protection to repository %s: %v", reponame, err) } - branchprotection.DatabaseId = res.Data.CreateBranchProtectionRule.BranchProtectionRule.DatabaseId + branchprotection.Id = res.Data.CreateBranchProtectionRule.BranchProtectionRule.Id } repo.BranchProtections[branchprotection.Pattern] = branchprotection @@ -1943,7 +1943,7 @@ mutation updateBranchProtectionRule( branchProtectionRuleId: $branchProtectionRuleId, pattern: $pattern, requiresApprovingReviews: $requiresApprovingReviews, - requiredApprovingReviewCount: $requriedApprovingReviewCount, + requiredApprovingReviewCount: $requiredApprovingReviewCount, dismissesStaleReviews: $dismissesStaleReviews, requiresCodeOwnerReviews: $requiresCodeOwnerReviews, requireLastPushApproval: $requireLastPushApproval, @@ -1956,6 +1956,9 @@ mutation updateBranchProtectionRule( allowsForcePushes: $allowsForcePushes, allowsDeletions: $allowsDeletions }) { + branchProtectionRule { + id + } } }` @@ -1968,7 +1971,7 @@ func (g *GoliacRemoteImpl) UpdateRepositoryBranchProtection(ctx context.Context, ctx, updateBranchProtectionRule, map[string]interface{}{ - "branchProtectionRuleId": branchprotection.DatabaseId, + "branchProtectionRuleId": branchprotection.Id, "pattern": branchprotection.Pattern, "requiresApprovingReviews": branchprotection.RequiresApprovingReviews, "requiredApprovingReviewCount": branchprotection.RequiredApprovingReviewCount, @@ -2021,7 +2024,7 @@ func (g *GoliacRemoteImpl) DeleteRepositoryBranchProtection(ctx context.Context, ctx, deleteBranchProtectionRule, map[string]interface{}{ - "branchProtectionRuleId": branchprotection.DatabaseId, + "branchProtectionRuleId": branchprotection.Id, }, ) if err != nil {