Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

gitserver: Add oauth2 username to GitHub clone url#45137

Merged
pjlast merged 8 commits into
mainfrom
pjlast/45136-add-oauth2-username-to-github-clone-url
Dec 6, 2022
Merged

gitserver: Add oauth2 username to GitHub clone url#45137
pjlast merged 8 commits into
mainfrom
pjlast/45136-add-oauth2-username-to-github-clone-url

Conversation

@pjlast

@pjlast pjlast commented Dec 5, 2022

Copy link
Copy Markdown
Contributor

Closes #45136

Based on this Stackoverflow thread: https://stackoverflow.com/questions/42148841/github-clone-with-oauth-access-token/66156992#66156992 it seems that GitHub fine-grained Personal Access Tokens don't work without the oauth2 username. This PR adds the username when generating the GitHub clone URL.

This is how other code hosts work as well, for example the clone URL for GitLab uses the oauth2 username as well: https://sourcegraph.com/github.com/sourcegraph/sourcegraph@ce477a4ad027d3dfcf74d4246d7f4c4a0eec3876/-/blob/internal/repos/clone_url.go?L216&subtree=true

I also confirmed that, with this change, the classic PATs still work.

Test plan

Tested repository cloning with both classic PATs and fine-grained PATs after the oauth2 username was added.

@pjlast pjlast requested review from a team December 5, 2022 08:21
@sourcegraph-bot

sourcegraph-bot commented Dec 5, 2022

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff e14b9c8...70877e0.

Notify File(s)
@indradhanush internal/repos/clone_url.go
internal/repos/clone_url_test.go
@ryanslade internal/repos/clone_url.go
internal/repos/clone_url_test.go
@sashaostrikov internal/repos/clone_url.go
internal/repos/clone_url_test.go
@sourcegraph/delivery doc/admin/external_service/github.md

u.User = url.UserPassword("x-access-token", cfg.Token)
} else {
u.User = url.User(cfg.Token)
u.User = url.UserPassword("oauth2", cfg.Token)

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.

What if someone uses the legacy token? Does that still work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In the description of the PR:

I also confirmed that, with this change, the classic PATs still work.

:)

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.

Sorry, that's on me :|

Comment thread doc/admin/external_service/github.md Outdated

> NOTE: Fine-grained personal access token suport is still experimental. Some functionality may not yet work.

To sync repositories using fine-grained personal access tokens read-only access on the Content permission is required for repositories.

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.

Suggested change
To sync repositories using fine-grained personal access tokens read-only access on the Content permission is required for repositories.
To sync repositories using fine-grained personal access tokens read-only access on the `Content` permission is required for repositories.

Comment thread doc/admin/external_service/github.md Outdated

To sync repositories using fine-grained personal access tokens read-only access on the Content permission is required for repositories.

It should also be noted that fine-grained personal access tokens work a bit different than normal personal access tokens. For example, a classic personal access token can access all repositories of the owner, as well as repositories of organizations that the owner belongs to. However, for fine-grained personal access tokens, access is restricted to the owner of the token. If access to a GitHub organization's repositories is required, the GitHub organization has to be the owner of the fine-grained personal access token.

@ryanslade ryanslade Dec 6, 2022

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.

Suggested change
It should also be noted that fine-grained personal access tokens work a bit different than normal personal access tokens. For example, a classic personal access token can access all repositories of the owner, as well as repositories of organizations that the owner belongs to. However, for fine-grained personal access tokens, access is restricted to the owner of the token. If access to a GitHub organization's repositories is required, the GitHub organization has to be the owner of the fine-grained personal access token.
It should also be noted that fine-grained personal access tokens work a bit differently than normal personal access tokens. For example, a classic personal access token can access all repositories of the owner, as well as repositories of organizations that the owner belongs to. However, for fine-grained personal access tokens, access is restricted to the owner of the token. If access to a GitHub organization's repositories is required, the GitHub organization has to be the owner of the fine-grained personal access token.

@mrnugget mrnugget Dec 6, 2022

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.

Pretty sure it's differently :)

(EDIT: @ryanslade I fixed it)

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.

It also looked wrong to me but spell-checker didn't kick in, so I assumed I made a mistake. It showed the red line under it only when I went back to edit it... weird.

Comment thread doc/admin/external_service/github.md Outdated

### Fine-grained personal access tokens

> NOTE: Fine-grained personal access token suport is still experimental. Some functionality may not yet work.

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.

We do have an experimental badge in use in docs. Use that please too.

Comment thread doc/admin/external_service/github.md Outdated

To sync repositories using fine-grained personal access tokens read-only access on the Content permission is required for repositories.

It should also be noted that fine-grained personal access tokens work a bit different than normal personal access tokens. For example, a classic personal access token can access all repositories of the owner, as well as repositories of organizations that the owner belongs to. However, for fine-grained personal access tokens, access is restricted to the owner of the token. If access to a GitHub organization's repositories is required, the GitHub organization has to be the owner of the fine-grained personal access token.

@mrnugget mrnugget Dec 6, 2022

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.

Pretty sure it's differently :)

(EDIT: @ryanslade I fixed it)

Comment thread CHANGELOG.md Outdated
- An issue causing certain kinds of queries to behave inconsistently in Code Insights. [#44917](https://github.com/sourcegraph/sourcegraph/pull/44917)
- When the setting `batchChanges.enforceForks` is enabled, Batch Changes will now prefix the name of the fork repo it creates with the original repo's namespace name in order to prevent repo name collisions. [#43681](https://github.com/sourcegraph/sourcegraph/pull/43681), [#44458](https://github.com/sourcegraph/sourcegraph/pull/44458), [#44548](https://github.com/sourcegraph/sourcegraph/pull/44548), [#44924](https://github.com/sourcegraph/sourcegraph/pull/44924)
- Code Insights: fixed an issue where certain queries matching sequential whitespace characters would overcount. [#44969](https://github.com/sourcegraph/sourcegraph/pull/44969)
- GitHub fine-grained Personal Access Tokens can now clone repositories correctly. [#45137](https://github.com/sourcegraph/sourcegraph/pull/45137)

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.

This is confusing. We say in changelog that they can clone repositories and we say in the docs that they are not yet supported. Which one is it then? :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I mean, they can clone repositories but they aren't supported 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe I'll add "but are not yet officially supported"

@pjlast pjlast enabled auto-merge (squash) December 6, 2022 13:59
@pjlast pjlast merged commit daeedc4 into main Dec 6, 2022
@pjlast pjlast deleted the pjlast/45136-add-oauth2-username-to-github-clone-url branch December 6, 2022 14:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add oauth2 username to GitHub clone URLs that use fine-grained tokens

5 participants