Skip to content

Commit 08620a4

Browse files
committed
CLI: Check and create forks on policy ops
Signed-off-by: Adolfo García Veytia (Puerco) <puerco@carabiner.dev>
1 parent c831ad0 commit 08620a4

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

internal/cmd/policy.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package cmd
66
import (
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
196201
sourcetool 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+
}

internal/cmd/setup.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ a fork of the repository you want to protect.
307307
// Create a new sourcetool object
308308
srctool, err := sourcetool.New(
309309
sourcetool.WithAuthenticator(authenticator),
310-
sourcetool.WithPolicyRepo(opts.policyRepo),
310+
// Uncomment when we support other policy repo
311+
// sourcetool.WithPolicyRepo(opts.policyRepo),
311312
sourcetool.WithUserForkOrg(opts.userForkOrg),
312313
sourcetool.WithEnforce(opts.enforce),
313314
)
@@ -316,8 +317,16 @@ a fork of the repository you want to protect.
316317
}
317318
cs := []models.ControlConfiguration{}
318319
if opts.interactive {
319-
fmt.Println("\nsourcetool is about to perform the following actions on your behalf:")
320-
fmt.Println("")
320+
// Check if we need the policy fork
321+
if slices.Contains(opts.configs, string(models.CONFIG_POLICY)) {
322+
if err := ensureOrCreatePolicyFork(srctool); err != nil {
323+
return err
324+
}
325+
}
326+
327+
fmt.Println()
328+
fmt.Println("sourcetool is about to perform the following actions on your behalf:")
329+
fmt.Println()
321330

322331
for _, c := range opts.configs {
323332
cs = append(cs, models.ControlConfiguration(c))

0 commit comments

Comments
 (0)