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

Add GitLab API backend #591

Merged
merged 1 commit into from
Aug 27, 2018
Merged
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
106 changes: 106 additions & 0 deletions anitya/lib/backends/gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2018 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2, or (at your option) any later
# version. This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY expressed or implied, including the
# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details. You
# should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Any Red Hat trademarks that are incorporated in the source
# code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission
# of Red Hat, Inc.
#
"""
Authors:
Michal Konecny <[email protected]>

"""

from anitya.lib.backends import BaseBackend
from anitya.lib.exceptions import AnityaPluginException

import logging

_log = logging.getLogger(__name__)


class GitlabBackend(BaseBackend):
''' The custom class for projects hosted on gitlab.

This backend allows to specify a version_url and owner, name of the repository.
'''

name = 'GitLab'
examples = [
'https://gitlab.gnome.org/GNOME/gnome-video-arcade',
'https://gitlab.com/xonotic/xonotic',
]

@classmethod
def get_version(cls, project):
''' Method called to retrieve the latest version of the projects
provided, project that relies on the backend of this plugin.

:arg Project project: a :class:`anitya.db.models.Project` object whose backend
corresponds to the current plugin.
:return: the latest version found upstream
:return type: str
:raise AnityaPluginException: a
:class:`anitya.lib.exceptions.AnityaPluginException` exception
when the version cannot be retrieved correctly

'''
return cls.get_ordered_versions(project)[-1]

@classmethod
def get_versions(cls, project):
''' Method called to retrieve all the versions (that can be found)
of the projects provided, project that relies on the backend of
this plugin.

:arg Project project: a :class:`anitya.db.models.Project` object whose backend
corresponds to the current plugin.
:return: a list of all the possible releases found
:return type: list
:raise AnityaPluginException: a
:class:`anitya.lib.exceptions.AnityaPluginException` exception
when the versions cannot be retrieved correctly

'''
url_template = '%(hostname)s/api/v4/projects/%(owner)s%%2F%(repo)s/repository/tags'
if project.version_url:
tokens = project.version_url.split('/')
elif project.homepage:
tokens = project.homepage.split('/')
else:
raise AnityaPluginException(
'Project %s was incorrectly set-up' % project.name)

url = url_template % {'hostname': tokens[0] + '//' + tokens[2],
'owner': tokens[3],
'repo': tokens[4]}

resp = cls.call_url(url)

if resp.ok:
json = resp.json()
else:
raise AnityaPluginException(
'%s: Server responded with status "%s": "%s"' % (
project.name, resp.status_code, resp.reason))

_log.debug('Received %d tags for %s' % (len(json), project.name))

tags = []
for tag in json:
tags.append(tag["name"])

return tags
4 changes: 4 additions & 0 deletions anitya/templates/project_new.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ <h1>{{ context }} project</h1>
$("label[for='version_url']").text('GitHub owner/project');
$('#version_url_row').show();
};
if (str == 'GitLab'){
$("label[for='version_url']").text('GitLab project url');
$('#version_url_row').show();
};
if (str == 'BitBucket'){
$("label[for='version_url']").text('BitBucket owner/project');
$('#version_url_row').show();
Expand Down
144 changes: 144 additions & 0 deletions anitya/tests/lib/backends/test_gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2018 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2, or (at your option) any later
# version. This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY expressed or implied, including the
# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details. You
# should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Any Red Hat trademarks that are incorporated in the source
# code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission
# of Red Hat, Inc.
#

'''
Anitya tests for the GitLab backend.
'''

import unittest

import anitya.lib.backends.gitlab as backend
from anitya.db import models
from anitya.lib.exceptions import AnityaPluginException
from anitya.tests.base import DatabaseTestCase, create_distro


BACKEND = 'GitLab'


class GitlabBackendtests(DatabaseTestCase):
""" Gitlab backend tests. """

def setUp(self):
""" Set up the environnment, ran before every tests. """
super(GitlabBackendtests, self).setUp()

create_distro(self.session)
self.create_project()

def create_project(self):
""" Create some basic projects to work with. """
project = models.Project(
name='gnome-video-arcade',
homepage='https://gitlab.gnome.org/GNOME/gnome-video-arcade',
version_url='https://gitlab.gnome.org/GNOME/gnome-video-arcade',
backend=BACKEND,
)
self.session.add(project)
self.session.commit()

project = models.Project(
name='foobar',
homepage='https://gitlab.com/foo/bar',
version_url='https://gitlab.com/foo/bar',
backend=BACKEND,
)
self.session.add(project)
self.session.commit()

project = models.Project(
name='xonotic',
homepage='https://gitlab.com/xonotic/xonotic',
backend=BACKEND,
)
self.session.add(project)
self.session.commit()

def test_get_version(self):
""" Test the get_version function of the gitlab backend. """
pid = 1
project = models.Project.get(self.session, pid)
exp = '0.8.8'
obs = backend.GitlabBackend.get_version(project)
self.assertEqual(obs, exp)

pid = 2
project = models.Project.get(self.session, pid)
self.assertRaises(
AnityaPluginException,
backend.GitlabBackend.get_version,
project
)

pid = 3
project = models.Project.get(self.session, pid)
exp = 'xonotic-v0.8.2'
obs = backend.GitlabBackend.get_version(project)
self.assertEqual(obs, exp)

def test_get_versions(self):
""" Test the get_versions function of the gitlab backend. """
pid = 1
project = models.Project.get(self.session, pid)
exp = [
u'0.6.1', u'0.6.1.1', u'0.6.2', u'0.6.3',
u'0.6.4', u'0.6.5', u'0.6.6', u'0.6.7',
u'0.6.8', u'0.7.0', u'0.7.1', u'0.8.0',
u'0.8.1', u'0.8.2', u'0.8.3', u'0.8.4',
u'0.8.5', u'0.8.6', u'0.8.7', u'0.8.8'
]
obs = backend.GitlabBackend.get_ordered_versions(project)
self.assertEqual(obs, exp)

pid = 2
project = models.Project.get(self.session, pid)
self.assertRaises(
AnityaPluginException,
backend.GitlabBackend.get_versions,
project
)

pid = 3
project = models.Project.get(self.session, pid)
exp = [
u'xonotic-v0.1.0preview', u'xonotic-v0.5.0',
u'xonotic-v0.6.0', u'xonotic-v0.7.0',
u'xonotic-v0.8.0', u'xonotic-v0.8.1',
u'xonotic-v0.8.2'
]
obs = backend.GitlabBackend.get_ordered_versions(project)
self.assertEqual(obs, exp)

project = models.Project(
name='foobar',
homepage='',
backend=BACKEND,
)
self.assertRaises(
AnityaPluginException,
backend.GitlabBackend.get_versions,
project
)


if __name__ == '__main__':
SUITE = unittest.TestLoader().loadTestsFromTestCase(GitlabBackendtests)
unittest.TextTestRunner(verbosity=2).run(SUITE)
2 changes: 1 addition & 1 deletion anitya/tests/lib/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
EXPECTED_BACKENDS = [
'BitBucket', 'CPAN (perl)', 'CRAN (R)', 'crates.io', 'Debian project',
'Drupal6', 'Drupal7', 'Freshmeat',
'GNOME', 'GNU project', 'GitHub', 'Google code', 'Hackage',
'GNOME', 'GNU project', 'GitHub', 'GitLab', 'Google code', 'Hackage',
'Launchpad', 'Maven Central', 'PEAR', 'PECL', 'Packagist', 'PyPI',
'Rubygems', 'Sourceforge', 'Stackage', 'custom', 'folder', 'npmjs',
'pagure',
Expand Down
Loading