Skip to content

Commit 12e0d48

Browse files
authored
Fix policy PR, add interactive mode (#237)
* Fix options not parsing branch Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]> * Fix bug creating local feature branch Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]> * Add interactive mode to policy create Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]> --------- Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
1 parent 7b2df11 commit 12e0d48

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

sourcetool/internal/cmd/options.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ type branchOptions struct {
9191
branch string
9292
}
9393

94+
// ParseLocator parses an SPDX locator string and assigns its components
95+
// to the branch options fields.
9496
func (bo *branchOptions) ParseLocator(lString string) error {
95-
components, err := vcslocator.Locator(lString).Parse()
97+
components, err := vcslocator.Locator(lString).Parse(vcslocator.WithRefAsBranch(true))
9698
if err != nil {
9799
return fmt.Errorf("parsing repository slug: %w", err)
98100
}

sourcetool/internal/cmd/policy.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88

99
"github.com/spf13/cobra"
10+
"sigs.k8s.io/release-utils/util"
1011

1112
"github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/policy"
1213
"github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/sourcetool"
@@ -19,12 +20,14 @@ type policyViewOpts struct {
1920

2021
type policyCreateOpts struct {
2122
branchOptions
23+
interactive bool
2224
openPullRequest bool
2325
}
2426

2527
func (pco *policyCreateOpts) AddFlags(cmd *cobra.Command) {
2628
pco.branchOptions.AddFlags(cmd)
2729
cmd.PersistentFlags().BoolVar(&pco.openPullRequest, "pr", true, "Open a pull request to check-in the policy")
30+
cmd.PersistentFlags().BoolVar(&pco.interactive, "interactive", true, "confirm before performing changes")
2831
}
2932

3033
func addPolicy(parentCmd *cobra.Command) {
@@ -125,7 +128,10 @@ func addPolicyCreate(parent *cobra.Command) {
125128
policyViewCmd := &cobra.Command{
126129
Short: "creates a source policy for a repository",
127130
Long: `The create subcommand inspects the controls in place for a repo
128-
and creates a new policy for it.
131+
and creates a new policy for it. By default it will create a pull request
132+
in the community source policy repository. If you choose not to, it will
133+
just print the generated policy.
134+
129135
`,
130136
Use: "create owner/repo@branch",
131137
SilenceUsage: false,
@@ -143,6 +149,10 @@ and creates a new policy for it.
143149
return err
144150
}
145151

152+
if err := opts.EnsureDefaults(); err != nil {
153+
return err
154+
}
155+
146156
return nil
147157
},
148158
RunE: func(cmd *cobra.Command, args []string) (err error) {
@@ -161,7 +171,9 @@ and creates a new policy for it.
161171
// Create a new sourcetool object
162172
srctool, err := sourcetool.New(
163173
sourcetool.WithAuthenticator(authenticator),
174+
// Uncomment when we want to support custom policy repos
164175
// sourcetool.WithPolicyRepo(opts.policyRepo),
176+
sourcetool.WithCreatePolicyPR(opts.openPullRequest),
165177
)
166178
if err != nil {
167179
return err
@@ -175,6 +187,31 @@ and creates a new policy for it.
175187
return fmt.Errorf("repository already has a policy checked into the community repo")
176188
}
177189

190+
if opts.openPullRequest && opts.interactive {
191+
fmt.Printf(`
192+
193+
sourcetool is about to perform the following actions on your behalf:
194+
195+
> Open a pull request in %s/%s checking in
196+
a SLSA source policy for the current controls enabled
197+
in %s/%s.
198+
199+
We will push a branch to your fork of the community repository and
200+
open the pull request from there.
201+
202+
`, policy.SourcePolicyRepoOwner, policy.SourcePolicyRepo, opts.owner, opts.repository)
203+
204+
_, s, err := util.Ask("Type 'yes' if you want to continue?", "yes|no|no", 3)
205+
if err != nil {
206+
return err
207+
}
208+
209+
if !s {
210+
fmt.Println("Cancelled.")
211+
return nil
212+
}
213+
}
214+
178215
// Create the policy, this will open the pull request in the community
179216
// repo if the options say so.
180217
pcy, pr, err := srctool.CreateRepositoryPolicy(

sourcetool/internal/cmd/setup.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/spf13/cobra"
99
"sigs.k8s.io/release-utils/util"
1010

11-
"github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/policy"
1211
"github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/sourcetool"
1312
"github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/sourcetool/models"
1413
)
@@ -30,9 +29,10 @@ func (so *setupOpts) AddFlags(cmd *cobra.Command) {
3029
cmd.PersistentFlags().StringVar(
3130
&so.userForkOrg, "user-fork", "", "GitHub organization to look for forks of repos (for pull requests)",
3231
)
33-
cmd.PersistentFlags().StringVar(
34-
&so.policyRepo, "policy-repo", fmt.Sprintf("%s/%s", policy.SourcePolicyRepoOwner, policy.SourcePolicyRepo), "repository to store the SLSA source policy",
35-
)
32+
// Uncomment when we support custom policy repos
33+
// cmd.PersistentFlags().StringVar(
34+
// &so.policyRepo, "policy-repo", fmt.Sprintf("%s/%s", policy.SourcePolicyRepoOwner, policy.SourcePolicyRepo), "repository to store the SLSA source policy",
35+
// )
3636

3737
cmd.PersistentFlags().BoolVar(
3838
&so.interactive, "interactive", true, "confirm before performing changes",

sourcetool/pkg/repo/clone.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,14 @@ func (c *Clone) AddRemote(name, url string) error {
9999

100100
// PushToRemote pushes the active branch to the specified remote
101101
func (c *Clone) PushRemote(remoteName string) error {
102+
refSpecs := []config.RefSpec{
103+
config.RefSpec(fmt.Sprintf(
104+
"+refs/heads/%s:refs/heads/%s", c.FeatureBranch, c.FeatureBranch,
105+
)),
106+
}
102107
err := c.repo.Push(&git.PushOptions{
103108
RemoteName: remoteName,
109+
RefSpecs: refSpecs,
104110
})
105111
if err != nil {
106112
return fmt.Errorf("pushing to remote: %w", err)

sourcetool/pkg/repo/implementation.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ func (impl *defaultPrmImpl) CloneRepo(opts *options.PullRequestManagerOptions, a
134134
return nil, fmt.Errorf("adding remote: %w", err)
135135
}
136136

137+
// Create the feature branch
138+
if err := clone.CreateFeatureBranch(); err != nil {
139+
clone.Cleanup()
140+
return nil, fmt.Errorf("creating feature branch locally: %w", err)
141+
}
142+
137143
return clone, nil
138144
}
139145

0 commit comments

Comments
 (0)