@@ -11,6 +11,7 @@ import (
11
11
"os"
12
12
13
13
"github.com/spf13/cobra"
14
+ "google.golang.org/protobuf/proto"
14
15
"sigs.k8s.io/release-utils/util"
15
16
16
17
"github.com/slsa-framework/slsa-source-poc/pkg/policy"
@@ -26,12 +27,14 @@ type policyCreateOpts struct {
26
27
branchOptions
27
28
interactive bool
28
29
openPullRequest bool
30
+ update bool
29
31
}
30
32
31
33
func (pco * policyCreateOpts ) AddFlags (cmd * cobra.Command ) {
32
34
pco .branchOptions .AddFlags (cmd )
33
35
cmd .PersistentFlags ().BoolVar (& pco .openPullRequest , "pr" , true , "Open a pull request to check-in the policy" )
34
36
cmd .PersistentFlags ().BoolVar (& pco .interactive , "interactive" , true , "confirm before performing changes" )
37
+ cmd .PersistentFlags ().BoolVar (& pco .update , "update" , false , "update if existing policy found" )
35
38
}
36
39
37
40
func addPolicy (parentCmd * cobra.Command ) {
@@ -172,6 +175,8 @@ just print the generated policy.
172
175
return err
173
176
}
174
177
178
+ fmt .Println ()
179
+ fmt .Println (w (fmt .Sprintf ("Creating source policy for %s#%s" , opts .GetRepository ().GetHttpURL (), opts .GetBranch ().Name )))
175
180
// Create a new sourcetool object
176
181
srctool , err := sourcetool .New (
177
182
sourcetool .WithAuthenticator (authenticator ),
@@ -187,8 +192,44 @@ just print the generated policy.
187
192
if err != nil {
188
193
return fmt .Errorf ("checking for existing policy: %w" , err )
189
194
}
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
+ }
190
226
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
+ }
192
233
}
193
234
194
235
if opts .openPullRequest && opts .interactive {
@@ -222,7 +263,7 @@ open the pull request from there.
222
263
223
264
// Create the policy, this will open the pull request in the community
224
265
// repo if the options say so.
225
- pcy , pr , err := srctool .CreateRepositoryPolicy (
266
+ _ , pr , err := srctool .CreateRepositoryPolicy (
226
267
context .Background (), opts .GetRepository (), []* models.Branch {opts .GetBranch ()},
227
268
)
228
269
if err != nil {
0 commit comments