Skip to content

Commit

Permalink
add readOnly check
Browse files Browse the repository at this point in the history
  • Loading branch information
jetersen committed Jul 30, 2022
1 parent eb15db4 commit 3eb7c08
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cmd/cmd-print.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func print(cmd *cobra.Command, args []string) error {
strOutput, _ := flag.GetString("output")
strErrOutput, _ := flag.GetString("error-output")

_ = flag.Set("readOnly", "true")

if concurrent < 1 {
return errors.New("concurrent runs can't be less than one")
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func configurePlatform(cmd *cobra.Command) {
flags.BoolP("include-subgroups", "", false, "Include GitLab subgroups when using the --group flag.")
flags.BoolP("ssh-auth", "", false, `Use SSH cloning URL instead of HTTPS + token. This requires that a setup with ssh keys that have access to all repos and that the server is already in known_hosts.`)

// This is only used by PrintCmd to mark readOnly mode for version control platform
flags.Bool("readOnly", false, "If set, This is running in readonly will be read-only.")
_ = flags.MarkHidden("readOnly")

flags.StringP("platform", "p", "github", "The platform that is used. Available values: github, gitlab, gitea, bitbucket_server.")
_ = cmd.RegisterFlagCompletionFunc("platform", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"github", "gitlab", "gitea", "bitbucket_server"}, cobra.ShellCompDirectiveDefault
Expand Down Expand Up @@ -122,6 +126,7 @@ func createGithubClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versi
forkMode, _ := flag.GetBool("fork")
forkOwner, _ := flag.GetString("fork-owner")
sshAuth, _ := flag.GetBool("ssh-auth")
readOnly, _ := flag.GetBool("readOnly")

if verifyFlags && len(orgs) == 0 && len(users) == 0 && len(repos) == 0 {
return nil, errors.New("no organization, user or repo set")
Expand Down Expand Up @@ -149,7 +154,7 @@ func createGithubClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versi
Organizations: orgs,
Users: users,
Repositories: repoRefs,
}, mergeTypes, forkMode, forkOwner, sshAuth)
}, mergeTypes, forkMode, forkOwner, sshAuth, readOnly)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion internal/scm/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func New(
forkMode bool,
forkOwner string,
sshAuth bool,
readOnly bool,
) (*Github, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
Expand Down Expand Up @@ -54,6 +55,7 @@ func New(
ForkOwner: forkOwner,
SSHAuth: sshAuth,
ghClient: client,
ReadOnly: readOnly,
httpClient: &http.Client{
Transport: transportMiddleware(http.DefaultTransport),
},
Expand All @@ -71,6 +73,9 @@ type Github struct {
// In this package, it mainly determines which repos are possible to make changes on
Fork bool

// This determines when we are running in read only mode.
ReadOnly bool

// If set, the fork will happen to the ForkOwner value, and not the logged in user
ForkOwner string

Expand Down Expand Up @@ -143,7 +148,7 @@ func (g *Github) GetRepositories(ctx context.Context) ([]scm.Repository, error)
case !permissions["pull"]:
log.Debug("Skipping repository since the token does not have pull permissions")
continue
case !g.Fork && !permissions["push"]:
case !g.Fork && !g.ReadOnly && !permissions["push"]:
log.Debug("Skipping repository since the token does not have push permissions and the run will not fork")
continue
}
Expand Down

0 comments on commit 3eb7c08

Please sign in to comment.