-
Notifications
You must be signed in to change notification settings - Fork 52
/
create-git-repos.go
192 lines (161 loc) · 5.35 KB
/
create-git-repos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
}