Skip to content

Commit 900e508

Browse files
zyronixsamrocketman
authored andcommitted
Added support for version 4 of the Gitlab API (#100)
* Added support for version 4 of the Gitlab API * Defaulting to APIv4 * Updated documentation
1 parent 60607d6 commit 900e508

File tree

8 files changed

+107
-27
lines changed

8 files changed

+107
-27
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ gitlab-mirrors creates read only copies of remote repositories in gitlab. It
77
provides a CLI management interface for managing the mirrored repositories (e.g.
88
add, delete, update) so that an admin may regularly update all mirrors using
99
`crontab`. It operates by interacting with the [GitLab API][gitlab-api] using
10-
[python-gitlab3][python-gitlab3].
10+
[python-gitlab][python-gitlab].
1111

1212
## Features
1313

@@ -87,4 +87,4 @@ Created by Sam Gleske under [MIT License](LICENSE).
8787
[gm-puppet]: https://github.com/logicminds/gitlab_mirrors
8888
[issues]: https://github.com/samrocketman/gitlab-mirrors/issues
8989
[mirror-missing]: http://feedback.gitlab.com/forums/176466-general/suggestions/4286666-mirror-git-svn-into-repo-
90-
[python-gitlab3]: https://github.com/alexvh/python-gitlab3
90+
[python-gitlab]: https://github.com/python-gitlab/python-gitlab

add_mirror.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ if [ ! -f "${git_mirrors_dir}/config.sh" ];then
1818
exit 1
1919
fi
2020

21+
#check if api version is set
22+
[ -z $gitlab_api_version ] && gitlab_api_version=4
23+
2124
#export env vars for python script
22-
export gitlab_user_token_secret gitlab_url gitlab_namespace gitlab_user ssl_verify
25+
export gitlab_user_token_secret gitlab_url gitlab_namespace gitlab_user ssl_verify gitlab_api_version
2326

2427
PROGNAME="${0##*/}"
2528
PROGVERSION="${VERSION}"

config.sh.SAMPLE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ gitlab_url="https://gitlab.example.com"
4242
gitlab_user="gitmirror"
4343
#Generate a token for your $gitlab_user and set it here.
4444
gitlab_user_token_secret="$(head -n1 "${user_home}/private_token" 2> /dev/null || echo "")"
45+
#Sets the Gitlab API version, either 3 or 4
46+
gitlab_api_version=4
4547
#Verify signed SSL certificates?
4648
ssl_verify=true
4749
#Push to GitLab over http? Otherwise will push projects via SSH.

delete_mirror.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ if [ ! -f "${git_mirrors_dir}/config.sh" ];then
1616
exit 1
1717
fi
1818

19+
#check if api version is set
20+
[ -z $gitlab_api_version ] && gitlab_api_version=4
21+
1922
#export env vars for python script
20-
export gitlab_user_token_secret gitlab_url gitlab_namespace gitlab_user ssl_verify
23+
export gitlab_user_token_secret gitlab_url gitlab_namespace gitlab_user ssl_verify gitlab_api_version
2124

2225
cd "${git_mirrors_dir}"
2326

docs/prerequisites.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
### Required software
44

5-
* [Tested with GitLab 6.x/7.x/8.x][gitlab]
6-
* [pyapi-gitlab3 @ v0.5.4][python-gitlab3]
5+
* [Tested with GitLab 8.x/9.x/10/x][gitlab]
6+
* [python-gitlab @ v1.1.0][python-gitlab]
77
* [GNU coreutils][coreutils]
88
* [git 1.8.0][git] or later
99

@@ -24,14 +24,10 @@ aditional options.
2424

2525
### Required software install snippets
2626

27-
#### python-gitlab3
28-
29-
yum install python-setuptools
30-
git clone https://github.com/alexvh/python-gitlab3.git
31-
cd python-gitlab3
32-
git checkout v0.5.4
33-
python setup.py install
27+
#### python-gitlab
3428

29+
yum install python-pip
30+
pip install python-gitlab
3531

3632
#### Installing git
3733

@@ -93,4 +89,4 @@ Next up is [Installation and Setup](installation.md).
9389
[gitlab]: https://about.gitlab.com/
9490
[git-src]: http://code.google.com/p/git-core/
9591
[git-svn]: https://www.kernel.org/pub/software/scm/git/docs/git-svn.html
96-
[python-gitlab3]: https://github.com/alexvh/python-gitlab3
92+
[python-gitlab]: https://github.com/python-gitlab/python-gitlab

docs/upgrade/upgrade_0.5.x-0.6.x.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Upgrade Notes
2+
3+
This documentation outlines steps for you to upgrade from gitlab-mirrors `0.5.x`
4+
to gitlab-mirrors `0.6.x`. It is assumed you'll be working on a test instance
5+
of gitlab in a production environment. If you only have a single gitlab
6+
instance then follow these steps with care and at your own risk.
7+
8+
gitlab-mirrors has been certified to use a new prerequisite library called
9+
[python-gitlab](https://github.com/python-gitlab/python-gitlab). Therefore
10+
you must install `python-gitlab` before upgrading `gitlab-mirrors` to the latest
11+
edition.
12+
13+
# 1. Disable any cron jobs
14+
15+
If you have cron jobs set up then you'll need to disable them to avoid them
16+
launching gitlab-mirrors during your upgrade.
17+
18+
# 2. Update python-gitlab3 python-gitlab
19+
20+
I'll ouline the steps here real quick.
21+
22+
yum install python-pip
23+
pip uninstall gitlab3
24+
pip install python-gitlab
25+
26+
# 3. Update your gitlab-mirrors
27+
28+
su - gitmirror
29+
cd gitlab-mirrors
30+
git checkout master
31+
git fetch
32+
git pull origin master
33+
git checkout v0.6.x
34+
35+
Test on a dummy project to ensure your new setup works. Once you have verified
36+
everything works then you can re-enable the cron jobs.

lib/manage_gitlab_project.py

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from optparse import OptionParser
88
import os
99
try:
10-
import gitlab3 as gitlab
10+
import gitlab
1111
except ImportError:
12-
raise ImportError("python-gitlab3 module is not installed. You probably didn't read the install instructions closely enough. See docs/prerequisites.md.")
12+
raise ImportError("python-gitlab module is not installed. You probably didn't read the install instructions closely enough. See docs/prerequisites.md.")
1313

1414

1515

@@ -19,6 +19,7 @@
1919
gitlab_namespace=os.environ['gitlab_namespace']
2020
gitlab_user=os.environ['gitlab_user']
2121
ssl_verify=os.environ['ssl_verify']
22+
gitlab_api_version=os.environ['gitlab_api_version']
2223
except KeyError:
2324
print >> stderr, "Environment config missing. Do not run this script standalone."
2425
exit(1)
@@ -44,14 +45,44 @@
4445
project_name=args[0]
4546

4647
if not eval(ssl_verify.capitalize()):
47-
git=gitlab.GitLab(gitlab_url=gitlab_url,token=token_secret,ssl_verify=False)
48+
git=gitlab.Gitlab(gitlab_url,token_secret,ssl_verify=False,api_version=gitlab_api_version)
4849
else:
49-
git=gitlab.GitLab(gitlab_url=gitlab_url,token=token_secret,ssl_verify=True)
50+
git=gitlab.Gitlab(gitlab_url,token_secret,ssl_verify=True,api_version=gitlab_api_version)
51+
52+
def find_group(**kwargs):
53+
groups = git.groups.list()
54+
return _find_matches(groups, kwargs, False)
55+
56+
def find_project(**kwargs):
57+
projects = git.projects.list(as_list=True)
58+
return _find_matches(projects, kwargs, False)
59+
60+
def _find_matches(objects, kwargs, find_all):
61+
"""Helper function for _add_find_fn. Find objects whose properties
62+
match all key, value pairs in kwargs.
63+
Source: https://github.com/doctormo/python-gitlab3/blob/master/gitlab3/__init__.py
64+
"""
65+
ret = []
66+
for obj in objects:
67+
match = True
68+
# Match all supplied parameters
69+
for param, val in kwargs.items():
70+
if not getattr(obj, param) == val:
71+
match = False
72+
break
73+
if match:
74+
if find_all:
75+
ret.append(obj)
76+
else:
77+
return obj
78+
if not find_all:
79+
return None
80+
return ret
5081

5182
# transfer the project from the source namespace to the specified group namespace
5283
def transfer_project(src_project, group):
5384
value = group.transfer_project(src_project.id)
54-
dest_project = git.find_project(name=src_project.name)
85+
dest_project = find_project(name=src_project.name)
5586
return dest_project
5687

5788
def createproject(pname):
@@ -69,14 +100,16 @@ def createproject(pname):
69100
'wiki_enabled': options.wiki,
70101
'snippets_enabled': options.snippets,
71102
'public': options.public,
72-
'namespace_id': git.find_group(name=gitlab_namespace).id,
103+
'namespace_id': find_group(name=gitlab_namespace).id,
73104
}
74105
#make all project options lowercase boolean strings i.e. true instead of True
75106
for x in project_options.keys():
76107
project_options[x] = str(project_options[x]).lower()
77108
print >> stderr, "Creating new project %s" % pname
78-
git.add_project(pname,description=description,**project_options)
79-
found_project = git.find_project(name=pname)
109+
project_options['name'] = pname
110+
project_options['description'] = description
111+
git.projects.create(project_options)
112+
found_project = find_project(name=pname)
80113
if needs_transfer(gitlab_user, gitlab_namespace, found_project):
81114
found_project = transfer_project(found_project, found_group)
82115
return found_project
@@ -88,13 +121,19 @@ def needs_transfer(user, groupname, project):
88121
namespace = groupname
89122
else:
90123
namespace = user
91-
return project.namespace['name'] != namespace
124+
if type(project.namespace) == gitlab.v3.objects.Group:
125+
return project.namespace.name != namespace
126+
else:
127+
return project.namespace['name'] != namespace
128+
92129

93130
if options.create:
94-
found_group=git.find_group(name=gitlab_namespace)
131+
found_group = find_group(name=gitlab_namespace)
95132
found_project = None
96-
# search the group namespace first
97-
found_project=git.find_project(name=project_name)
133+
134+
found_project= find_project(name=project_name)
135+
#exit()
136+
98137
if found_project:
99138
if needs_transfer(gitlab_user, gitlab_namespace, found_project):
100139
found_project = transfer_project(found_project, found_group)
@@ -112,7 +151,7 @@ def needs_transfer(user, groupname, project):
112151
print found_project.ssh_url_to_repo
113152
elif options.delete:
114153
try:
115-
deleted_project=git.find_project(name=project_name).delete()
154+
deleted_project=find_project(name=project_name).delete()
116155
except Exception as e:
117156
print >> stderr, e
118157
exit(1)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-gitlab>=1.0.2,<2

0 commit comments

Comments
 (0)