@@ -6,6 +6,7 @@ package cmd
66import (
77 "context"
88 "encoding/json"
9+ "errors"
910 "fmt"
1011 "os"
1112
@@ -191,6 +192,10 @@ just print the generated policy.
191192 }
192193
193194 if opts .openPullRequest && opts .interactive {
195+ if err := ensureOrCreatePolicyFork (srctool ); err != nil {
196+ return err
197+ }
198+
194199 fmt .Printf (`
195200
196201sourcetool is about to perform the following actions on your behalf:
@@ -230,7 +235,7 @@ open the pull request from there.
230235
231236 if opts .openPullRequest && pr != nil {
232237 fmt .Fprintf (os .Stderr , "\n " )
233- fmt .Fprintf (os .Stderr , "Opened pull request: https://github.com/%s/pulls /%d\n \n " , pr .Repo .Path , pr .Number )
238+ fmt .Fprintf (os .Stderr , "pull request open : https://github.com/%s/pull /%d\n \n " , pr .Repo .Path , pr .Number )
234239 }
235240
236241 return nil
@@ -260,3 +265,37 @@ func displayPolicy(opts repoOptions, pcy *policy.RepoPolicy) error {
260265 fmt .Println ()
261266 return nil
262267}
268+
269+ // ensureOrCreatePolicyFork checks the user has a fork of the policy repo.
270+ // In case they do not, asks to create a fork in their GitHub account.
271+ func ensureOrCreatePolicyFork (srctool * sourcetool.Tool ) error {
272+ found , err := srctool .CheckPolicyRepoFork ()
273+ if err != nil {
274+ return fmt .Errorf ("checking for policy repo fork: %w" , err )
275+ }
276+ if found {
277+ return nil
278+ }
279+
280+ fmt .Println ()
281+ fmt .Println ()
282+ fmt .Printf ("%s sourcetool could not find a fork of the community source\n " , w ("Note:" ))
283+ fmt .Printf ("policy repository (%s). We need it to\n " , srctool .Options .PolicyRepo )
284+ fmt .Println ("create pull requests for your policies." )
285+ fmt .Println ()
286+ fmt .Println ("Would you like to create the fork in your GitHub account now?" )
287+ fmt .Println ()
288+
289+ _ , s , err := util .Ask ("Type 'yes' if you want to continue?" , "yes|no|no" , 3 )
290+ if err != nil {
291+ return err
292+ }
293+ if ! s {
294+ return errors .New ("no policy repo found and creation declined" )
295+ }
296+
297+ if err := srctool .CreatePolicyRepoFork (context .Background ()); err != nil {
298+ return err
299+ }
300+ return nil
301+ }
0 commit comments