From cbbe0d523ce70f6819e7c2767356ce0781f8c5bd Mon Sep 17 00:00:00 2001 From: Shawn Hartsell Date: Mon, 22 Apr 2024 17:18:38 -0500 Subject: [PATCH] fixing refactor again --- src/push.go | 61 +++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/push.go b/src/push.go index e5170e5..89dd292 100644 --- a/src/push.go +++ b/src/push.go @@ -205,40 +205,41 @@ func getOrCreateGitHubRepo(ctx context.Context, client *github.Client, repoName, } // check if repository already exists - ghRepo, resp, err := client.Repositories.Get(ctx, ownerName, repoName) + var ghRepo *github.Repository + var resp *github.Response + ghRepo, resp, err = client.Repositories.Get(ctx, ownerName, repoName) if err != nil { + // repo not existing yet - try to create + if resp != nil && resp.StatusCode == 404 { + fmt.Printf("repo `%s/%s` doesn't exist, attempting to create\n", ownerName, repoName) + + visibility := github.String("public") + if isAE { + visibility = github.String("internal") + } + + repo := &github.Repository{ + Name: github.String(repoName), + HasIssues: github.Bool(false), + HasWiki: github.Bool(false), + HasPages: github.Bool(false), + HasProjects: github.Bool(false), + Visibility: visibility, + } + + ghRepo, _, err = client.Repositories.Create(ctx, createRepoOrgName, repo) + if err != nil { + return nil, errors.Wrapf(err, "error creating repository %s/%s", ownerName, repoName) + } + + fmt.Printf("created repo `%s/%s`\n", ownerName, repoName) + return ghRepo, nil + } + return nil, errors.Wrapf(err, "error creating repository %s/%s", ownerName, repoName) } - fmt.Printf("Existing repo `%s/%s`\n", ownerName, repoName) - - if resp != nil && resp.StatusCode == 404 { - // repo not existing yet - try to create - visibility := github.String("public") - if isAE { - visibility = github.String("internal") - } - repo := &github.Repository{ - Name: github.String(repoName), - HasIssues: github.Bool(false), - HasWiki: github.Bool(false), - HasPages: github.Bool(false), - HasProjects: github.Bool(false), - Visibility: visibility, - } - - ghRepo, _, err = client.Repositories.Create(ctx, createRepoOrgName, repo) - if err == nil { - fmt.Printf("Created repo `%s/%s`\n", ownerName, repoName) - } else { - return nil, errors.Wrapf(err, "error creating repository %s/%s", ownerName, repoName) - } - } - - if ghRepo == nil { - return nil, errors.New("error repository is nil") - } - + fmt.Printf("found existing repo `%s/%s`\n", ownerName, repoName) return ghRepo, nil }