@@ -11,13 +11,16 @@ type RepositoryMetadata interface {
1111 IsPublic () bool
1212 OrganizationBlogURL () * string
1313 IsMFARequiredForAdministrativeActions () * bool
14+ IsDefaultBranchProtected () * bool
15+ DefaultBranchRequiresPRReviews () * bool
16+ IsDefaultBranchProtectedFromDeletion () * bool
1417}
1518
1619type GitHubRepositoryMetadata struct {
17- Releases []ReleaseData
18- Rulesets [] Ruleset
19- ghRepo * github.Repository
20- ghOrg * github.Organization
20+ Releases []ReleaseData
21+ defaultBranchRules * github. BranchRules
22+ ghRepo * github.Repository
23+ ghOrg * github.Organization
2124}
2225
2326func (r * GitHubRepositoryMetadata ) IsActive () bool {
@@ -28,6 +31,30 @@ func (r *GitHubRepositoryMetadata) IsPublic() bool {
2831 return ! r .ghRepo .GetPrivate ()
2932}
3033
34+ func (r * GitHubRepositoryMetadata ) IsDefaultBranchProtected () * bool {
35+ if r .defaultBranchRules == nil {
36+ return nil
37+ }
38+ updateBlockedByRule := r .defaultBranchRules != nil && len (r .defaultBranchRules .Update ) > 0
39+ return & updateBlockedByRule
40+ }
41+
42+ func (r * GitHubRepositoryMetadata ) IsDefaultBranchProtectedFromDeletion () * bool {
43+ if r .defaultBranchRules == nil {
44+ return nil
45+ }
46+ deletionBlockedByRule := r .defaultBranchRules != nil && len (r .defaultBranchRules .Deletion ) > 0
47+ return & deletionBlockedByRule
48+ }
49+
50+ func (r * GitHubRepositoryMetadata ) DefaultBranchRequiresPRReviews () * bool {
51+ if r .defaultBranchRules == nil {
52+ return nil
53+ }
54+ requiresReviews := r .defaultBranchRules != nil && r .defaultBranchRules .PullRequest != nil && len (r .defaultBranchRules .PullRequest ) > 0 && r .defaultBranchRules .PullRequest [0 ].Parameters .RequiredApprovingReviewCount > 0
55+ return & requiresReviews
56+ }
57+
3158func (r * GitHubRepositoryMetadata ) OrganizationBlogURL () * string {
3259 if r .ghOrg != nil {
3360 return r .ghOrg .Blog
@@ -53,8 +80,30 @@ func loadRepositoryMetadata(ghClient *github.Client, owner, repo string) (ghRepo
5380 ghRepo : repository ,
5481 }, nil
5582 }
83+ branchRules , err := getRuleset (ghClient , owner , repo , repository .GetDefaultBranch ())
84+ if err != nil {
85+ return repository , & GitHubRepositoryMetadata {
86+ ghRepo : repository ,
87+ ghOrg : organization ,
88+ }, nil
89+ }
5690 return repository , & GitHubRepositoryMetadata {
57- ghRepo : repository ,
58- ghOrg : organization ,
91+ ghRepo : repository ,
92+ ghOrg : organization ,
93+ defaultBranchRules : branchRules ,
5994 }, nil
6095}
96+
97+ func getRuleset (ghClient * github.Client , owner , repo string , branchName string ) (* github.BranchRules , error ) {
98+ branchRules , _ , err := ghClient .Repositories .GetRulesForBranch (
99+ context .Background (),
100+ owner ,
101+ repo ,
102+ branchName ,
103+ nil ,
104+ )
105+ if err != nil {
106+ return nil , err
107+ }
108+ return branchRules , nil
109+ }
0 commit comments