Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion internal/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package cmd

import (
"context"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -146,11 +147,22 @@ sourcetool status myorg/myrepo@mybranch
}

fmt.Println()
policyNeedsUpdate := false
if policyControlStatus != nil {
fmt.Printf("%-35s ", "Repo policy found:")
switch policyControlStatus.State {
case slsa.StateActive:
fmt.Println("✅")
fmt.Print("✅")
// Check if the policy needs updating
pcy, err := srctool.GetRepositoryPolicy(context.Background(), opts.GetRepository())
if err == nil {
pb := pcy.GetBranchPolicy(opts.GetBranch().Name)
if pb != nil && pb.GetTargetSlsaSourceLevel() != string(toplevel) {
fmt.Print(w2(fmt.Sprintf(" (needs update to %s)", toplevel)))
policyNeedsUpdate = true
}
}
fmt.Println()
case slsa.StateNotEnabled:
fmt.Println("🚫")
case slsa.StateInProgress:
Expand Down Expand Up @@ -188,6 +200,15 @@ sourcetool status myorg/myrepo@mybranch
fmt.Println()
}

if policyNeedsUpdate {
if !titled {
fmt.Println(w2("✨ Recommended actions:"))
}
fmt.Println(" - Update the repository source policy")
fmt.Printf(" > sourcetool policy create --update %s\n", opts.GetRepository().Path)
fmt.Println()
}

return nil
},
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const (
)

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

branchPolicy := rp.getBranchPolicy(branch.Name)
branchPolicy := rp.GetBranchPolicy(branch.Name)
if branchPolicy == nil {
branchPolicy = createDefaultBranchPolicy(branch)
policyPath = "DEFAULT"
Expand Down Expand Up @@ -555,7 +556,7 @@ func (pe *PolicyEvaluator) EvaluateSourceProv(ctx context.Context, repo *models.
return slsa.SourceVerifiedLevels{}, "", err
}

branchPolicy := rp.getBranchPolicy(branch.Name)
branchPolicy := rp.GetBranchPolicy(branch.Name)
if branchPolicy == nil {
branchPolicy = createDefaultBranchPolicy(branch)
policyPath = "DEFAULT"
Expand Down
2 changes: 1 addition & 1 deletion pkg/policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@ func assertPolicyResultEquals(t *testing.T, ctx context.Context, ghConn *ghcontr

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

gotPb := rp.getBranchPolicy(ghcontrol.GetBranchFromRef(ghConn.GetFullRef()))
gotPb := rp.GetBranchPolicy(ghcontrol.GetBranchFromRef(ghConn.GetFullRef()))

if expectedBranchPolicy == nil {
if gotPb != nil {
Expand Down
Loading