-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathcodeowners_create.go
More file actions
94 lines (82 loc) · 2.42 KB
/
Copy pathcodeowners_create.go
File metadata and controls
94 lines (82 loc) · 2.42 KB
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
package main
import (
"context"
"strings"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/clicompat"
"github.com/sourcegraph/src-cli/internal/cmderrors"
"github.com/urfave/cli/v3"
)
const codeownersCreateExamples = `
Create a codeowners file for a repository.
Examples:
$ src codeowners create -repo='github.com/sourcegraph/sourcegraph' -f CODEOWNERS
$ src codeowners create -repo='github.com/sourcegraph/sourcegraph' -f -
`
var codeownersCreateCommand = clicompat.Wrap(&cli.Command{
Name: "create",
Usage: "create a codeowners file",
UsageText: "src codeowners create [options]",
Description: codeownersCreateExamples,
HideVersion: true,
Flags: clicompat.WithAPIFlags(
&cli.StringFlag{
Name: "repo",
Usage: "The repository to attach the data to",
Required: true,
Validator: requiresNotEmpty("provide a repo name using -repo"),
},
&cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "File path to read ownership information from (- for stdin)",
TakesFile: true,
Required: true,
Validator: requiresNotEmpty("provide a file using -file"),
},
),
Action: func(ctx context.Context, cmd *cli.Command) error {
repoName := cmd.String("repo")
fileName := cmd.String("file")
content, err := readFile(fileName)
if err != nil {
return err
}
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
query := `mutation CreateCodeownersFile(
$repoName: String!,
$content: String!
) {
addCodeownersFile(input: {
repoName: $repoName,
fileContents: $content,
}
) {
...CodeownersFileFields
}
}
` + codeownersFragment
var result struct {
AddCodeownersFile CodeownersIngestedFile
}
if ok, err := client.NewRequest(query, map[string]any{
"repoName": repoName,
"content": string(content),
}).Do(ctx, &result); err != nil || !ok {
var gqlErr api.GraphQlErrors
if errors.As(err, &gqlErr) {
for _, e := range gqlErr {
if strings.Contains(e.Error(), "repo not found:") {
return cmderrors.ExitCode(2, errors.Newf("repository %q not found", repoName))
}
if strings.Contains(e.Error(), "codeowners file has already been ingested for this repository") {
return cmderrors.ExitCode(2, errors.New("codeowners file has already been ingested for this repository"))
}
}
}
return err
}
return nil
},
})