Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZERO126 adding repo creation and initial commit (#141)
* ZERO126 adding repo creation and initial commit * ZERO126 moved files, minor cleanup
- Loading branch information
Showing
3 changed files
with
208 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package vcs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/machinebox/graphql" | ||
"os/exec" | ||
) | ||
|
||
// this is being developed with the following assumptions: | ||
// 1. create.go will handle parsing the repo names and github credentials and pass them into initializeRepostiories(). | ||
// 2. if organizationName is set, create an org owned repo. if not, create a personal repo. | ||
|
||
// takes a list of subdirectories containing modules to create a repository and do initial commit for | ||
func InitializeRepositories(moduleDirs []string, remoteRepository string, organizationName string, githubApiKey string) { | ||
|
||
for _, moduleDir := range moduleDirs { | ||
// if organizationName is not set, create a personal github repo, else create an org owned repo. | ||
if organizationName == "" { | ||
if err := createPersonalRepository(moduleDir, githubApiKey); err != nil { | ||
fmt.Printf("error creating repository: %s\n", err.Error()) | ||
continue | ||
} | ||
} else { | ||
if err := createOrganizationOwnedRepository(moduleDir, githubApiKey, organizationName); err != nil { | ||
fmt.Printf("error creating repository: %s\n", err.Error()) | ||
continue | ||
} | ||
} | ||
|
||
if err := doInitialCommit(moduleDir, remoteRepository); err != nil { | ||
fmt.Printf("error initializing repository: %s\n", err.Error()) | ||
} | ||
} | ||
|
||
} | ||
|
||
const createPersonalRepositoryMutation = `mutation ($repoName: String!, $repoDescription: String!) { | ||
createRepository( | ||
input: { | ||
name:$repoName, | ||
visibility: PRIVATE, | ||
description: $repoDescription | ||
}) | ||
{ | ||
clientMutationId | ||
} | ||
}` | ||
|
||
func createPersonalRepository(moduleDir string, githubApiKey string) error { | ||
|
||
fmt.Printf("Creating repository for module: %s\n", moduleDir) | ||
|
||
// create client and mutation | ||
client := graphql.NewClient("https://api.github.com/graphql") | ||
req := graphql.NewRequest(createPersonalRepositoryMutation) | ||
req.Var("repoName", moduleDir) | ||
req.Var("repoDescription", fmt.Sprintf("Repository for %s", moduleDir)) | ||
|
||
// add auth token | ||
var bearer = fmt.Sprintf("Bearer %s", githubApiKey) | ||
req.Header.Add("Authorization", bearer) | ||
|
||
ctx := context.Background() | ||
if err := client.Run(ctx, req, nil); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Repository successfully created for module: %s\n", moduleDir) | ||
|
||
return nil | ||
} | ||
|
||
const createOrganizationRepositoryMutation = `mutation ($repoName: String!, $repoDescription: String!, $ownerId: String!) { | ||
createRepository( | ||
input: { | ||
name:$repoName, | ||
visibility: PRIVATE, | ||
description: $repoDescription | ||
ownerId: $ownerId | ||
}) | ||
{ | ||
clientMutationId | ||
} | ||
}` | ||
|
||
const getOrganizationQuery = `query ($organizationName: String!) { | ||
organization(login: $organizationName) { | ||
id | ||
} | ||
}` | ||
|
||
type organizationQueryResponse struct { | ||
Organization struct { | ||
Id string | ||
} | ||
} | ||
|
||
func createOrganizationOwnedRepository(moduleDir string, githubApiKey string, organizationName string) error { | ||
|
||
fmt.Printf("Creating org owned repository for module: %s\n", moduleDir) | ||
|
||
// create client and organization query | ||
var bearer = fmt.Sprintf("Bearer %s", githubApiKey) | ||
client := graphql.NewClient("https://api.github.com/graphql") | ||
orgIdReq := graphql.NewRequest(getOrganizationQuery) | ||
orgIdReq.Var("organizationName", organizationName) | ||
orgIdReq.Header.Add("Authorization", bearer) | ||
|
||
var orgIdResp organizationQueryResponse | ||
ctx := context.Background() | ||
if err := client.Run(ctx, orgIdReq, &orgIdResp); err != nil { | ||
return err | ||
} | ||
organizationId := orgIdResp.Organization.Id | ||
|
||
// create mutation and run it | ||
req := graphql.NewRequest(createOrganizationRepositoryMutation) | ||
req.Var("repoName", moduleDir) | ||
req.Var("repoDescription", fmt.Sprintf("Repository for %s", moduleDir)) | ||
req.Var("ownerId", organizationId) | ||
req.Header.Add("Authorization", bearer) | ||
if err := client.Run(ctx, req, nil); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Repository successfully created for module: %s\n", moduleDir) | ||
|
||
return nil | ||
} | ||
|
||
type InitialCommands struct { | ||
description string | ||
command string | ||
args []string | ||
} | ||
|
||
// do initial commit to a repository | ||
func doInitialCommit(moduleDir string, remoteRepository string) error { | ||
fmt.Printf("Initializing repository for module: %s\n", moduleDir) | ||
|
||
remoteOrigin := fmt.Sprintf("%s/%s.git", remoteRepository, moduleDir) | ||
commands := []InitialCommands{ | ||
{ | ||
description: "git init", | ||
command: "git", | ||
args: []string{"init"}, | ||
}, | ||
{ | ||
description: "git add .", | ||
command: "git", | ||
args: []string{"add", "."}, | ||
}, | ||
{ | ||
description: "git commit -m \"initial commit by zero\"", | ||
command: "git", | ||
args: []string{"commit", "-m", "initial commit by zero"}, | ||
}, | ||
{ | ||
description: fmt.Sprintf("git remote add origin %s", remoteOrigin), | ||
command: "git", | ||
args: []string{"remote", "add", "origin", remoteOrigin}, | ||
}, | ||
{ | ||
description: "git push -u origin master", | ||
command: "git", | ||
args: []string{"push", "-u", "origin", "master"}, | ||
}, | ||
} | ||
|
||
for _, command := range commands { | ||
fmt.Printf(">> %s\n", command.description) | ||
|
||
cmd := exec.Command(command.command, command.args...) | ||
cmd.Dir = "./" + moduleDir | ||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
fmt.Printf("ERROR: failed to run %s: %s\n", command.description, err.Error()) | ||
// this is a partial failure. some commands may have exec'ed successfully. | ||
break | ||
} else { | ||
response := string(out) | ||
if len(response) > 0 { | ||
fmt.Println(response) | ||
} | ||
} | ||
} | ||
|
||
fmt.Printf("Repository successfully initialized for module: %s\n", moduleDir) | ||
|
||
return nil | ||
} |