InitializeRepository Creates and initializes a github repository for the given url repositoryUrl is expected to be in the format "github.com/{ownerName}/{repositoryName}"
(repositoryUrl string, githubApiKey string)
| 13 | // InitializeRepository Creates and initializes a github repository for the given url |
| 14 | // repositoryUrl is expected to be in the format "github.com/{ownerName}/{repositoryName}" |
| 15 | func InitializeRepository(repositoryUrl string, githubApiKey string) { |
| 16 | |
| 17 | var err error |
| 18 | ownerName, repositoryName, err := parseRepositoryUrl(repositoryUrl) |
| 19 | if err != nil { |
| 20 | fmt.Printf("error creating repository: %s\n", err.Error()) |
| 21 | return |
| 22 | } |
| 23 | flog.Debugf("Initialized repo: %s/%s", ownerName, repositoryName) |
| 24 | |
| 25 | isOrgOwned, ownerId, err := isOrganizationOwned(ownerName, githubApiKey) |
| 26 | if err != nil { |
| 27 | fmt.Printf("error creating repository: %s\n", err.Error()) |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | if isOrgOwned { |
| 32 | r := graphql.NewRequest(createOrganizationRepositoryMutation) |
| 33 | r.Var("repoName", repositoryName) |
| 34 | r.Var("repoDescription", fmt.Sprintf("Repository for %s", repositoryName)) |
| 35 | r.Var("ownerId", ownerId) |
| 36 | r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", githubApiKey)) |
| 37 | |
| 38 | if err := createRepository(r); err != nil { |
| 39 | fmt.Printf("error creating repository: %s\n", err.Error()) |
| 40 | return |
| 41 | } |
| 42 | } else { |
| 43 | r := graphql.NewRequest(createPersonalRepositoryMutation) |
| 44 | r.Var("repoName", repositoryName) |
| 45 | r.Var("repoDescription", fmt.Sprintf("Repository for %s", repositoryName)) |
| 46 | r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", githubApiKey)) |
| 47 | |
| 48 | if err := createRepository(r); err != nil { |
| 49 | fmt.Printf("error creating repository: %s\n", err.Error()) |
| 50 | return |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if err := doInitialCommit(ownerName, repositoryName); err != nil { |
| 55 | fmt.Printf("error initializing repository: %s\n", err.Error()) |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | flog.Infof(":check_mark_button: Repository created: %s", repositoryUrl) |
| 60 | } |
| 61 | |
| 62 | // parseRepositoryUrl extracts the owner name and repository name from a repository url. |
| 63 | // repositoryUrl is expected to be in the format "github.com/{ownerName}/{repositoryName}" |
no test coverage detected