1
+ using Octokit ;
2
+ using Octokit . GraphQL ;
3
+ using Octokit . GraphQL . Core ;
4
+ using Octokit . GraphQL . Model ;
5
+
6
+ using Connection = Octokit . GraphQL . Connection ;
7
+ using Repository = Octokit . Repository ;
8
+
9
+ namespace Nullinside . Cicd . GitHub . Rule ;
10
+
11
+ public class CreateRulesets : IRepoRule {
12
+ public async Task Handle ( GitHubClient client , Connection graphQl , ID projectId , Repository repo ) {
13
+ // This currently doesn't run properly. You get an error about not specifying multiple Parameters on the status
14
+ // checks. Waiting on an update from the source library to not include nulls in the compiled query.
15
+ // https://github.com/octokit/octokit.graphql.net/issues/320
16
+ IEnumerable < string > ? rulesets = await graphQl . Run ( new Query ( )
17
+ . Repository ( repo . Name , Constants . GITHUB_ORG )
18
+ . Rulesets ( )
19
+ . AllPages ( )
20
+ . Select ( i => i . Name ) ) ;
21
+
22
+ string ? expectedRuleset =
23
+ rulesets . FirstOrDefault ( r => "main" . Equals ( r , StringComparison . InvariantCultureIgnoreCase ) ) ;
24
+ if ( null != expectedRuleset ) {
25
+ return ;
26
+ }
27
+
28
+ ID id = await graphQl . Run ( new Query ( )
29
+ . Repository ( repo . Name , Constants . GITHUB_ORG )
30
+ . Select ( i => i . Id ) ) ;
31
+
32
+ Console . WriteLine ( $ "{ repo . Name } : Creating default ruleset") ;
33
+ StatusCheckConfigurationInput [ ] statusChecks = null ;
34
+ if ( "Typescript" . Equals ( repo . Language , StringComparison . InvariantCultureIgnoreCase ) ) {
35
+ statusChecks = new [ ] {
36
+ new StatusCheckConfigurationInput {
37
+ Context = "ng test"
38
+ } ,
39
+ new StatusCheckConfigurationInput {
40
+ Context = "ng lint"
41
+ } ,
42
+ new StatusCheckConfigurationInput {
43
+ Context = "Analyze (javascript-typescript)"
44
+ } ,
45
+ new StatusCheckConfigurationInput {
46
+ Context = "CodeQL"
47
+ }
48
+ } ;
49
+ }
50
+ else if ( "C#" . Equals ( repo . Language , StringComparison . InvariantCultureIgnoreCase ) ) {
51
+ statusChecks = new [ ] {
52
+ new StatusCheckConfigurationInput {
53
+ Context = "Roslyn Compiler Warnings"
54
+ } ,
55
+ new StatusCheckConfigurationInput {
56
+ Context = "Tests"
57
+ } ,
58
+ new StatusCheckConfigurationInput {
59
+ Context = "Analyze (csharp)"
60
+ } ,
61
+ new StatusCheckConfigurationInput {
62
+ Context = "CodeQL"
63
+ }
64
+ } ;
65
+ }
66
+
67
+ var rules = new List < RepositoryRuleInput > ( [
68
+ new RepositoryRuleInput {
69
+ Type = RepositoryRuleType . Deletion
70
+ } ,
71
+ new RepositoryRuleInput {
72
+ Type = RepositoryRuleType . PullRequest ,
73
+ Parameters = new RuleParametersInput {
74
+ PullRequest = new PullRequestParametersInput {
75
+ DismissStaleReviewsOnPush = true ,
76
+ RequireCodeOwnerReview = true
77
+ }
78
+ }
79
+ }
80
+ ] ) ;
81
+
82
+ if ( null != statusChecks ) {
83
+ rules . Add ( new RepositoryRuleInput {
84
+ Type = RepositoryRuleType . RequiredStatusChecks ,
85
+ Parameters = new RuleParametersInput {
86
+ RequiredStatusChecks = new RequiredStatusChecksParametersInput {
87
+ RequiredStatusChecks = statusChecks ,
88
+ StrictRequiredStatusChecksPolicy = true
89
+ }
90
+ }
91
+ } ) ;
92
+ }
93
+
94
+ await graphQl . Run ( new Mutation ( )
95
+ . CreateRepositoryRuleset ( new Arg < CreateRepositoryRulesetInput > ( new CreateRepositoryRulesetInput {
96
+ SourceId = id ,
97
+ Name = "main" ,
98
+ Enforcement = RuleEnforcement . Active ,
99
+ Target = RepositoryRulesetTarget . Branch ,
100
+ Rules = rules ,
101
+ Conditions = new RepositoryRuleConditionsInput {
102
+ RefName = new RefNameConditionTargetInput {
103
+ Include = new [ ] { "~DEFAULT_BRANCH" } ,
104
+ Exclude = new string [ ] { }
105
+ }
106
+ }
107
+ } ) )
108
+ . Select ( x => x . Ruleset . Id )
109
+ ) ;
110
+ }
111
+ }
0 commit comments