-
Notifications
You must be signed in to change notification settings - Fork 56
/
repos.go
139 lines (119 loc) · 3.18 KB
/
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
package main
import (
"context"
"flag"
"fmt"
"time"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/src-cli/internal/api"
)
var reposCommands commander
func init() {
usage := `'src repos' is a tool that manages repositories on a Sourcegraph instance.
Usage:
src repos command [command options]
The commands are:
get gets a repository
list lists repositories
delete deletes repositories
add-metadata adds a key-value pair metadata to a repository
update-metadata updates a key-value pair metadata on a repository
delete-metadata deletes a key-value pair metadata from a repository
Use "src repos [command] -h" for more information about a command.
`
flagSet := flag.NewFlagSet("repos", flag.ExitOnError)
handler := func(args []string) error {
reposCommands.run(flagSet, "src repos", usage, args)
return nil
}
// Register the command.
commands = append(commands, &command{
flagSet: flagSet,
aliases: []string{"repo"},
handler: handler,
usageFunc: func() {
fmt.Println(usage)
},
})
}
const repositoryFragment = `
fragment RepositoryFields on Repository {
id
name
url
description
language
createdAt
updatedAt
externalRepository {
id
serviceType
serviceID
}
defaultBranch {
name
displayName
}
viewerCanAdminister
keyValuePairs {
key
value
}
}
`
type Repository struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
Description string `json:"description"`
Language string `json:"language"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt *time.Time `json:"updatedAt"`
ExternalRepository ExternalRepository `json:"externalRepository"`
DefaultBranch GitRef `json:"defaultBranch"`
ViewerCanAdminister bool `json:"viewerCanAdminister"`
KeyValuePairs []KeyValuePair `json:"keyValuePairs"`
}
type KeyValuePair struct {
Key string `json:"key"`
Value *string `json:"value"`
}
type ExternalRepository struct {
ID string `json:"id"`
ServiceType string `json:"serviceType"`
ServiceID string `json:"serviceID"`
}
type GitRef struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
}
func fetchRepositoryID(ctx context.Context, client api.Client, repoName string) (string, error) {
query := `query RepositoryID($repoName: String!) {
repository(name: $repoName) {
id
}
}`
var result struct {
Repository struct {
ID string
}
}
if ok, err := client.NewRequest(query, map[string]interface{}{
"repoName": repoName,
}).Do(ctx, &result); err != nil || !ok {
return "", err
}
if result.Repository.ID == "" {
return "", fmt.Errorf("repository not found: %s", repoName)
}
return result.Repository.ID, nil
}
func getRepoIdOrError(ctx context.Context, client api.Client, id *string, repoName *string) (*string, error) {
if *id != "" {
return id, nil
} else if *repoName != "" {
repoID, err := fetchRepositoryID(ctx, client, *repoName)
return &repoID, err
}
return nil, errors.New("error: repo or repoName is required")
}