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

feat: Self hosted GitLab instances #957

Open
wants to merge 7 commits 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
3 changes: 2 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export default class App extends PureComponent {
});
break;
case 'GitLab':
const gitlabOAuth = createGitlabOAuth();
let project = getPersistedField('gitLabURL');
const gitlabOAuth = createGitlabOAuth(project);
if (gitlabOAuth.isAuthorized()) {
client = createGitLabSyncBackendClient(gitlabOAuth);
initialState.syncBackend = Map({
Expand Down
14 changes: 4 additions & 10 deletions src/components/SyncServiceSignIn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import GitLabLogo from './gitlab.svg';
import { persistField } from '../../util/settings_persister';
import {
createGitlabOAuth,
gitLabProjectIdFromURL,
} from '../../sync_backend_clients/gitlab_sync_backend_client';

import { DropboxAuth } from 'dropbox';
Expand Down Expand Up @@ -113,15 +112,10 @@ function GitLab() {
const defaultProject = 'https://gitlab.com/your/project';
const [project, setProject] = useState(defaultProject);
const handleSubmit = (evt) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Minor issue here: yarn eslint will complain that evt is never used. I'm not removing it as long as the validation of the projectId is still in question.

const projectId = gitLabProjectIdFromURL(project);
if (projectId) {
persistField('authenticatedSyncService', 'GitLab');
persistField('gitLabProject', projectId);
createGitlabOAuth().fetchAuthorizationCode();
} else {
evt.preventDefault();
alert('Project does not appear to be a valid gitlab.com URL');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@zaz I see that you've refactored the extraction of the projectId into the sync backend itself (function getProject). Is it still possible to have a validation if no projectId can be extracted as here? You've left a TODO here yourself^^

Copy link

Choose a reason for hiding this comment

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

Sure. Do you want the alert in gitLabProjectIdFromURL? Or would it be possible to move the alert into createGitlabOAuth? It might be nice to remove most of the checks in gitLabProjectIdFromURL and instead just attempt the OAuth and gracefully handle errors there, unless there would be a downside to that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You can move it to createGitlabOAuth, I guess. It's a Redux action and those can have side effects.

}
persistField('authenticatedSyncService', 'GitLab');
persistField('gitLabURL', project);
// TODO handle incorrect URLs, possibly with try ... catch ...
createGitlabOAuth(project).fetchAuthorizationCode();
};

return (
Expand Down
22 changes: 15 additions & 7 deletions src/sync_backend_clients/gitlab_sync_backend_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import { getPersistedField } from '../util/settings_persister';

import { fromJS, Map } from 'immutable';

export const createGitlabOAuth = () => {
export const createGitlabOAuth = (url = 'https://gitlab.com') => {
// Use promises as mutex to prevent concurrent token refresh attempts, which causes problems.
// More info: https://github.com/BitySA/oauth2-auth-code-pkce/issues/29
// TODO: remove this workaround if/when oauth2-auth-code-pkce fixes the issue.
let expiryPromise;
let invalidGrantPromise;
url = new URL(url)
return new OAuth2AuthCodePKCE({
authorizationUrl: 'https://gitlab.com/oauth/authorize',
tokenUrl: 'https://gitlab.com/oauth/token',
authorizationUrl: url.origin + '/oauth/authorize',
tokenUrl: url.origin + '/oauth/token',
clientId: process.env.REACT_APP_GITLAB_CLIENT_ID,
redirectUrl: window.location.origin,
scopes: ['api'],
Expand Down Expand Up @@ -64,7 +65,7 @@ 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) {
if (path.split('/').length > 1) {
return encodeURIComponent(path);
} else {
return undefined;
Expand Down Expand Up @@ -130,7 +131,7 @@ export const treeToDirectoryListing = (tree) => {
);
};

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

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

const getProjectApi = () => `${API_URL}/projects/${getPersistedField('gitLabProject')}`;
const getApi = () => {
let url = new URL(getPersistedField('gitLabURL'))
return url.origin + API_PATH
}

const getProject = () => gitLabProjectIdFromURL(getPersistedField('gitLabURL'))

const getProjectApi = () => getApi() + 'projects/' + getProject()

const isSignedIn = async () => {
if (!oauthClient.isAuthorized()) {
Expand Down Expand Up @@ -174,7 +182,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(getApi() + 'user'),
// https://docs.gitlab.com/ee/api/members.html#list-all-members-of-a-group-or-project
decoratedFetch(`${getProjectApi()}/members`),
]);
Expand Down
15 changes: 15 additions & 0 deletions src/sync_backend_clients/gitlab_sync_backend_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ test('Parses GitLab project from URL', () => {
});
});

test('Parses GitLab project from URL of a self-managed GitLab instance', () => {
[
['https://mygitlab.com/user/foo', 'user%2Ffoo'],
['https://mygitlab.com/group/subgroup/project', 'group%2Fsubgroup%2Fproject'],
['mygitlab.com/foo/bar', 'foo%2Fbar'],
['mygitlab.com/user-but-no-project', undefined],
['https://www.mygitlab.com/user/foo', 'user%2Ffoo'],
['https://www.mygitlab.com/group/subgroup/project', 'group%2Fsubgroup%2Fproject'],
['www.mygitlab.com/foo/bar', 'foo%2Fbar'],
['www.mygitlab.com/user-but-no-project', undefined],
].forEach(([input, expected]) => {
expect(gitLabProjectIdFromURL(input)).toEqual(expected);
});
});

test('Parses Link pagination header', () => {
[
[null, {}],
Expand Down