| 1 |
// Copyright 2023 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
// metadata is a command-line tool used to check and update this repo. |
| 7 |
// See CONTRIBUTING.md for details. |
| 8 |
package main |
| 9 |
|
| 10 |
import ( |
| 11 |
"context" |
| 12 |
"encoding/json" |
| 13 |
"fmt" |
| 14 |
"os" |
| 15 |
"path/filepath" |
| 16 |
|
| 17 |
"github.com/alecthomas/kong" |
| 18 |
"github.com/google/go-github/v60/github" |
| 19 |
) |
| 20 |
|
| 21 |
var helpVars = kong.Vars{ |
| 22 |
"update_openapi_help": ` |
| 23 |
Update openapi_operations.yaml from OpenAPI descriptions in github.com/github/rest-api-description at the given git ref. |
| 24 |
`, |
| 25 |
|
| 26 |
"update_go_help": ` |
| 27 |
Update go source code to be consistent with openapi_operations.yaml. |
| 28 |
- Adds and updates "// GitHub API docs:" comments for service methods. |
| 29 |
- Updates "//meta:operation" comments to use canonical operation names. |
| 30 |
- Updates formatting of "//meta:operation" comments to make sure there isn't a space between the "//" and the "meta". |
| 31 |
- Formats modified files with the equivalent of "go fmt". |
| 32 |
`, |
| 33 |
|
| 34 |
"format_help": `Format whitespace in openapi_operations.yaml and sort its operations.`, |
| 35 |
"unused_help": `List operations in openapi_operations.yaml that aren't used by any service methods.`, |
| 36 |
|
| 37 |
"working_dir_help": `Working directory. Should be the root of the go-github repository.`, |
| 38 |
"openapi_ref_help": `Git ref to pull OpenAPI descriptions from.`, |
| 39 |
|
| 40 |
"openapi_validate_help": ` |
| 41 |
Instead of updating, make sure that the operations in openapi_operations.yaml's "openapi_operations" field are |
| 42 |
consistent with the sha listed in "openapi_commit". This is run in CI as a convenience so that reviewers can trust |
| 43 |
changes to openapi_operations.yaml. |
| 44 |
`, |
| 45 |
|
| 46 |
"output_json_help": `Output JSON.`, |
| 47 |
} |
| 48 |
|
| 49 |
type rootCmd struct { |
| 50 |
UpdateOpenAPI updateOpenAPICmd `kong:"cmd,name=update-openapi,help=${update_openapi_help}"` |
| 51 |
UpdateGo updateGoCmd `kong:"cmd,help=${update_go_help}"` |
| 52 |
Format formatCmd `kong:"cmd,help=${format_help}"` |
| 53 |
Unused unusedCmd `kong:"cmd,help=${unused_help}"` |
| 54 |
|
| 55 |
WorkingDir string `kong:"short=C,default=.,help=${working_dir_help}"` |
| 56 |
|
| 57 |
// for testing |
| 58 |
GithubURL string `kong:"hidden,default='https://api.github.com'"` |
| 59 |
} |
| 60 |
|
| 61 |
func (c *rootCmd) opsFile() (string, *operationsFile, error) { |
| 62 |
filename := filepath.Join(c.WorkingDir, "openapi_operations.yaml") |
| 63 |
opsFile, err := loadOperationsFile(filename) |
| 64 |
if err != nil { |
| 65 |
return "", nil, err |
| 66 |
} |
| 67 |
return filename, opsFile, nil |
| 68 |
} |
| 69 |
|
| 70 |
func githubClient(apiURL string) (*github.Client, error) { |
| 71 |
token := os.Getenv("GITHUB_TOKEN") |
| 72 |
if token == "" { |
| 73 |
return nil, fmt.Errorf("GITHUB_TOKEN environment variable must be set to a GitHub personal access token with the public_repo scope") |
| 74 |
} |
| 75 |
return github.NewClient(nil).WithAuthToken(token).WithEnterpriseURLs(apiURL, "") |
| 76 |
} |
| 77 |
|
| 78 |
type updateOpenAPICmd struct { |
| 79 |
Ref string `kong:"default=main,help=${openapi_ref_help}"` |
| 80 |
ValidateGithub bool `kong:"name=validate,help=${openapi_validate_help}"` |
| 81 |
} |
| 82 |
|
| 83 |
func (c *updateOpenAPICmd) Run(root *rootCmd) error { |
| 84 |
ctx := context.Background() |
| 85 |
if c.ValidateGithub && c.Ref != "main" { |
| 86 |
return fmt.Errorf("--validate and --ref are mutually exclusive") |
| 87 |
} |
| 88 |
filename, opsFile, err := root.opsFile() |
| 89 |
if err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
origOps := make([]*operation, len(opsFile.OpenapiOps)) |
| 93 |
copy(origOps, opsFile.OpenapiOps) |
| 94 |
for i := range origOps { |
| 95 |
origOps[i] = origOps[i].clone() |
| 96 |
} |
| 97 |
client, err := githubClient(root.GithubURL) |
| 98 |
if err != nil { |
| 99 |
return err |
| 100 |
} |
| 101 |
ref := c.Ref |
| 102 |
if c.ValidateGithub { |
| 103 |
ref = opsFile.GitCommit |
| 104 |
if ref == "" { |
| 105 |
return fmt.Errorf("openapi_operations.yaml does not have an openapi_commit field") |
| 106 |
} |
| 107 |
} |
| 108 |
err = opsFile.updateFromGithub(ctx, client, ref) |
| 109 |
if err != nil { |
| 110 |
return err |
| 111 |
} |
| 112 |
if !c.ValidateGithub { |
| 113 |
return opsFile.saveFile(filename) |
| 114 |
} |
| 115 |
if !operationsEqual(origOps, opsFile.OpenapiOps) { |
| 116 |
return fmt.Errorf("openapi_operations.yaml does not match the OpenAPI descriptions in github.com/github/rest-api-description") |
| 117 |
} |
| 118 |
return nil |
| 119 |
} |
| 120 |
|
| 121 |
type formatCmd struct{} |
| 122 |
|
| 123 |
func (c *formatCmd) Run(root *rootCmd) error { |
| 124 |
filename, opsFile, err := root.opsFile() |
| 125 |
if err != nil { |
| 126 |
return err |
| 127 |
} |
| 128 |
return opsFile.saveFile(filename) |
| 129 |
} |
| 130 |
|
| 131 |
type updateGoCmd struct{} |
| 132 |
|
| 133 |
func (c *updateGoCmd) Run(root *rootCmd) error { |
| 134 |
_, opsFile, err := root.opsFile() |
| 135 |
if err != nil { |
| 136 |
return err |
| 137 |
} |
| 138 |
err = updateDocs(opsFile, filepath.Join(root.WorkingDir, "github")) |
| 139 |
return err |
| 140 |
} |
| 141 |
|
| 142 |
type unusedCmd struct { |
| 143 |
JSON bool `kong:"help=${output_json_help}"` |
| 144 |
} |
| 145 |
|
| 146 |
func (c *unusedCmd) Run(root *rootCmd, k *kong.Context) error { |
| 147 |
_, opsFile, err := root.opsFile() |
| 148 |
if err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
unused, err := unusedOps(opsFile, filepath.Join(root.WorkingDir, "github")) |
| 152 |
if err != nil { |
| 153 |
return err |
| 154 |
} |
| 155 |
if c.JSON { |
| 156 |
enc := json.NewEncoder(k.Stdout) |
| 157 |
enc.SetIndent("", " ") |
| 158 |
return enc.Encode(unused) |
| 159 |
} |
| 160 |
fmt.Fprintf(k.Stdout, "Found %d unused operations\n", len(unused)) |
| 161 |
if len(unused) == 0 { |
| 162 |
return nil |
| 163 |
} |
| 164 |
fmt.Fprintln(k.Stdout, "") |
| 165 |
for _, op := range unused { |
| 166 |
fmt.Fprintln(k.Stdout, op.Name) |
| 167 |
if op.DocumentationURL != "" { |
| 168 |
fmt.Fprintf(k.Stdout, "doc: %s\n", op.DocumentationURL) |
| 169 |
} |
| 170 |
fmt.Fprintln(k.Stdout, "") |
| 171 |
} |
| 172 |
return nil |
| 173 |
} |
| 174 |
|
| 175 |
func main() { |
| 176 |
err := run(os.Args[1:], nil) |
| 177 |
if err != nil { |
| 178 |
fmt.Fprintln(os.Stderr, err) |
| 179 |
os.Exit(1) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func run(args []string, opts []kong.Option) error { |
| 184 |
var cmd rootCmd |
| 185 |
parser, err := kong.New(&cmd, append(opts, helpVars)...) |
| 186 |
if err != nil { |
| 187 |
return err |
| 188 |
} |
| 189 |
k, err := parser.Parse(args) |
| 190 |
if err != nil { |
| 191 |
return err |
| 192 |
} |
| 193 |
return k.Run() |
| 194 |
} |