Skip to content

Commit

Permalink
Better error message when org doesn't exist
Browse files Browse the repository at this point in the history
* Produce a better error message when the destination org doesn't exist.
* Support pushing to the authenticated user's account

**Before**
`error creating github repository `synced-actions/setup-node`: error creating repository: POST http://my-ghes-hostname/api/v3/orgs/synced-actions/repos: 404 Not Found []`

**After**
`error creating github repository `synced-actions/setup-node`: Organization `synced-actions` doesn't exist at http://my-ghes-hostname. You must create it first.`

#1
  • Loading branch information
hashtagchris committed Sep 22, 2020
1 parent 9c28fe3 commit 4e5bb47
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ When there are machines which have access to both the public internet and the GH

```
actions-sync sync \
--cache-dir "tmp/cache" \
--cache-dir "/tmp/cache" \
--destination-token "token" \
--destination-url "www.example.com" \
--destination-url "https://www.example.com" \
--repo-name actions/setup-node
```

Expand Down Expand Up @@ -97,7 +97,7 @@ When no machine has access to both the public internet and the GHES instance:
bin/actions-sync push \
--cache-dir "/tmp/cache" \
--destination-token "token" \
--destination-url "http://www.example.com"
--destination-url "https://www.example.com"
```


Expand Down
30 changes: 26 additions & 4 deletions src/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"strings"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
Expand Down Expand Up @@ -102,7 +103,7 @@ func PushWithGitImpl(ctx context.Context, flags *PushFlags, repoName string, ghC
}

fmt.Printf("syncing `%s`\n", nwo)
ghRepo, err := getOrCreateGitHubRepo(ctx, ghClient, bareRepoName, ownerName)
ghRepo, err := getOrCreateGitHubRepo(ctx, flags, ghClient, bareRepoName, ownerName)
if err != nil {
return errors.Wrapf(err, "error creating github repository `%s`", nwo)
}
Expand All @@ -114,17 +115,38 @@ func PushWithGitImpl(ctx context.Context, flags *PushFlags, repoName string, ghC
return nil
}

func getOrCreateGitHubRepo(ctx context.Context, client *github.Client, repoName, orgName string) (*github.Repository, error) {
func getOrCreateGitHubRepo(ctx context.Context, flags *PushFlags, client *github.Client, repoName, ownerName string) (*github.Repository, error) {
repo := &github.Repository{
Name: github.String(repoName),
HasIssues: github.Bool(false),
HasWiki: github.Bool(false),
HasPages: github.Bool(false),
HasProjects: github.Bool(false),
}

orgName := ownerName

// 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)
}
}
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, _, err = client.Repositories.Get(ctx, orgName, repoName)
ghRepo, _, err = client.Repositories.Get(ctx, ownerName, repoName)
}
if err != nil {
return nil, errors.Wrap(err, "error creating repository")
Expand All @@ -138,7 +160,7 @@ func getOrCreateGitHubRepo(ctx context.Context, client *github.Client, repoName,
func syncWithCachedRepository(ctx context.Context, flags *PushFlags, ghRepo *github.Repository, repoDir string, gitimpl GitImplementation) error {
gitRepo, err := gitimpl.NewGitRepository(repoDir)
if err != nil {
return errors.Wrapf(err, "error opening git repository %s", flags.CacheDir)
return errors.Wrapf(err, "error opening git repository %s", repoDir)
}
_ = gitRepo.DeleteRemote("ghes")
remote, err := gitRepo.CreateRemote(&config.RemoteConfig{
Expand Down
12 changes: 12 additions & 0 deletions test/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ func main() {

r := mux.NewRouter()
r.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {})

r.HandleFunc("/api/v3/orgs/{org}", func(w http.ResponseWriter, r *http.Request) {
orgName := mux.Vars(r)["org"]

org := github.Organization{Login: &orgName}
b, _ := json.Marshal(org)
_, err := w.Write(b)
if err != nil {
panic(err)
}
})

r.HandleFunc("/api/v3/orgs/{org}/repos", func(w http.ResponseWriter, r *http.Request) {
orgName := mux.Vars(r)["org"]
b, err := ioutil.ReadAll(r.Body)
Expand Down

0 comments on commit 4e5bb47

Please sign in to comment.