Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign GitHub teams during bootstrap #41

Merged
merged 1 commit into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion cmd/tk/bootstrap_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ the bootstrap command will perform an upgrade if needed.`,
# Run bootstrap for a private repo owned by a GitHub organization
bootstrap github --owner=<organization> --repository=<repo name>

# Run bootstrap for a private repo and assign organization teams to it
bootstrap github --owner=<organization> --repository=<repo name> --team=<team1 slug> --team=<team2 slug>

# Run bootstrap for a repository path
bootstrap github --owner=<organization> --repository=<repo name> --path=dev-cluster

Expand All @@ -63,6 +66,7 @@ var (
ghPrivate bool
ghHostname string
ghPath string
ghTeams []string
)

const (
Expand All @@ -72,11 +76,13 @@ const (
ghSourceManifest = "toolkit-source.yaml"
ghKustomizationManifest = "toolkit-kustomization.yaml"
ghDefaultHostname = "github.com"
ghDefaultPermission = "maintain"
)

func init() {
bootstrapGitHubCmd.Flags().StringVar(&ghOwner, "owner", "", "GitHub user or organization name")
bootstrapGitHubCmd.Flags().StringVar(&ghRepository, "repository", "", "GitHub repository name")
bootstrapGitHubCmd.Flags().StringArrayVar(&ghTeams, "team", []string{}, "GitHub team to be given maintainer access")
bootstrapGitHubCmd.Flags().BoolVar(&ghPersonal, "personal", false, "is personal repository")
bootstrapGitHubCmd.Flags().BoolVar(&ghPrivate, "private", true, "is private repository")
bootstrapGitHubCmd.Flags().DurationVar(&ghInterval, "interval", time.Minute, "sync interval")
Expand Down Expand Up @@ -118,6 +124,19 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
return err
}

withErrors := false
// add teams to org repository
if !ghPersonal {
for _, team := range ghTeams {
if err := addGitHubTeam(ctx, ghHostname, ghOwner, ghRepository, ghToken, team, ghDefaultPermission); err != nil {
logFailure(err.Error())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this to return as soon as it runs into an error, as the current code could result in no-one getting granted access to the repository while the bootstrap continues.

Copy link
Member Author

@stefanprodan stefanprodan Jun 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that was the intention as you can't make the operations transactional I opted for best-effort. Note that the user that runs the command has full access to the repo even if teams don't.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. In that case I would expect a partly successful bootstrap run to return a non-zero exit code as an indicator.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

withErrors = true
} else {
logSuccess("%s team access granted", team)
}
}
}

// clone repository and checkout the master branch
repo, err := checkoutGitHubRepository(ctx, ghURL, ghBranch, ghToken, tmpDir)
if err != nil {
Expand Down Expand Up @@ -221,6 +240,10 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
}
}

if withErrors {
return fmt.Errorf("bootstrap completed with errors")
}

logSuccess("bootstrap finished")
return nil
}
Expand Down Expand Up @@ -275,6 +298,37 @@ func createGitHubRepository(ctx context.Context, hostname, owner, name, token st
return nil
}

func addGitHubTeam(ctx context.Context, hostname, owner, repository, token string, teamSlug, permission string) error {
gh, err := makeGitHubClient(hostname, token)
if err != nil {
return err
}

// check team exists
_, _, err = gh.Teams.GetTeamBySlug(ctx, owner, teamSlug)
if err != nil {
return fmt.Errorf("github get team %s error: %w", teamSlug, err)
}

// check if team is assigned to the repo
_, resp, err := gh.Teams.IsTeamRepoBySlug(ctx, owner, teamSlug, owner, repository)
if resp == nil && err != nil {
return fmt.Errorf("github is team %s error: %w", teamSlug, err)
}

// add team to the repo
if resp.StatusCode == 404 {
_, err = gh.Teams.AddTeamRepoBySlug(ctx, owner, teamSlug, owner, repository, &github.TeamAddTeamRepoOptions{
Permission: permission,
})
if err != nil {
return fmt.Errorf("github add team %s error: %w", teamSlug, err)
}
}

return nil
}

func checkoutGitHubRepository(ctx context.Context, url, branch, token, path string) (*git.Repository, error) {
auth := &http.BasicAuth{
Username: "git",
Expand Down Expand Up @@ -423,7 +477,7 @@ func generateGitHubKustomization(url, name, namespace, targetPath, tmpDir string
Interval: metav1.Duration{
Duration: 10 * time.Minute,
},
Path: "./" + strings.TrimPrefix("./", targetPath),
Path: fmt.Sprintf("./%s", strings.TrimPrefix(targetPath, "./")),
Prune: true,
SourceRef: corev1.TypedLocalObjectReference{
APIGroup: &emptyAPIGroup,
Expand Down
6 changes: 5 additions & 1 deletion cmd/tk/get_kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func getKsCmdRun(cmd *cobra.Command, args []string) error {
for _, condition := range kustomization.Status.Conditions {
if condition.Type == kustomizev1.ReadyCondition {
if condition.Status != corev1.ConditionFalse {
logSuccess("%s last applied revision %s", kustomization.GetName(), kustomization.Status.LastAppliedRevision)
if kustomization.Status.LastAppliedRevision != "" {
logSuccess("%s last applied revision %s", kustomization.GetName(), kustomization.Status.LastAppliedRevision)
} else {
logSuccess("%s reconciling", kustomization.GetName())
}
} else {
logFailure("%s %s", kustomization.GetName(), condition.Message)
}
Expand Down