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

Extend GitLab sync support to self-hosted instance #874

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/actions/sync_backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const signOut = () => (dispatch, getState) => {
break;
case 'GitLab':
persistField('gitLabProject', null);
persistField('gitLabHost', null);
createGitlabOAuth().reset();
break;
default:
Expand Down
3 changes: 2 additions & 1 deletion src/components/SyncServiceSignIn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ function GitLab() {
const defaultProject = 'https://gitlab.com/your/project';
const [project, setProject] = useState(defaultProject);
const handleSubmit = (evt) => {
const projectId = gitLabProjectIdFromURL(project);
const [hostname, projectId] = gitLabProjectIdFromURL(project);
if (projectId) {
persistField('authenticatedSyncService', 'GitLab');
persistField('gitLabHost', hostname);
persistField('gitLabProject', projectId);
createGitlabOAuth().fetchAuthorizationCode();
} else {
Expand Down
16 changes: 8 additions & 8 deletions src/sync_backend_clients/gitlab_sync_backend_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const createGitlabOAuth = () => {
let expiryPromise;
let invalidGrantPromise;
return new OAuth2AuthCodePKCE({
authorizationUrl: 'https://gitlab.com/oauth/authorize',
tokenUrl: 'https://gitlab.com/oauth/token',
authorizationUrl: `https://${getPersistedField('gitLabHost')}/oauth/authorize`,
tokenUrl: `https://${getPersistedField('gitLabHost')}/oauth/token`,
clientId: process.env.REACT_APP_GITLAB_CLIENT_ID,
redirectUrl: window.location.origin,
scopes: ['api'],
Expand Down Expand Up @@ -64,8 +64,8 @@ export const gitLabProjectIdFromURL = (projectURL) => {
// to a project. Reminder: a project path is not necessarily
// /user/project because it may be under one or more groups such
// as /user/group/subgroup/project.
if (url.hostname === 'gitlab.com' && path.split('/').length > 1) {
return encodeURIComponent(path);
if (path.split('/').length > 1) {
return [url.hostname, encodeURIComponent(path)];
} else {
return undefined;
}
Expand Down Expand Up @@ -130,8 +130,6 @@ export const treeToDirectoryListing = (tree) => {
);
};

const API_URL = 'https://gitlab.com/api/v4';

/**
* GitLab sync backend, implemented using their REST API.
*
Expand All @@ -141,7 +139,9 @@ const API_URL = 'https://gitlab.com/api/v4';
export default (oauthClient) => {
const decoratedFetch = oauthClient.decorateFetchHTTPClient(fetch);

const getProjectApi = () => `${API_URL}/projects/${getPersistedField('gitLabProject')}`;
const getApiUrl = () => `https://${getPersistedField('gitLabHost')}/api/v4`;

const getProjectApi = () => `${getApiUrl()}/projects/${getPersistedField('gitLabProject')}`;

const isSignedIn = async () => {
if (!oauthClient.isAuthorized()) {
Expand Down Expand Up @@ -174,7 +174,7 @@ export default (oauthClient) => {
// commit.
const [userResponse, membersResponse] = await Promise.all([
// https://docs.gitlab.com/ee/api/users.html#list-current-user-for-normal-users
decoratedFetch(`${API_URL}/user`),
decoratedFetch(`${getApiUrl()}/user`),
// https://docs.gitlab.com/ee/api/members.html#list-all-members-of-a-group-or-project
decoratedFetch(`${getProjectApi()}/members`),
]);
Expand Down
8 changes: 5 additions & 3 deletions src/sync_backend_clients/gitlab_sync_backend_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {

test('Parses GitLab project from URL', () => {
[
['https://gitlab.com/user/foo', 'user%2Ffoo'],
['https://gitlab.com/group/subgroup/project', 'group%2Fsubgroup%2Fproject'],
['gitlab.com/foo/bar', 'foo%2Fbar'],
['https://gitlab.com/user/foo', ['gitlab.com', 'user%2Ffoo']],
['https://gitlab.com/group/subgroup/project', ['gitlab.com', 'group%2Fsubgroup%2Fproject']],
['https://gitlab.example.com/user/foo', ['gitlab.example.com', 'user%2Ffoo']],
['gitlab.com/foo/bar', ['gitlab.com', 'foo%2Fbar']],
['gitlab.example.com/foo/bar', ['gitlab.example.com', 'foo%2Fbar']],
['gitlab.com/user-but-no-project', undefined],
schoettl marked this conversation as resolved.
Show resolved Hide resolved
['', undefined],
].forEach(([input, expected]) => {
Expand Down