Create org if necessary

This commit is contained in:
Chris Sidi
2020-09-22 13:52:54 -04:00
parent 4e5bb47042
commit d39e48f007
4 changed files with 193 additions and 23 deletions

View File

@@ -103,7 +103,7 @@ func PushWithGitImpl(ctx context.Context, flags *PushFlags, repoName string, ghC
}
fmt.Printf("syncing `%s`\n", nwo)
ghRepo, err := getOrCreateGitHubRepo(ctx, flags, ghClient, bareRepoName, ownerName)
ghRepo, err := getOrCreateGitHubRepo(ctx, ghClient, bareRepoName, ownerName)
if err != nil {
return errors.Wrapf(err, "error creating github repository `%s`", nwo)
}
@@ -115,7 +115,7 @@ func PushWithGitImpl(ctx context.Context, flags *PushFlags, repoName string, ghC
return nil
}
func getOrCreateGitHubRepo(ctx context.Context, flags *PushFlags, client *github.Client, repoName, ownerName string) (*github.Repository, error) {
func getOrCreateGitHubRepo(ctx context.Context, client *github.Client, repoName, ownerName string) (*github.Repository, error) {
repo := &github.Repository{
Name: github.String(repoName),
HasIssues: github.Bool(false),
@@ -124,32 +124,36 @@ func getOrCreateGitHubRepo(ctx context.Context, flags *PushFlags, client *github
HasProjects: github.Bool(false),
}
orgName := ownerName
currentUser, _, err := client.Users.Get(ctx, "")
if err != nil {
return nil, errors.Wrap(err, "error retrieving authenticated user")
}
if currentUser == nil || currentUser.Login == nil {
return nil, errors.New("error retrieving authenticated user's login name")
}
// Confirm the org exists
_, resp, err := client.Organizations.Get(ctx, orgName)
if resp != nil && resp.StatusCode == 404 {
// Check if the destination owner matches the authenticated user. (best effort)
currentUser, _, _ := client.Users.Get(ctx, "")
if currentUser != nil && strings.EqualFold(*currentUser.Login, ownerName) {
// create the new repo under the authenticated user's account.
orgName = ""
err = nil
} else {
return nil, errors.Errorf("Organization `%s` doesn't exist at %s. You must create it first.", ownerName, flags.BaseURL)
// check if the owner refers to the authenticated user or an organization.
var createRepoOrgName string
if strings.EqualFold(*currentUser.Login, ownerName) {
// we'll create the repo under the authenticated user's account.
createRepoOrgName = ""
} else {
// ensure the org exists.
createRepoOrgName = ownerName
_, err := getOrCreateGitHubOrg(ctx, client, ownerName, *currentUser.Login)
if err != nil {
return nil, err
}
}
if err != nil {
return nil, errors.Wrapf(err, "error retrieving organization %s", ownerName)
}
// Create the repo if necessary
ghRepo, resp, err := client.Repositories.Create(ctx, orgName, repo)
if resp != nil && resp.StatusCode == 422 {
ghRepo, resp, err := client.Repositories.Create(ctx, createRepoOrgName, repo)
if err == nil {
fmt.Printf("Created repo `%s/%s`\n", ownerName, repoName)
} else if resp != nil && resp.StatusCode == 422 {
ghRepo, _, err = client.Repositories.Get(ctx, ownerName, repoName)
}
if err != nil {
return nil, errors.Wrap(err, "error creating repository")
return nil, errors.Wrapf(err, "error creating repository %s/%s", ownerName, repoName)
}
if ghRepo == nil {
return nil, errors.New("error repository is nil")
@@ -157,6 +161,27 @@ func getOrCreateGitHubRepo(ctx context.Context, flags *PushFlags, client *github
return ghRepo, nil
}
func getOrCreateGitHubOrg(ctx context.Context, client *github.Client, orgName, admin string) (*github.Organization, error) {
org := &github.Organization{Login: &orgName}
var getErr error
ghOrg, _, createErr := client.Admin.CreateOrg(ctx, org, admin)
if createErr == nil {
fmt.Printf("Created organization `%s` (admin: %s)\n", orgName, admin)
} else {
// Regardless of why create failed, see if we can retrieve the org
ghOrg, _, getErr = client.Organizations.Get(ctx, orgName)
}
if createErr != nil && getErr != nil {
return nil, errors.Wrapf(createErr, "error creating organization %s", orgName)
}
if ghOrg == nil {
return nil, errors.New("error organization is nil")
}
return ghOrg, nil
}
func syncWithCachedRepository(ctx context.Context, flags *PushFlags, ghRepo *github.Repository, repoDir string, gitimpl GitImplementation) error {
gitRepo, err := gitimpl.NewGitRepository(repoDir)
if err != nil {