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

[ENG-4961] Allow Gitlab full configuration via V2 API #10496

Open
wants to merge 1 commit into
base: develop
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
19 changes: 19 additions & 0 deletions addons/gitlab/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,22 @@ def delete_hook(self, save=True):
self.save()
return True
return False

def get_folders(self, *args, **kwargs):
connection = GitLabClient(external_account=self.external_account)
return [
{
'addon': 'gitlab',
'path': '/',
'kind': 'folder',
'id': repo.id,
'name': repo.path_with_namespace,
} for repo in connection.repos(all=True)
]

def set_folder(self, folder_data, auth):
if folder_data.get('folder_path') != self.repo or folder_data.get('folder_id') != self.repo_id:
self.repo = folder_data['folder_path']
self.repo_id = folder_data['folder_id']
self.nodelogger.log(action='folder_selected', save=True)
self.save()
2 changes: 1 addition & 1 deletion addons/gitlab/static/gitlabNodeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ var ViewModel = oop.extend(UserViewModel,{
return Boolean(self.selectedHost());
});
self.tokenUrl = ko.pureComputed(function() {
return self.host() ? self.host() + '/profile/personal_access_tokens' : null;
return self.host() ? self.host() + '/-/profile/personal_access_tokens' : null;
});
},
authSuccessCallback: function() {
Expand Down
2 changes: 1 addition & 1 deletion addons/gitlab/static/gitlabUserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var ViewModel = oop.extend(OAuthAddonSettingsViewModel,{
return Boolean(self.selectedHost());
});
self.tokenUrl = ko.pureComputed(function() {
return self.host() ? self.host() + '/profile/personal_access_tokens' : null;
return self.host() ? self.host() + '/-/profile/personal_access_tokens/' : null;
});
},
clearModal: function() {
Expand Down
4 changes: 2 additions & 2 deletions api/base/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@
VARNISH_SERVERS = osf_settings.VARNISH_SERVERS
ESI_MEDIA_TYPES = osf_settings.ESI_MEDIA_TYPES

ADDONS_FOLDER_CONFIGURABLE = ['box', 'dropbox', 's3', 'googledrive', 'figshare', 'owncloud', 'onedrive']
ADDONS_OAUTH = ADDONS_FOLDER_CONFIGURABLE + ['dataverse', 'github', 'bitbucket', 'gitlab', 'mendeley', 'zotero', 'forward', 'boa']
ADDONS_FOLDER_CONFIGURABLE = ['box', 'dropbox', 's3', 'googledrive', 'figshare', 'owncloud', 'onedrive', 'gitlab']
ADDONS_OAUTH = ADDONS_FOLDER_CONFIGURABLE + ['dataverse', 'github', 'bitbucket', 'mendeley', 'zotero', 'forward', 'boa']

BYPASS_THROTTLE_TOKEN = 'test-token'

Expand Down
54 changes: 54 additions & 0 deletions api/nodes/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gitlab

from django.db import connection
from distutils.version import StrictVersion

Expand Down Expand Up @@ -43,6 +45,7 @@
from website.project import new_private_link
from website.project.model import NodeUpdateError
from osf.utils import permissions as osf_permissions
from addons.gitlab.api import GitLabClient


class RegistrationProviderRelationshipField(RelationshipField):
Expand Down Expand Up @@ -2038,3 +2041,54 @@ def update(self, obj, validated_data):
# permission is in writeable_method_fields, so validation happens on OSF Group model
raise exceptions.ValidationError(detail=str(e))
return obj


class GitlabNodeAddonSettingsSerializer(NodeAddonSettingsSerializer):
"""
This overrides the serializer to provide specific behavior for Gitlab addons, it adds the ability to add Gitlab
credential entirely via the v2 API.
"""

host = ser.CharField(required=False, allow_null=True, write_only=True)
access_token = ser.CharField(required=False, allow_null=True, write_only=True)

def update(self, instance, validated_data):
instance = super().update(instance, validated_data)

host = validated_data.get('host')
access_token = validated_data.get('access_token')
# Validate host and access_token
if host and access_token:
client = GitLabClient(access_token=access_token, host=host)
try:
client.gitlab.auth()
user = client.gitlab.user
except ConnectionError:
raise exceptions.PermissionDenied('Gitlab host is invalid')
except gitlab.exceptions.GitlabAuthenticationError:
raise exceptions.PermissionDenied('Gitlab credentials were invalid')
except gitlab.exceptions.GitlabGetError:
raise exceptions.PermissionDenied('Gitlab credentials were invalid')

# When user's add credentials via API, make sure it's also saved to their user settings as well
account, created = ExternalAccount.objects.update_or_create(
provider='gitlab',
provider_id=user.web_url, # unique for host/username
defaults={
'display_name': user.username,
'oauth_key': access_token,
'oauth_secret': host,
},
)
instance.external_account_id = account.id
user = self.context['request'].user
if not user.external_accounts.filter(id=account.id).exists():
user.external_accounts.add(account)
user.save()

return instance

def get_folder_info(self, data, addon_name):
if data.get('folder_id') and data.get('folder_path'):
return True, data
return False, None
6 changes: 5 additions & 1 deletion api/nodes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
NodeGroupsSerializer,
NodeGroupsCreateSerializer,
NodeGroupsDetailSerializer,
GitlabNodeAddonSettingsSerializer,
)
from api.nodes.utils import NodeOptimizationMixin, enforce_no_children
from api.osf_groups.views import OSFGroupMixin
Expand Down Expand Up @@ -1419,8 +1420,11 @@ def get_serializer_class(self):
"""
Use NodeDetailSerializer which requires 'id'
"""
if 'provider' in self.kwargs and self.kwargs['provider'] == 'forward':
provider = self.kwargs.get('provider')
if provider == 'forward':
return ForwardNodeAddonSettingsSerializer
elif provider == 'gitlab':
return GitlabNodeAddonSettingsSerializer
else:
return NodeAddonSettingsSerializer

Expand Down
84 changes: 84 additions & 0 deletions api_tests/addons_tests/gitlab/test_configure_gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import mock
import pytest
from framework.auth.core import Auth
from api.base.settings.defaults import API_BASE
from osf_tests.factories import ProjectFactory, AuthUserFactory, ExternalAccountFactory

_mock = lambda attributes: type('MockObject', (mock.Mock,), attributes)


def mock_gitlab_client():
return _mock({
'gitlab': _mock({
'auth': lambda *args, **kwargs: True,
'user': _mock({
'username': 'WeaponX',
'web_url': 'https://BrianDawkins@eagles.bird'
})
})
})


@pytest.mark.django_db
class TestGitlabConfig:

@pytest.fixture()
def user(self):
return AuthUserFactory()

@pytest.fixture()
def node(self, user):
return ProjectFactory(creator=user)

@pytest.fixture()
def enabled_addon(self, node, user):
addon = node.get_or_add_addon('gitlab', auth=Auth(user))
addon.save()
return addon

@pytest.fixture()
def node_with_authorized_addon(self, user, node, enabled_addon):
external_account = ExternalAccountFactory(provider='gitlab')
enabled_addon.external_account = external_account
user_settings = user.get_or_add_addon('gitlab')
user_settings.save()
enabled_addon.user_settings = user_settings
user.external_accounts.add(external_account)
user.save()
enabled_addon.save()
return node

@mock.patch('api.nodes.serializers.GitLabClient', return_value=mock_gitlab_client())
def test_addon_folders_PATCH(self, mock_client, app, node_with_authorized_addon, user):
resp = app.patch_json_api(
f'/{API_BASE}nodes/{node_with_authorized_addon._id}/addons/gitlab/',
{
'data': {
'attributes': {
'folder_path': 'john.e.tordoff/test-project',
'folder_id': '52343250'
}
}
},
auth=user.auth
)
assert resp.status_code == 200
assert resp.json['data']['attributes']['folder_id'] == 'john.e.tordoff/test-project'
assert resp.json['data']['attributes']['folder_path'] == 'john.e.tordoff/test-project'

@mock.patch('api.nodes.serializers.GitLabClient', return_value=mock_gitlab_client())
def test_addon_credentials_PATCH(self, mock_client, app, node, user, enabled_addon):
resp = app.patch_json_api(
f'/{API_BASE}nodes/{node._id}/addons/gitlab/',
{
'data': {
'attributes': {
'host': 'https://hasanreddick@eagles.bird',
'access_token': 'JoshSweat'
}
}
},
auth=user.auth
)
assert resp.status_code == 200
assert resp.json['data']['attributes']['external_account_id']
Loading