|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/gomicro/concord/client" |
| 10 | + gh_pb "github.com/gomicro/concord/github/v1" |
| 11 | + "github.com/gomicro/concord/manifest" |
| 12 | + "github.com/gomicro/concord/report" |
| 13 | + "github.com/google/go-github/v56/github" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +func init() { |
| 18 | + applyCmd.AddCommand(NewApplyOrgCmd(os.Stdout)) |
| 19 | +} |
| 20 | + |
| 21 | +func NewApplyOrgCmd(out io.Writer) *cobra.Command { |
| 22 | + cmd := &cobra.Command{ |
| 23 | + Use: "org", |
| 24 | + Short: "Apply specific org level configuration", |
| 25 | + Long: `Apply specific org level configuration against github`, |
| 26 | + RunE: applyOrgRun, |
| 27 | + } |
| 28 | + |
| 29 | + cmd.SetOut(out) |
| 30 | + |
| 31 | + return cmd |
| 32 | +} |
| 33 | + |
| 34 | +func applyOrgRun(cmd *cobra.Command, args []string) error { |
| 35 | + file := cmd.Flags().Lookup("file").Value.String() |
| 36 | + cmd.SetContext(manifest.WithManifest(cmd.Context(), file)) |
| 37 | + |
| 38 | + dry := strings.EqualFold(cmd.Flags().Lookup("dry").Value.String(), "true") |
| 39 | + |
| 40 | + ctx := cmd.Context() |
| 41 | + |
| 42 | + org, err := manifest.OrgFromContext(ctx) |
| 43 | + if err != nil { |
| 44 | + return handleError(cmd, err) |
| 45 | + } |
| 46 | + |
| 47 | + clt, err := client.ClientFromContext(ctx) |
| 48 | + if err != nil { |
| 49 | + return handleError(cmd, err) |
| 50 | + } |
| 51 | + |
| 52 | + exists, err := clt.OrgExists(ctx, org.Name) |
| 53 | + if err != nil { |
| 54 | + return handleError(cmd, err) |
| 55 | + } |
| 56 | + |
| 57 | + if !exists { |
| 58 | + return handleError(cmd, errors.New("organization does not exist")) |
| 59 | + } |
| 60 | + |
| 61 | + report.PrintHeader("Org") |
| 62 | + report.Println() |
| 63 | + |
| 64 | + err = orgRun(cmd, args) |
| 65 | + if err != nil { |
| 66 | + return handleError(cmd, err) |
| 67 | + } |
| 68 | + |
| 69 | + if !dry { |
| 70 | + if !confirm(cmd, "Apply changes? (y/n): ") { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + err = clt.Apply() |
| 75 | + if err != nil { |
| 76 | + return handleError(cmd, err) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func orgRun(cmd *cobra.Command, args []string) error { |
| 84 | + ctx := cmd.Context() |
| 85 | + |
| 86 | + org, err := manifest.OrgFromContext(ctx) |
| 87 | + if err != nil { |
| 88 | + return handleError(cmd, err) |
| 89 | + } |
| 90 | + |
| 91 | + clt, err := client.ClientFromContext(ctx) |
| 92 | + if err != nil { |
| 93 | + return handleError(cmd, err) |
| 94 | + } |
| 95 | + |
| 96 | + report.Println() |
| 97 | + report.PrintHeader("Permissions") |
| 98 | + report.Println() |
| 99 | + |
| 100 | + err = clt.SetOrgPrivileges(ctx, org.Name, buildOrgState(org)) |
| 101 | + if err != nil { |
| 102 | + return handleError(cmd, err) |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func buildOrgState(org *gh_pb.Organization) *github.Organization { |
| 109 | + state := &github.Organization{} |
| 110 | + |
| 111 | + if org.Permissions != nil { |
| 112 | + if org.Permissions.BasePermissions != nil { |
| 113 | + state.DefaultRepoPermission = org.Permissions.BasePermissions |
| 114 | + } |
| 115 | + |
| 116 | + if org.Permissions.CreatePrivateRepos != nil { |
| 117 | + state.MembersCanCreatePrivateRepos = org.Permissions.CreatePrivateRepos |
| 118 | + } |
| 119 | + |
| 120 | + if org.Permissions.CreatePublicRepos != nil { |
| 121 | + state.MembersCanCreatePublicRepos = org.Permissions.CreatePublicRepos |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return state |
| 126 | +} |
0 commit comments