Skip to content
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

Use metadata to reconcile go-github with GitHub's OpenAPI descriptions #2919

Merged
merged 38 commits into from
Nov 3, 2023

Conversation

WillAbides
Copy link
Contributor

@WillAbides WillAbides commented Sep 9, 2023

see #2908

TODO before submitting for review:

  • Fix line-end issue on Windows
  • Update CONTRIBUTING.md
  • Add copyright notices to new files

This PR introduces a file called openapi_operations.yaml that contains details of GitHub's endpoints as described in https://github.com/github/rest-api-description. When combined with the //meta:operation directive, this allows us to automatically keep documentation links up to date, but more importantly it allows us to track endpoints as they are added and removed.

The change set is huge, but it is primarily made up of openapi_operations.yaml itself and changes to godoc.

"tools/cmd/metadata" is a tool to manage metadata.yaml. I'll paste its help output here for convenience:

Usage: metadata <command>

Flags:
  -h, --help               Show context-sensitive help.
  -C, --working-dir="."    Working directory. Should be the root of the go-github repository.

Commands:
  update-openapi
    Update openapi_operations.yaml from OpenAPI descriptions in github.com/github/rest-api-description at the given git ref.

  update-go
    Update go source code to be consistent with openapi_operations.yaml.
      - Adds and updates "// GitHub API docs:" comments for service methods.
      - Updates "//meta:operation" comments to use canonical operation names.
      - Updates formatting of "//meta:operation" comments to make sure there isn't a space between the "//" and the "meta".
      - Formats modified files with the equivalent of "go fmt".

  format
    Format whitespace in openapi_operations.yaml and sort its operations.

  unused
    List operations in openapi_operations.yaml that aren't used by any service methods.

Run "metadata <command> --help" for more information on a command.

I'm trying to limit the scope of this PR because its line count is already pretty high. I will make a follow-up PR with a daily scheduled workflow that runs "update-openapi" and "update-go" and creates a PR if there are changes.

@codecov
Copy link

codecov bot commented Sep 9, 2023

Codecov Report

Merging #2919 (4a065a9) into master (5b34ea7) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #2919   +/-   ##
=======================================
  Coverage   98.11%   98.11%           
=======================================
  Files         149      149           
  Lines       12796    12796           
=======================================
  Hits        12555    12555           
  Misses        166      166           
  Partials       75       75           
Files Coverage Δ
github/actions_artifacts.go 95.71% <ø> (ø)
github/actions_cache.go 100.00% <ø> (ø)
github/actions_oidc.go 100.00% <ø> (ø)
github/actions_permissions_enterprise.go 100.00% <ø> (ø)
github/actions_permissions_orgs.go 100.00% <ø> (ø)
github/actions_required_workflows.go 100.00% <ø> (ø)
github/actions_runner_groups.go 100.00% <ø> (ø)
github/actions_runners.go 100.00% <ø> (ø)
github/actions_secrets.go 100.00% <ø> (ø)
github/actions_variables.go 100.00% <ø> (ø)
... and 133 more

Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be OOO for one week, but before I go, I just wanted to point out that some of the new URLs have double slashes next to each other, e.g. https://docs.github.com/enterprise-cloud@latest//rest/actions/cache#get-github-actions-cache-usage-for-an-enterprise.

Also, in cases where URLs have been completely removed, it might be nice to point
to explanations as to why they are being removed, such as:

Have a great week, and thanks for doing this, @WillAbides !

@WillAbides
Copy link
Contributor Author

@gmlewis enjoy your time away.

Those double slashed URLs are the same way in the OpenAPI description. They work as-is, but I wouldn't mind normalizing the path for aesthetic reasons.

That's a good point about adding explanations to the ones that lost their doc links. I'll go through those and add some notes.

@WillAbides
Copy link
Contributor Author

I updated update-urls to only modify links that don't resolve to the same docs. This makes the change set for this PR more manageable.

Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, @WillAbides - very nice.
I'm looking forward to using it.
It looks like the PR is still a "Draft" but looks pretty complete to me. What's missing?
Maybe more instructions for maintainers? It looks pretty automated, though.

@WillAbides
Copy link
Contributor Author

@gmlewis You're right about instructions. After I started to work on CONTRIBUTING.md I realized that this adds complexity for new contributors by asking them to edit a large yaml file. I also suspect that most new contributors don't do much more than skim CONTRIBUTING.md before making their first PR. I know I'm often guilty of that for new projects. A typical new contribution would be:

  1. code the change
  2. create the PR
  3. wait for CI to be approved
  4. find out you need to update metadata.yaml

I gave it a little more thought and came up with a change I think will help. People tend to copy and adapt existing code. We can move the operation mapping to method a directive comment so that new contributors will see it on other methods as they write their code.

The code comment for RepositoriesService.Get would become:

// Get fetches a repository.
//
// GitHub API docs: https://docs.github.com/en/rest/repos/repos#get-a-repository
//
//meta:operation GET /repos/{owner}/{repo}

//meta:operation ... is a directive comment so godoc ignores it and outputs the same as before.

I started coding this up thinking it would add complexity, but it actually simplifies the tooling code quite a bit.

What do you think of moving the mapping to a directive comment?

@gmlewis
Copy link
Collaborator

gmlewis commented Oct 16, 2023

I think that is a great idea, @WillAbides.
I notice that most contributors, myself included, start with an existing working example, then modify it for the task at hand.

This is super obvious when I ask for a change in a PR and their reply is "it was done this way over here". Sometimes my response is, fine, keep it, but sometimes I say "can you please fix it over there too?" 😄

So having this with each method will probably make the maintenance burden much lower moving forward, as it will be obvious that it is needed for each method. I like it.

@WillAbides
Copy link
Contributor Author

I'm glad you like that idea. I have it mostly working, but it might be a few days before I have time to finish it up.

# Conflicts:
#	update-urls/go.mod
#	update-urls/go.sum
@WillAbides WillAbides marked this pull request as ready for review October 19, 2023 18:41
Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, @WillAbides !
Just a few minor tweaks, please, then we'll be ready for a second LGTM+Approval before merging.

CONTRIBUTING.md Outdated Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
CONTRIBUTING.md Outdated
in `openapi_commit`. `update-openapi --validate` is called
by `script/lint.sh`.

- `update-go` - updates go files with documentation urls and formats comments.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"go" => "Go" or maybe ".go" here?
Also, "urls" => "URLs".

CONTRIBUTING.md Outdated Show resolved Hide resolved
Comment on lines 132 to 133
major, _ := strconv.Atoi(m[pattern.SubexpIndex("major")])
minor, _ := strconv.Atoi(m[pattern.SubexpIndex("minor")])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really not care here if the major and/or minor versions are not supplied?
If so, could you please add a comment before line 132 stating that we don't care?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it to return errors

tools/metadata/metadata_test.go Show resolved Hide resolved
tools/metadata/main.go Show resolved Hide resolved
@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Oct 19, 2023
Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @WillAbides !
LGTM.

Awaiting second LGTM+Approval from any other contributor to this repo before merging.

@WillAbides WillAbides changed the title Use metadata.yaml to reconcile go-github with GitHub's OpenAPI descriptions Use metadata to reconcile go-github with GitHub's OpenAPI descriptions Oct 23, 2023
Copy link
Contributor

@vandanrohatgi vandanrohatgi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if my approval helps here (won't be offended if we decide to wait for another approval), but I have gone through some the changes and LGTM.
Hoping this moves things along a bit.

@gmlewis
Copy link
Collaborator

gmlewis commented Nov 3, 2023

Thank you, @vandanrohatgi and @valbeat !
Merging.

@gmlewis gmlewis merged commit a54bc7d into google:master Nov 3, 2023
7 checks passed
@gmlewis gmlewis removed the NeedsReview PR is awaiting a review before merging. label Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants