@@ -108,6 +108,10 @@ func TestRepoCreate(t *testing.T) {
108108 }
109109 }
110110
111+ if len (http .Requests ) != 1 {
112+ t .Fatalf ("expected 1 HTTP request, got %d" , len (http .Requests ))
113+ }
114+
111115 bodyBytes , _ := ioutil .ReadAll (http .Requests [0 ].Body )
112116 json .Unmarshal (bodyBytes , & reqBody )
113117 if repoName := reqBody .Variables .Input ["name" ].(string ); repoName != "REPO" {
@@ -116,6 +120,138 @@ func TestRepoCreate(t *testing.T) {
116120 if repoVisibility := reqBody .Variables .Input ["visibility" ].(string ); repoVisibility != "PRIVATE" {
117121 t .Errorf ("expected %q, got %q" , "PRIVATE" , repoVisibility )
118122 }
123+ if _ , ownerSet := reqBody .Variables .Input ["ownerId" ]; ownerSet {
124+ t .Error ("expected ownerId not to be set" )
125+ }
126+ }
127+
128+ func TestRepoCreate_org (t * testing.T ) {
129+ ctx := context .NewBlank ()
130+ ctx .SetBranch ("master" )
131+ initContext = func () context.Context {
132+ return ctx
133+ }
134+
135+ http := initFakeHTTP ()
136+ http .StubResponse (200 , bytes .NewBufferString (`
137+ { "node_id": "ORGID"
138+ }
139+ ` ))
140+ http .StubResponse (200 , bytes .NewBufferString (`
141+ { "data": { "createRepository": {
142+ "repository": {
143+ "id": "REPOID",
144+ "url": "https://github.com/ORG/REPO"
145+ }
146+ } } }
147+ ` ))
148+
149+ var seenCmd * exec.Cmd
150+ restoreCmd := utils .SetPrepareCmd (func (cmd * exec.Cmd ) utils.Runnable {
151+ seenCmd = cmd
152+ return & outputStub {}
153+ })
154+ defer restoreCmd ()
155+
156+ output , err := RunCommand (repoCreateCmd , "repo create ORG/REPO" )
157+ if err != nil {
158+ t .Errorf ("error running command `repo create`: %v" , err )
159+ }
160+
161+ eq (t , output .String (), "https://github.com/ORG/REPO\n " )
162+ eq (t , output .Stderr (), "" )
163+
164+ if seenCmd == nil {
165+ t .Fatal ("expected a command to run" )
166+ }
167+ eq (t , strings .Join (seenCmd .Args , " " ), "git remote add origin https://github.com/ORG/REPO.git" )
168+
169+ var reqBody struct {
170+ Query string
171+ Variables struct {
172+ Input map [string ]interface {}
173+ }
174+ }
175+
176+ if len (http .Requests ) != 2 {
177+ t .Fatalf ("expected 2 HTTP requests, got %d" , len (http .Requests ))
178+ }
179+
180+ eq (t , http .Requests [0 ].URL .Path , "/users/ORG" )
181+
182+ bodyBytes , _ := ioutil .ReadAll (http .Requests [1 ].Body )
183+ json .Unmarshal (bodyBytes , & reqBody )
184+ if orgID := reqBody .Variables .Input ["ownerId" ].(string ); orgID != "ORGID" {
185+ t .Errorf ("expected %q, got %q" , "ORGID" , orgID )
186+ }
187+ if _ , teamSet := reqBody .Variables .Input ["teamId" ]; teamSet {
188+ t .Error ("expected teamId not to be set" )
189+ }
190+ }
191+
192+ func TestRepoCreate_orgWithTeam (t * testing.T ) {
193+ ctx := context .NewBlank ()
194+ ctx .SetBranch ("master" )
195+ initContext = func () context.Context {
196+ return ctx
197+ }
198+
199+ http := initFakeHTTP ()
200+ http .StubResponse (200 , bytes .NewBufferString (`
201+ { "node_id": "TEAMID",
202+ "organization": { "node_id": "ORGID" }
203+ }
204+ ` ))
205+ http .StubResponse (200 , bytes .NewBufferString (`
206+ { "data": { "createRepository": {
207+ "repository": {
208+ "id": "REPOID",
209+ "url": "https://github.com/ORG/REPO"
210+ }
211+ } } }
212+ ` ))
213+
214+ var seenCmd * exec.Cmd
215+ restoreCmd := utils .SetPrepareCmd (func (cmd * exec.Cmd ) utils.Runnable {
216+ seenCmd = cmd
217+ return & outputStub {}
218+ })
219+ defer restoreCmd ()
220+
221+ output , err := RunCommand (repoCreateCmd , "repo create ORG/REPO --team monkeys" )
222+ if err != nil {
223+ t .Errorf ("error running command `repo create`: %v" , err )
224+ }
225+
226+ eq (t , output .String (), "https://github.com/ORG/REPO\n " )
227+ eq (t , output .Stderr (), "" )
228+
229+ if seenCmd == nil {
230+ t .Fatal ("expected a command to run" )
231+ }
232+ eq (t , strings .Join (seenCmd .Args , " " ), "git remote add origin https://github.com/ORG/REPO.git" )
233+
234+ var reqBody struct {
235+ Query string
236+ Variables struct {
237+ Input map [string ]interface {}
238+ }
239+ }
240+
241+ if len (http .Requests ) != 2 {
242+ t .Fatalf ("expected 2 HTTP requests, got %d" , len (http .Requests ))
243+ }
244+
245+ eq (t , http .Requests [0 ].URL .Path , "/orgs/ORG/teams/monkeys" )
246+
247+ bodyBytes , _ := ioutil .ReadAll (http .Requests [1 ].Body )
248+ json .Unmarshal (bodyBytes , & reqBody )
249+ if orgID := reqBody .Variables .Input ["ownerId" ].(string ); orgID != "ORGID" {
250+ t .Errorf ("expected %q, got %q" , "ORGID" , orgID )
251+ }
252+ if teamID := reqBody .Variables .Input ["teamId" ].(string ); teamID != "TEAMID" {
253+ t .Errorf ("expected %q, got %q" , "TEAMID" , teamID )
254+ }
119255}
120256
121257func TestRepoView (t * testing.T ) {
0 commit comments