Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
More relaxed branch protection
Browse files Browse the repository at this point in the history
Ref: ipfs/aegir#225

License: MIT
Signed-off-by: Victor Bjelkholm <[email protected]>
  • Loading branch information
victorb committed May 7, 2018
1 parent ca8f090 commit f66ab48
Showing 1 changed file with 63 additions and 40 deletions.
103 changes: 63 additions & 40 deletions checks/github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,70 @@ import (
// "restricts who can push to this branch"

func GithubBranchProtection(client *github.Client, repo *github.Repository) bool {
// Skipping most of the protections for now
// Ref: https://github.com/ipfs/aegir/issues/225

// ctx := context.Background()
// protection, res, err := client.Repositories.GetBranchProtection(ctx, repo.GetOwner().GetLogin(), repo.GetName(), "master")
// if err != nil && res.StatusCode != 404 {
// panic(err)
// }
// hasProtection := protection != nil
// // Attempt to fix protection if none exists
// // TODO should check individual rules to see if they are correct
// if !hasProtection {
// Fix protection
preq := &github.ProtectionRequest{
RequiredStatusChecks: &github.RequiredStatusChecks{
Strict: true,
Contexts: []string{},
// Contexts: []string{"continuous-integration/jenkins/pr-merge"},
},
RequiredPullRequestReviews: nil,
// RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{
// DismissalRestrictionsRequest: &github.DismissalRestrictionsRequest{
// Users: &[]string{},
// Teams: &[]string{},
// },
// DismissStaleReviews: true,
// // TODO change this once we have code owners (dx wants to own tests for example)
// RequireCodeOwnerReviews: false,
// },
EnforceAdmins: false,
Restrictions: &github.BranchRestrictionsRequest{
Users: []string{
// From https://github.com/ipfs/pm/issues/600#issuecomment-385674334
"diasdavid",
"VictorBjelkholm",
"olizilla",
"hacdias",
"vmx",
"kumavis",
"wanderer",
"pgte",
"dignifiedquire",
"jacobheun",
"achingbrain",
"alanshaw",
},
Teams: []string{},
},
}
ctx := context.Background()
protection, res, err := client.Repositories.GetBranchProtection(ctx, repo.GetOwner().GetLogin(), repo.GetName(), "master")
if err != nil && res.StatusCode != 404 {
panic(err)
_, res, err := client.Repositories.UpdateBranchProtection(ctx, repo.GetOwner().GetLogin(), repo.GetName(), "master", preq)
if res.StatusCode == 404 {
log.Println(res.String())
log.Println("Repo missing master branch???")
return false
}
hasProtection := protection != nil
// Attempt to fix protection if none exists
// TODO should check individual rules to see if they are correct
if !hasProtection {
// Fix protection
preq := &github.ProtectionRequest{
RequiredStatusChecks: &github.RequiredStatusChecks{
Strict: true,
Contexts: []string{"continuous-integration/jenkins/pr-merge"},
},
RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{
DismissalRestrictionsRequest: &github.DismissalRestrictionsRequest{
Users: &[]string{},
Teams: &[]string{},
},
DismissStaleReviews: true,
// TODO change this once we have code owners (dx wants to own tests for example)
RequireCodeOwnerReviews: false,
},
EnforceAdmins: true,
Restrictions: nil,
}
ctx := context.Background()
_, res, err := client.Repositories.UpdateBranchProtection(ctx, repo.GetOwner().GetLogin(), repo.GetName(), "master", preq)
if res.StatusCode == 404 {
log.Println(res.String())
log.Println("Repo missing master branch???")
return false
}
if err != nil {
panic(err)
}
// Call to check if it's been fixed now (should be!)
return GithubBranchProtection(client, repo)
if err != nil {
panic(err)
}
log.Print("Has protection? ", hasProtection)
// Should always return true as if fails, we should fix them
return hasProtection
// Call to check if it's been fixed now (should be!)
return true
// return GithubBranchProtection(client, repo)
// }
// log.Print("Has protection? ", hasProtection)
// // Should always return true as if fails, we should fix them
// return hasProtection
}

0 comments on commit f66ab48

Please sign in to comment.