Skip to content

Commit 77fc6e7

Browse files
authored
Support updating policies in 'policy create' (#276)
This commit adds support for updating existing policies in sourcetool policy create. If an existing policy is found, sourcetool will ask if you want to update it. If the policy data has not changed, then nothing will be done. Signed-off-by: Adolfo Garcia Veytia (puerco) <[email protected]>
1 parent 6ecefcd commit 77fc6e7

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

internal/cmd/policy.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212

1313
"github.com/spf13/cobra"
14+
"google.golang.org/protobuf/proto"
1415
"sigs.k8s.io/release-utils/util"
1516

1617
"github.com/slsa-framework/slsa-source-poc/pkg/policy"
@@ -26,12 +27,14 @@ type policyCreateOpts struct {
2627
branchOptions
2728
interactive bool
2829
openPullRequest bool
30+
update bool
2931
}
3032

3133
func (pco *policyCreateOpts) AddFlags(cmd *cobra.Command) {
3234
pco.branchOptions.AddFlags(cmd)
3335
cmd.PersistentFlags().BoolVar(&pco.openPullRequest, "pr", true, "Open a pull request to check-in the policy")
3436
cmd.PersistentFlags().BoolVar(&pco.interactive, "interactive", true, "confirm before performing changes")
37+
cmd.PersistentFlags().BoolVar(&pco.update, "update", false, "update if existing policy found")
3538
}
3639

3740
func addPolicy(parentCmd *cobra.Command) {
@@ -172,6 +175,8 @@ just print the generated policy.
172175
return err
173176
}
174177

178+
fmt.Println()
179+
fmt.Println(w(fmt.Sprintf("Creating source policy for %s#%s", opts.GetRepository().GetHttpURL(), opts.GetBranch().Name)))
175180
// Create a new sourcetool object
176181
srctool, err := sourcetool.New(
177182
sourcetool.WithAuthenticator(authenticator),
@@ -187,8 +192,44 @@ just print the generated policy.
187192
if err != nil {
188193
return fmt.Errorf("checking for existing policy: %w", err)
189194
}
195+
196+
if epcy != nil && !opts.interactive && !opts.update {
197+
fmt.Fprintln(os.Stderr, "There is an existing policy in the community repository")
198+
return nil
199+
}
200+
201+
if epcy != nil && !opts.update {
202+
fmt.Println()
203+
fmt.Println("There is a policy already published for this repository.")
204+
fmt.Println("Do you want to update it?")
205+
fmt.Println()
206+
207+
_, s, err := util.Ask("Type 'yes' if you want to continue", "yes|no|no", 3)
208+
if err != nil {
209+
return err
210+
}
211+
212+
if !s {
213+
fmt.Println("Not changing existing policy.")
214+
return nil
215+
}
216+
}
217+
218+
// If there is an existing policy, check if something changed.
219+
var pcy *policy.RepoPolicy
220+
pcy, err = srctool.CreateBranchPolicy(
221+
context.Background(), opts.GetRepository(), []*models.Branch{opts.GetBranch()},
222+
)
223+
if err != nil {
224+
return fmt.Errorf("creating new source policy: %w", err)
225+
}
190226
if epcy != nil {
191-
return fmt.Errorf("repository already has a policy checked into the community repo")
227+
if proto.Equal(pcy, epcy) {
228+
fmt.Println()
229+
fmt.Printf("Repository policy has not changed. All done.")
230+
fmt.Println()
231+
return nil
232+
}
192233
}
193234

194235
if opts.openPullRequest && opts.interactive {
@@ -222,7 +263,7 @@ open the pull request from there.
222263

223264
// Create the policy, this will open the pull request in the community
224265
// repo if the options say so.
225-
pcy, pr, err := srctool.CreateRepositoryPolicy(
266+
_, pr, err := srctool.CreateRepositoryPolicy(
226267
context.Background(), opts.GetRepository(), []*models.Branch{opts.GetBranch()},
227268
)
228269
if err != nil {

0 commit comments

Comments
 (0)