Skip to content

Commit

Permalink
Use id to access orgs (#1873)
Browse files Browse the repository at this point in the history
closes #1743 

fixes: setting secrets for own user namespace

- create org in database
- use orgID for org related APIs

Co-authored-by: 6543 <[email protected]>
  • Loading branch information
anbraten and 6543 authored Jul 21, 2023
1 parent aec2051 commit e5d5ec8
Show file tree
Hide file tree
Showing 51 changed files with 1,258 additions and 389 deletions.
6 changes: 6 additions & 0 deletions cli/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ var RepoFlag = &cli.StringFlag{
Aliases: []string{"repo"},
Usage: "repository id or full-name (e.g. 134 or octocat/hello-world)",
}

var OrgFlag = &cli.StringFlag{
Name: "organization",
Aliases: []string{"org"},
Usage: "organization id or full-name (e.g. 123 or octocat)",
}
37 changes: 28 additions & 9 deletions cli/secret/secret.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package secret

import (
"fmt"
"strconv"
"strings"

"github.com/urfave/cli/v2"
Expand All @@ -24,29 +26,46 @@ var Command = &cli.Command{
},
}

func parseTargetArgs(client woodpecker.Client, c *cli.Context) (global bool, owner string, repoID int64, err error) {
func parseTargetArgs(client woodpecker.Client, c *cli.Context) (global bool, orgID, repoID int64, err error) {
if c.Bool("global") {
return true, "", -1, nil
return true, -1, -1, nil
}

repoIDOrFullName := c.String("repository")
if repoIDOrFullName == "" {
repoIDOrFullName = c.Args().First()
}

orgName := c.String("organization")
if orgName != "" && repoIDOrFullName == "" {
return false, orgName, -1, err
orgIDOrName := c.String("organization")
if orgIDOrName == "" && repoIDOrFullName == "" {
if err := cli.ShowSubcommandHelp(c); err != nil {
return false, -1, -1, err
}

return false, -1, -1, fmt.Errorf("missing arguments")
}

if orgIDOrName != "" && repoIDOrFullName == "" {
if orgID, err := strconv.ParseInt(orgIDOrName, 10, 64); err == nil {
return false, orgID, -1, nil
}

org, err := client.OrgLookup(orgIDOrName)
if err != nil {
return false, -1, -1, err
}

return false, org.ID, -1, nil
}

if orgName != "" && !strings.Contains(repoIDOrFullName, "/") {
repoIDOrFullName = orgName + "/" + repoIDOrFullName
if orgIDOrName != "" && !strings.Contains(repoIDOrFullName, "/") {
repoIDOrFullName = orgIDOrName + "/" + repoIDOrFullName
}

repoID, err = internal.ParseRepo(client, repoIDOrFullName)
if err != nil {
return false, "", -1, err
return false, -1, -1, err
}

return false, "", repoID, nil
return false, -1, repoID, nil
}
13 changes: 6 additions & 7 deletions cli/secret/secret_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ var secretCreateCmd = &cli.Command{
Name: "global",
Usage: "global secret",
},
&cli.StringFlag{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
common.OrgFlag,
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Expand Down Expand Up @@ -74,7 +71,7 @@ func secretCreate(c *cli.Context) error {
secret.Value = string(out)
}

global, owner, repoID, err := parseTargetArgs(client, c)
global, orgID, repoID, err := parseTargetArgs(client, c)
if err != nil {
return err
}
Expand All @@ -83,10 +80,12 @@ func secretCreate(c *cli.Context) error {
_, err = client.GlobalSecretCreate(secret)
return err
}
if owner != "" {
_, err = client.OrgSecretCreate(owner, secret)

if orgID != -1 {
_, err = client.OrgSecretCreate(orgID, secret)
return err
}

_, err = client.SecretCreate(repoID, secret)
return err
}
Expand Down
11 changes: 4 additions & 7 deletions cli/secret/secret_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ var secretInfoCmd = &cli.Command{
Name: "global",
Usage: "global secret",
},
&cli.StringFlag{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
common.OrgFlag,
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Expand All @@ -44,7 +41,7 @@ func secretInfo(c *cli.Context) error {
return err
}

global, owner, repoID, err := parseTargetArgs(client, c)
global, orgID, repoID, err := parseTargetArgs(client, c)
if err != nil {
return err
}
Expand All @@ -55,8 +52,8 @@ func secretInfo(c *cli.Context) error {
if err != nil {
return err
}
} else if owner != "" {
secret, err = client.OrgSecret(owner, secretName)
} else if orgID != -1 {
secret, err = client.OrgSecret(orgID, secretName)
if err != nil {
return err
}
Expand Down
11 changes: 4 additions & 7 deletions cli/secret/secret_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ var secretListCmd = &cli.Command{
Name: "global",
Usage: "global secret",
},
&cli.StringFlag{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
common.OrgFlag,
common.RepoFlag,
common.FormatFlag(tmplSecretList, true),
),
Expand All @@ -39,7 +36,7 @@ func secretList(c *cli.Context) error {
return err
}

global, owner, repoID, err := parseTargetArgs(client, c)
global, orgID, repoID, err := parseTargetArgs(client, c)
if err != nil {
return err
}
Expand All @@ -50,8 +47,8 @@ func secretList(c *cli.Context) error {
if err != nil {
return err
}
} else if owner != "" {
list, err = client.OrgSecretList(owner)
} else if orgID != -1 {
list, err = client.OrgSecretList(orgID)
if err != nil {
return err
}
Expand Down
11 changes: 4 additions & 7 deletions cli/secret/secret_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ var secretDeleteCmd = &cli.Command{
Name: "global",
Usage: "global secret",
},
&cli.StringFlag{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
common.OrgFlag,
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Expand All @@ -37,16 +34,16 @@ func secretDelete(c *cli.Context) error {
return err
}

global, owner, repoID, err := parseTargetArgs(client, c)
global, orgID, repoID, err := parseTargetArgs(client, c)
if err != nil {
return err
}

if global {
return client.GlobalSecretDelete(secretName)
}
if owner != "" {
return client.OrgSecretDelete(owner, secretName)
if orgID != -1 {
return client.OrgSecretDelete(orgID, secretName)
}
return client.SecretDelete(repoID, secretName)
}
11 changes: 4 additions & 7 deletions cli/secret/secret_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ var secretUpdateCmd = &cli.Command{
Name: "global",
Usage: "global secret",
},
&cli.StringFlag{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
common.OrgFlag,
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Expand Down Expand Up @@ -71,7 +68,7 @@ func secretUpdate(c *cli.Context) error {
secret.Value = string(out)
}

global, owner, repoID, err := parseTargetArgs(client, c)
global, orgID, repoID, err := parseTargetArgs(client, c)
if err != nil {
return err
}
Expand All @@ -80,8 +77,8 @@ func secretUpdate(c *cli.Context) error {
_, err = client.GlobalSecretUpdate(secret)
return err
}
if owner != "" {
_, err = client.OrgSecretUpdate(owner, secret)
if orgID != -1 {
_, err = client.OrgSecretUpdate(orgID, secret)
return err
}
_, err = client.SecretUpdate(repoID, secret)
Expand Down
Loading

0 comments on commit e5d5ec8

Please sign in to comment.