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

ghcr.io: List packages based on whether owner is an org or a user #180

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ribbybibby
Copy link
Member

We were previously using releases to figure out tags for a Github package. I think this was wrong. Not all release tags will be pushed as package versions, and vice versa. Someone may choose to use GHCR without taking advantage of releases at all.

I've modified it so that the client will check if the owner is a user or an org and then make the appropriate PackageGetAllVersions function call to retrieve the tags.

I've also fixed a few other things I ran into while testing this:

  1. Create the github client in New so that rate limiting and other goodness is reused across calls.
  2. Fix RepoImageFromPath, so it will split the repository path into the 'owner' and 'repo' segments that Tags expects. Previously we would have got errors for subrepositories.
  3. Update TestRepoImage to ensure it doesn't panic on unexpected inputs.
  4. We don't need to use regex to match ghcr.io to ghcr.io.
  5. If we're excluding .att tags then we should probably exclude .sig and .sbom too.

Fixes #179, #175

We were previously using releases to figure out tags for a Github
package. I think this was wrong. Not all release tags will be pushed as
package versions, and vice versa. Someone may choose to use GHCR without
taking advantage of releases at all.

I've modified it so that the client will check if the owner is a user or
an org and then make the appropriate PackageGetAllVersions function call
to retrieve the tags.

I've also fixed a few other things I ran into while testing this:
1. Create the github client in `New` so that rate limiting and other
   goodness is reused across calls.
2. Fix `RepoImageFromPath`, so it will split the repository path into the
   'owner' and 'repo' segments that `Tags` expects. Previously we would
   have got errors for subrepositories.
3. Update `TestRepoImage` to ensure it doesn't panic on unexpected
   inputs.
4. We don't need to use regex to match `ghcr.io` to `ghcr.io`.
5. If we're excluding `.att` tags then we should probably exclude `.sig`
   and `.sbom` too.
// Choose the correct list packages function based on whether the owner
// is a user or an organization
getAllVersions := c.client.Organizations.PackageGetAllVersions
user, _, err := c.client.Users.Get(ctx, owner)
Copy link
Collaborator

Choose a reason for hiding this comment

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

GitHub has a very aggressive rate limit - we should look to try and cache this somewhere/where possible as this could cause image discovery to be significantly delayed or fail.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in efb6cc4.


func getTagsFromRelease(client *github.Client, owner string, repo string) ([]api.ImageTag, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using releases, was a way of being significantly quicker than searching the repo due to GitHubs aggressive rate limiting... Maybe this could be an option/annotation perhaps to enable/disable or a fallback?

Copy link
Member Author

Choose a reason for hiding this comment

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

Being able to say 'I want to use release versions to figure out latest' for specific repositories sounds like a good feature. We probably need to think about the best way to do that.

I would say that we could merge this PR, which fixes things for repositories that are currently broken, and then think about that in a follow up?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm happy to merge if an Issue is raised to capture this? (Once I get time to re-review things 🙈 )

Comment on lines 7 to 9
func (c *Client) IsHost(host string) bool {
return reg.MatchString(host)
return host == "ghcr.io"
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I get that this makes it simpler, but does make it harder or more complex if github decide to add additional subdomains etc or even their domain (not sure they would) additionally this does some what diverge the logic from other clients.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think the fact that we might need a regex down the line is a good reason to use a regex unnecessarily here. If GHCR starts using different domains, we could introduce a regex when that happens. We'd have to update the code either way in that situation.

I also don't think the implementation matching how things are done in other clients has much benefit either. As long as the interface is satisfied, I think the details of the implementation should be the simplest possible.

That being said, I just made this change in passing and it isn't really related to the PR, so happy to revert. It's more of a style thing than anything else.

@davidcollom davidcollom added this to the v1 release milestone May 16, 2024
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.

Cannot check packages stored on the Github Container Registry
2 participants