fluxcd / go-git-providers

Git provider client for Go
https://fluxcd.io
Apache License 2.0
71 stars 33 forks source link

Validate user ref when creating user repository #250

Closed somtochiama closed 9 months ago

somtochiama commented 9 months ago

Description

This pull request adds a function to the UserRepositories interface called GetUserLogin. It gets the current authenticated user associated with the token. This function is used when creating user repositories to validate that UserLogin. This is required because the Create API doesn't require you to pass a user and giving the wrong UserLogin successfully creates a repository.

When running flux bootstrap with the wrong user (and the repository already exists) returns this confusing error because ggp tries to create the repository under the authenticated user.

flux bootstrap github --owner=soule --token-auth --repository=fleet-infra --personal
► connecting to github.com
✗ failed to create new Git repository "https://github.com/soule/fleet-infra": multiple errors occurred:
- POST https://api.github.com/user/repos: 422 Repository creation failed. [{Resource:Repository Field:name Code:custom Message:name already exists on this account}]
- resource already exists, cannot create object. Use Reconcile() to create it idempotently

With these changes, we give a better error

./bin/flux bootstrap github --owner=soule --token-auth --repository=fleet-infraxx --personal
► connecting to github.com
✗ failed to create new Git repository "https://github.com/soule/fleet-infraxx": incorrect owner 'soule' passed in

Test results