Skip to content

Add GetAuthenticatedUser to the Forge interface - #197

Open
abhinavgautam01 wants to merge 2 commits into
git-pkgs:mainfrom
abhinavgautam01:authenticated-user
Open

abhinavgautam01 wants to merge 2 commits into
git-pkgs:mainfrom
abhinavgautam01:authenticated-user

Conversation

@abhinavgautam01

Copy link
Copy Markdown
Contributor

Closes #47

Summary

Adds GetAuthenticatedUser(ctx) (*forge.User, error) to the Forge interface. It returns the profile of the user the client's token belongs to. This lets apps that sign users in with OAuth show the user's username, name, avatar and email without calling each forge's API themselves.

It returns the existing forge.User type, so there are no new types.

Per-backend behaviour

Backend Endpoint Fields returned
GitHub GET /user Login, Name, Email, AvatarURL, HTMLURL
GitLab GET /api/v4/user Login, Name, Email, AvatarURL, HTMLURL
Gitea / Forgejo GET /api/v1/user Login, Name, Email, AvatarURL, HTMLURL
Bitbucket GET /2.0/user Login, Name, AvatarURL, HTMLURL
Gerrit GET /accounts/self (/a/accounts/self when a token is set) Login, Name, Email
Tangled none ErrNotSupported
GitLab under TinyGo none the existing TinyGo ErrNotSupported error

Notes:

  • Bitbucket: Email is left empty because Bitbucket only returns email addresses from the separate /user/emails endpoint.
  • Gerrit: uses the same account conversion as change owners and authors, so the login falls back to the email and then the account ID when there is no username.
  • Gitea: uses the SDK's GetMyUserInfo, the same way the other Gitea services call the SDK. The SDK call takes no context.
  • Errors: auth failures (for example a missing or expired token) come back as each backend's usual error. Nothing is wrapped or hidden.

Compatibility

This adds a method to the Forge interface, so code outside this repo that implements Forge needs to add it, the same as when GetRateLimit was added. The in-repo mocks in forges_test.go and internal/cli/pr_checkout_test.go are updated.

Tests

  • GitHub, GitLab, Gitea, Bitbucket and Gerrit each get a test against a fake server. Each test checks the endpoint path and every field mapped into forge.User. The GitLab, Gitea, Bitbucket and Gerrit tests also check that the token is sent in that forge's auth header.
  • GitHub and Bitbucket also have a 401 test, which checks that a failed login comes back as an error.
  • The Tangled test checks that it returns ErrNotSupported.
  • TestTinyGoServicesReturnUnsupported now covers GetAuthenticatedUser.
  • go test -race ./..., go test -tags=tinygo ./gitlab, the js/wasm and wasip1 builds and golangci-lint v2.14.0 all pass locally.

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, the shape is right and the per-backend tests are thorough. Two changes before merging.

Comment thread gitea/user.go Resolved
Comment thread gerrit/user.go Resolved
@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Thanks! Both changes are pushed: Gitea errors go through wrapErr (with a 401 test) and convertAccount is now a package-level function. Tests, lint and the wasm builds pass locally.

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.

Authenticated user API

2 participants