Skip to content

Commit f686636

Browse files
authored
validate command: ability to create access token for first admin user (#358)
* validate command: ability to create access token for first admin user * revert accidental test file additions
1 parent 0dd874f commit f686636

File tree

1 file changed

+72
-9
lines changed

1 file changed

+72
-9
lines changed

cmd/src/validate.go

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222

2323
type validationSpec struct {
2424
FirstAdmin struct {
25-
Email string
26-
Username string
27-
Password string
25+
Email string
26+
Username string
27+
Password string
28+
CreateAccessToken bool
2829
}
2930
WaitRepoCloned struct {
3031
Repo string
@@ -68,9 +69,9 @@ Please visit https://docs.sourcegraph.com/admin/validation for documentation of
6869
var (
6970
contextFlag = flagSet.String("context", "", `Comma-separated list of key=value pairs to add to the script execution context`)
7071
secretsFlag = flagSet.String("secrets", "", "Path to a file containing key=value lines. The key value pairs will be added to the script context")
71-
apiFlags = api.NewFlags(flagSet)
72+
apiFlags = api.NewFlags(flagSet)
7273
)
73-
74+
7475
handler := func(args []string) error {
7576
flagSet.Parse(args)
7677

@@ -117,8 +118,8 @@ Please visit https://docs.sourcegraph.com/admin/validation for documentation of
117118
}
118119

119120
commands = append(commands, &command{
120-
flagSet: flagSet,
121-
handler: handler,
121+
flagSet: flagSet,
122+
handler: handler,
122123
usageFunc: usageFunc,
123124
})
124125
}
@@ -174,6 +175,14 @@ func (vd *validator) validate(script []byte, scriptContext map[string]string, is
174175
if err != nil {
175176
return err
176177
}
178+
179+
if vspec.FirstAdmin.CreateAccessToken {
180+
token, err := vd.createAccessToken(vspec.FirstAdmin.Username)
181+
if err != nil {
182+
return err
183+
}
184+
fmt.Println(token)
185+
}
177186
}
178187

179188
if vspec.ExternalService.DisplayName != "" {
@@ -303,7 +312,7 @@ func (vd *validator) listClonedRepos(fs []string) ([]string, error) {
303312
var resp struct {
304313
Repositories struct {
305314
Nodes []struct {
306-
Name string `json:"name"`
315+
Name string `json:"name"`
307316
MirrorInfo struct {
308317
Cloned bool `json:"cloned"`
309318
} `json:"mirrorInfo"`
@@ -312,7 +321,7 @@ func (vd *validator) listClonedRepos(fs []string) ([]string, error) {
312321
}
313322

314323
err := vd.graphQL(vdListRepos, map[string]interface{}{
315-
"names": fs,
324+
"names": fs,
316325
}, &resp)
317326

318327
names := make([]string, 0, len(resp.Repositories.Nodes))
@@ -341,6 +350,60 @@ func (vd *validator) waitRepoCloned(repoName string, sleepSeconds int, maxTries
341350
return false, nil
342351
}
343352

353+
const vdUserQuery = `
354+
query User($username: String) {
355+
user(username: $username) {
356+
id
357+
}
358+
}`
359+
360+
func (vd *validator) userID(username string) (string, error) {
361+
var resp struct {
362+
User struct {
363+
ID string `json:"id"`
364+
} `json:"user"`
365+
}
366+
367+
err := vd.graphQL(vdUserQuery, map[string]interface{}{
368+
"username": username,
369+
}, &resp)
370+
371+
return resp.User.ID, err
372+
}
373+
374+
const vdCreateAccessTokenMutation = `
375+
mutation CreateAccessToken($user: ID!, $scopes: [String!]!, $note: String!) {
376+
createAccessToken(
377+
user:$user,
378+
scopes:$scopes,
379+
note: $note
380+
)
381+
{
382+
token
383+
}
384+
}`
385+
386+
func (vd *validator) createAccessToken(username string) (string, error) {
387+
userID, err := vd.userID(username)
388+
if err != nil {
389+
return "", err
390+
}
391+
392+
var resp struct {
393+
CreateAccessToken struct {
394+
Token string `json:"token"`
395+
} `json:"createAccessToken"`
396+
}
397+
398+
err = vd.graphQL(vdCreateAccessTokenMutation, map[string]interface{}{
399+
"user": userID,
400+
"scopes": []string{"user:all", "site-admin:sudo"},
401+
"note": "src_cli_validate",
402+
}, &resp)
403+
404+
return resp.CreateAccessToken.Token, err
405+
}
406+
344407
// SiteAdminInit initializes the instance with given admin account.
345408
// It returns an authenticated client as the admin for doing e2e testing.
346409
func (vd *validator) siteAdminInit(baseURL, email, username, password string) (*vdClient, error) {

0 commit comments

Comments
 (0)