Skip to content

Commit 8149757

Browse files
authored
Retry commit (#278)
* Expose GetBranchPolicy() func Signed-off-by: Adolfo Garcia Veytia (puerco) <[email protected]> * status: Show if policy needs update Signed-off-by: Adolfo Garcia Veytia (puerco) <[email protected]> * Add recommended action when policy needs update Signed-off-by: Adolfo Garcia Veytia (puerco) <[email protected]> --------- Signed-off-by: Adolfo Garcia Veytia (puerco) <[email protected]>
1 parent 97252a7 commit 8149757

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

internal/cmd/status.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cmd
55

66
import (
7+
"context"
78
"errors"
89
"fmt"
910
"strings"
@@ -146,11 +147,22 @@ sourcetool status myorg/myrepo@mybranch
146147
}
147148

148149
fmt.Println()
150+
policyNeedsUpdate := false
149151
if policyControlStatus != nil {
150152
fmt.Printf("%-35s ", "Repo policy found:")
151153
switch policyControlStatus.State {
152154
case slsa.StateActive:
153-
fmt.Println("✅")
155+
fmt.Print("✅")
156+
// Check if the policy needs updating
157+
pcy, err := srctool.GetRepositoryPolicy(context.Background(), opts.GetRepository())
158+
if err == nil {
159+
pb := pcy.GetBranchPolicy(opts.GetBranch().Name)
160+
if pb != nil && pb.GetTargetSlsaSourceLevel() != string(toplevel) {
161+
fmt.Print(w2(fmt.Sprintf(" (needs update to %s)", toplevel)))
162+
policyNeedsUpdate = true
163+
}
164+
}
165+
fmt.Println()
154166
case slsa.StateNotEnabled:
155167
fmt.Println("🚫")
156168
case slsa.StateInProgress:
@@ -188,6 +200,15 @@ sourcetool status myorg/myrepo@mybranch
188200
fmt.Println()
189201
}
190202

203+
if policyNeedsUpdate {
204+
if !titled {
205+
fmt.Println(w2("✨ Recommended actions:"))
206+
}
207+
fmt.Println(" - Update the repository source policy")
208+
fmt.Printf(" > sourcetool policy create --update %s\n", opts.GetRepository().Path)
209+
fmt.Println()
210+
}
211+
191212
return nil
192213
},
193214
}

pkg/policy/policy.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const (
3838
)
3939

4040
// Returns the policy for the branch or nil if the branch doesn't have one.
41-
func (rp *RepoPolicy) getBranchPolicy(branch string) *ProtectedBranch {
41+
func (rp *RepoPolicy) GetBranchPolicy(branch string) *ProtectedBranch {
42+
branch = strings.TrimPrefix(branch, "refs/heads/")
4243
for _, pb := range rp.GetProtectedBranches() {
4344
if pb.GetName() == branch {
4445
return pb
@@ -525,7 +526,7 @@ func (pe *PolicyEvaluator) EvaluateControl(ctx context.Context, repo *models.Rep
525526
return slsa.SourceVerifiedLevels{}, "", err
526527
}
527528

528-
branchPolicy := rp.getBranchPolicy(branch.Name)
529+
branchPolicy := rp.GetBranchPolicy(branch.Name)
529530
if branchPolicy == nil {
530531
branchPolicy = createDefaultBranchPolicy(branch)
531532
policyPath = "DEFAULT"
@@ -555,7 +556,7 @@ func (pe *PolicyEvaluator) EvaluateSourceProv(ctx context.Context, repo *models.
555556
return slsa.SourceVerifiedLevels{}, "", err
556557
}
557558

558-
branchPolicy := rp.getBranchPolicy(branch.Name)
559+
branchPolicy := rp.GetBranchPolicy(branch.Name)
559560
if branchPolicy == nil {
560561
branchPolicy = createDefaultBranchPolicy(branch)
561562
policyPath = "DEFAULT"

pkg/policy/policy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ func assertPolicyResultEquals(t *testing.T, ctx context.Context, ghConn *ghcontr
14431443

14441444
// TODO: check the rest of the contents of expectedPolicy?
14451445

1446-
gotPb := rp.getBranchPolicy(ghcontrol.GetBranchFromRef(ghConn.GetFullRef()))
1446+
gotPb := rp.GetBranchPolicy(ghcontrol.GetBranchFromRef(ghConn.GetFullRef()))
14471447

14481448
if expectedBranchPolicy == nil {
14491449
if gotPb != nil {

0 commit comments

Comments
 (0)