-
Notifications
You must be signed in to change notification settings - Fork 70
Implement CLI for ingested codeowners #943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| ) | ||
|
|
||
| var codeownersCommands commander | ||
|
|
||
| func init() { | ||
| usage := `'src codeowners' is a tool that manages ingested code ownership data in a Sourcegraph instance. | ||
|
|
||
| Usage: | ||
|
|
||
| src codeowners command [command options] | ||
|
|
||
| The commands are: | ||
|
|
||
| get returns the codeowners file for a repository, if exists | ||
| create create a codeowners file | ||
| update update a codeowners file | ||
| delete delete a codeowners file | ||
|
|
||
| Use "src codeowners [command] -h" for more information about a command. | ||
| ` | ||
|
|
||
| flagSet := flag.NewFlagSet("codeowners", flag.ExitOnError) | ||
| handler := func(args []string) error { | ||
| codeownersCommands.run(flagSet, "src codeowners", usage, args) | ||
| return nil | ||
| } | ||
|
|
||
| // Register the command. | ||
| commands = append(commands, &command{ | ||
| flagSet: flagSet, | ||
| aliases: []string{"codeowner"}, | ||
| handler: handler, | ||
| usageFunc: func() { | ||
| fmt.Println(usage) | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| const codeownersFragment = ` | ||
| fragment CodeownersFileFields on CodeownersIngestedFile { | ||
| contents | ||
| repository { | ||
| name | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| type CodeownersIngestedFile struct { | ||
| Contents string `json:"contents"` | ||
| Repository struct { | ||
| Name string `json:"name"` | ||
| } `json:"repository"` | ||
| } | ||
|
|
||
| func readFile(f string) (io.Reader, error) { | ||
| if f == "-" { | ||
| return os.Stdin, nil | ||
| } | ||
| return os.Open(f) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/lib/errors" | ||
|
|
||
| "github.com/sourcegraph/src-cli/internal/api" | ||
| "github.com/sourcegraph/src-cli/internal/cmderrors" | ||
| ) | ||
|
|
||
| func init() { | ||
| usage := ` | ||
| Examples: | ||
|
|
||
| Create a codeowners file for the repository "github.com/sourcegraph/sourcegraph": | ||
|
|
||
| $ src codeowners create -repo='github.com/sourcegraph/sourcegraph' -f CODEOWNERS | ||
|
|
||
| Create a codeowners file for the repository "github.com/sourcegraph/sourcegraph" from stdin: | ||
|
|
||
| $ src codeowners create -repo='github.com/sourcegraph/sourcegraph' -f - | ||
| ` | ||
|
|
||
| flagSet := flag.NewFlagSet("create", flag.ExitOnError) | ||
| usageFunc := func() { | ||
| fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src codeowners %s':\n", flagSet.Name()) | ||
| flagSet.PrintDefaults() | ||
| fmt.Println(usage) | ||
| } | ||
| var ( | ||
| repoFlag = flagSet.String("repo", "", "The repository to attach the data to") | ||
| fileFlag = flagSet.String("file", "", "File path to read ownership information from (- for stdin)") | ||
| fileShortFlag = flagSet.String("f", "", "File path to read ownership information from (- for stdin). Alias for -file") | ||
|
|
||
| apiFlags = api.NewFlags(flagSet) | ||
| ) | ||
|
|
||
| handler := func(args []string) error { | ||
| if err := flagSet.Parse(args); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if *repoFlag == "" { | ||
| return errors.New("provide a repo name using -repo") | ||
| } | ||
|
|
||
| if *fileFlag == "" && *fileShortFlag == "" { | ||
| return errors.New("provide a file using -file") | ||
| } | ||
| if *fileFlag != "" && *fileShortFlag != "" { | ||
| return errors.New("have to provide either -file or -f") | ||
| } | ||
| if *fileShortFlag != "" { | ||
| *fileFlag = *fileShortFlag | ||
| } | ||
|
|
||
| file, err := readFile(*fileFlag) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| content, err := io.ReadAll(file) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| client := cfg.apiClient(apiFlags, flagSet.Output()) | ||
|
|
||
| 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]interface{}{ | ||
| "repoName": *repoFlag, | ||
| "content": string(content), | ||
| }).Do(context.Background(), &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", *repoFlag)) | ||
| } | ||
| 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 | ||
| } | ||
|
|
||
| // Register the command. | ||
| codeownersCommands = append(codeownersCommands, &command{ | ||
| flagSet: flagSet, | ||
| handler: handler, | ||
| usageFunc: usageFunc, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/lib/errors" | ||
|
|
||
| "github.com/sourcegraph/src-cli/internal/api" | ||
| "github.com/sourcegraph/src-cli/internal/cmderrors" | ||
| ) | ||
|
|
||
| func init() { | ||
| usage := ` | ||
| Examples: | ||
|
|
||
| Delete a codeowners file for the repository "github.com/sourcegraph/sourcegraph": | ||
|
|
||
| $ src codeowners delete -repo='github.com/sourcegraph/sourcegraph' | ||
| ` | ||
|
|
||
| flagSet := flag.NewFlagSet("delete", flag.ExitOnError) | ||
| usageFunc := func() { | ||
| fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src codeowners %s':\n", flagSet.Name()) | ||
| flagSet.PrintDefaults() | ||
| fmt.Println(usage) | ||
| } | ||
| var ( | ||
| repoFlag = flagSet.String("repo", "", "The repository to delete the data for") | ||
| apiFlags = api.NewFlags(flagSet) | ||
| ) | ||
|
|
||
| handler := func(args []string) error { | ||
| if err := flagSet.Parse(args); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if *repoFlag == "" { | ||
| return errors.New("provide a repo name using -repo") | ||
| } | ||
|
|
||
| client := cfg.apiClient(apiFlags, flagSet.Output()) | ||
|
|
||
| query := `mutation DeleteCodeownersFile( | ||
| $repoName: String!, | ||
| ) { | ||
| deleteCodeownersFiles(repositories: [{ | ||
| repoName: $repoName, | ||
| }]) { | ||
| alwaysNil | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| var result struct { | ||
| DeleteCodeownersFile CodeownersIngestedFile | ||
| } | ||
| if ok, err := client.NewRequest(query, map[string]interface{}{ | ||
| "repoName": *repoFlag, | ||
| }).Do(context.Background(), &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", *repoFlag)) | ||
| } | ||
| if strings.Contains(e.Error(), "codeowners file not found:") { | ||
| return cmderrors.ExitCode(2, errors.Newf("no data found for repository %q", *repoFlag)) | ||
| } | ||
| } | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Register the command. | ||
| codeownersCommands = append(codeownersCommands, &command{ | ||
| flagSet: flagSet, | ||
| handler: handler, | ||
| usageFunc: usageFunc, | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/lib/errors" | ||
|
|
||
| "github.com/sourcegraph/src-cli/internal/api" | ||
| "github.com/sourcegraph/src-cli/internal/cmderrors" | ||
| ) | ||
|
|
||
| func init() { | ||
| usage := ` | ||
| Examples: | ||
|
|
||
| Read the current codeowners file for the repository "github.com/sourcegraph/sourcegraph": | ||
|
|
||
| $ src codeowners get -repo='github.com/sourcegraph/sourcegraph' | ||
| ` | ||
|
|
||
| flagSet := flag.NewFlagSet("get", flag.ExitOnError) | ||
| usageFunc := func() { | ||
| fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src codeowners %s':\n", flagSet.Name()) | ||
| flagSet.PrintDefaults() | ||
| fmt.Println(usage) | ||
| } | ||
| var ( | ||
| repoFlag = flagSet.String("repo", "", "The repository to attach the data to") | ||
| apiFlags = api.NewFlags(flagSet) | ||
| ) | ||
|
|
||
| handler := func(args []string) error { | ||
| if err := flagSet.Parse(args); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if *repoFlag == "" { | ||
| return errors.New("provide a repo name using -repo") | ||
| } | ||
|
|
||
| client := cfg.apiClient(apiFlags, flagSet.Output()) | ||
|
|
||
| query := `query GetCodeownersFile( | ||
| $repoName: String! | ||
| ) { | ||
| repository(name: $repoName) { | ||
| ingestedCodeowners { | ||
| ...CodeownersFileFields | ||
| } | ||
| } | ||
| } | ||
| ` + codeownersFragment | ||
|
|
||
| var result struct { | ||
| Repository *struct { | ||
| IngestedCodeowners *CodeownersIngestedFile | ||
| } | ||
| } | ||
| if ok, err := client.NewRequest(query, map[string]interface{}{ | ||
| "repoName": *repoFlag, | ||
| }).Do(context.Background(), &result); err != nil || !ok { | ||
| return err | ||
| } | ||
|
|
||
| if result.Repository == nil { | ||
| return cmderrors.ExitCode(2, errors.Newf("repository %q not found", *repoFlag)) | ||
| } | ||
|
|
||
| if result.Repository.IngestedCodeowners == nil { | ||
| return cmderrors.ExitCode(2, errors.Newf("no codeowners data found for %q", *repoFlag)) | ||
| } | ||
|
|
||
| fmt.Fprintf(os.Stdout, "%s", result.Repository.IngestedCodeowners.Contents) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Register the command. | ||
| codeownersCommands = append(codeownersCommands, &command{ | ||
| flagSet: flagSet, | ||
| handler: handler, | ||
| usageFunc: usageFunc, | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this isn't up to date with main anymore, it takes a slice (just in case we wanted an admin UI with it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you try it as of https://github.com/sourcegraph/sourcegraph/pull/48584?