@@ -232,49 +232,41 @@ func (ghc *GitHubConnection) getOldestActiveRule(ctx context.Context, rules []*g
232
232
return oldestActive , nil
233
233
}
234
234
235
- // Determines the controls that are in place for a branch at a specific commit using GitHub's APIs
236
- // This is necessarily only as good as GitHub's controls and existing APIs.
237
- func (ghc * GitHubConnection ) GetBranchControls (ctx context.Context , commit , ref string ) (* GhControlStatus , error ) {
238
- // We want to know when this commit was pushed to ensure the rules were active _then_.
239
- activity , err := ghc .commitActivity (ctx , commit , ref )
240
- if err != nil {
241
- return nil , err
242
- }
243
-
244
- controlStatus := GhControlStatus {
245
- CommitPushTime : activity .Timestamp ,
246
- ActivityType : activity .ActivityType ,
247
- ActorLogin : activity .Actor .Login ,
248
- Controls : slsa.Controls {},
249
- }
250
-
235
+ // GetBranchControls returns a list of the controls enabled at present for a branch.
236
+ // This function does not take into account a commit date, it just returns those controls
237
+ // that are active when called.
238
+ func (ghc * GitHubConnection ) GetBranchControls (ctx context.Context , ref string ) (* slsa.Controls , error ) {
251
239
branch := GetBranchFromRef (ref )
252
240
if branch == "" {
253
241
return nil , fmt .Errorf ("ref %s is not a branch" , ref )
254
242
}
243
+
244
+ controls := & slsa.Controls {}
245
+
255
246
// Do the branch specific stuff.
256
247
branchRules , _ , err := ghc .Client ().Repositories .GetRulesForBranch (ctx , ghc .Owner (), ghc .Repo (), branch )
257
248
if err != nil {
258
249
return nil , err
259
250
}
251
+
260
252
// Compute the controls enforced.
261
253
continuityControl , err := ghc .computeContinuityControl (ctx , branchRules )
262
254
if err != nil {
263
255
return nil , fmt .Errorf ("could not populate ContinuityControl: %w" , err )
264
256
}
265
- controlStatus .AddControl (continuityControl )
257
+ controls .AddControl (continuityControl )
266
258
267
259
reviewControl , err := ghc .computeReviewControl (ctx , branchRules .PullRequest )
268
260
if err != nil {
269
261
return nil , fmt .Errorf ("could not populate ReviewControl: %w" , err )
270
262
}
271
- controlStatus .AddControl (reviewControl )
263
+ controls .AddControl (reviewControl )
272
264
273
265
requiredCheckControls , err := ghc .computeRequiredChecks (ctx , branchRules .RequiredStatusChecks )
274
266
if err != nil {
275
267
return nil , fmt .Errorf ("could not populate RequiredChecks: %w" , err )
276
268
}
277
- controlStatus .AddControl (requiredCheckControls ... )
269
+ controls .AddControl (requiredCheckControls ... )
278
270
279
271
// Check the tag rules.
280
272
allRulesets , _ , err := ghc .Client ().Repositories .GetAllRulesets (ctx , ghc .Owner (), ghc .Repo (), true )
@@ -285,7 +277,38 @@ func (ghc *GitHubConnection) GetBranchControls(ctx context.Context, commit, ref
285
277
if err != nil {
286
278
return nil , fmt .Errorf ("could not populate TagHygieneControl: %w" , err )
287
279
}
288
- controlStatus .AddControl (TagHygieneControl )
280
+ controls .AddControl (TagHygieneControl )
281
+
282
+ return controls , nil
283
+ }
284
+
285
+ // GetBranchControlsAtCommit determines the controls that are in place for a branch
286
+ // at a specific commit using GitHub's APIs. This is necessarily only as good as
287
+ // GitHub's controls and existing APIs.
288
+ func (ghc * GitHubConnection ) GetBranchControlsAtCommit (ctx context.Context , commit , ref string ) (* GhControlStatus , error ) {
289
+ // We want to know when this commit was pushed to ensure the rules were active _then_.
290
+ activity , err := ghc .commitActivity (ctx , commit , ref )
291
+ if err != nil {
292
+ return nil , err
293
+ }
294
+
295
+ controlStatus := GhControlStatus {
296
+ CommitPushTime : activity .Timestamp ,
297
+ ActivityType : activity .ActivityType ,
298
+ ActorLogin : activity .Actor .Login ,
299
+ Controls : slsa.Controls {},
300
+ }
301
+
302
+ activeControls , err := ghc .GetBranchControls (ctx , ref )
303
+ if err != nil {
304
+ return nil , fmt .Errorf ("reading active controls: %w" , err )
305
+ }
306
+
307
+ // Add the controls to the control status object. This will
308
+ // discard any that were not active when the commit merged.
309
+ for _ , c := range * activeControls {
310
+ controlStatus .AddControl (& c )
311
+ }
289
312
290
313
return & controlStatus , nil
291
314
}
0 commit comments