offlate/offlate/systems/gitlab.py

gitlab.py

1
#   Copyright (c) 2018 Julien Lepiller <julien@lepiller.eu>
2
#
3
#   This program is free software: you can redistribute it and/or modify
4
#   it under the terms of the GNU Affero General Public License as
5
#   published by the Free Software Foundation, either version 3 of the
6
#   License, or (at your option) any later version.
7
#
8
#   This program is distributed in the hope that it will be useful,
9
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
#   GNU Affero General Public License for more details.
12
#
13
#   You should have received a copy of the GNU Affero General Public License
14
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
####
16
""" The gitlab system connector. """
17
18
from .git import GitProject
19
20
from urllib.parse import urlparse
21
import gitlab
22
23
class GitlabProject(GitProject):
24
    def __init__(self, conf, name, lang, data = {}):
25
        GitProject.__init__(self, conf, name, lang, data)
26
27
    def updateURI(self):
28
        self.uri = self.data['repo']
29
        self.branch = self.data['branch']
30
31
    def send(self, interface):
32
        server = urlparse(self.uri).hostname
33
        token = ""
34
        for serv in self.conf["servers"]:
35
            if serv["server"] == server:
36
                token = serv["token"]
37
                break
38
39
        if token == "":
40
            interface.gitlabTokenNotFound(server)
41
            return
42
43
        gl = gitlab.Gitlab("https://"+server, private_token=token)
44
        gl.auth()
45
46
        currentUser = gl.user.username
47
        projectname = self.uri.split('/')[-1]
48
        projectfullname = urlparse(self.uri).path[1:]
49
50
        originproject = gl.projects.get(projectfullname)
51
        try:
52
            project = gl.projects.get(currentUser + "/" + projectname)
53
        except:
54
            project = project.forks.create({})
55
56
        try:
57
            branch = project.branches.create({'branch': 'translation', 'ref': self.branch})
58
        except:
59
            interface.gitlabTokenBranchError('translation')
60
            return
61
        actions = []
62
        for mfile in self.translationfiles:
63
            mfile = mfile['filename']
64
            try:
65
                project.files.get(file_path=mfile, ref=self.branch)
66
                actions.append({'action': 'update',
67
                    'file_path': mfile,
68
                    'content': open(self.basedir + '/current/' + mfile).read()})
69
            except:
70
                actions.append({'action': 'create',
71
                    'file_path': mfile,
72
                    'content': open(self.basedir + '/current/' + mfile).read()})
73
        if actions == []:
74
            return
75
        project.commits.create({
76
            'branch': 'translation',
77
            'commit_message': 'Update \'' + self.lang + '\' translation',
78
            'actions': actions
79
            })
80
        project.mergerequests.create({'source_branch': 'translation',
81
            'target_branch': self.branch, 'target_project_id': originproject.id,
82
            'title': 'Update \'' + self.lang + '\' translation'})
83
84
    @staticmethod
85
    def isConfigured(conf):
86
        res = GitProject.isConfigured(conf)
87
        res = res and 'servers' in conf and conf['servers'] != '' and \
88
                conf['servers'] != None
89
        return res
90