Add gitlab API support for creating merge requests

Julien LepillerThu Aug 22 23:00:15+0200 2019

1b376bd

Add gitlab API support for creating merge requests

guix-full.manifest

2121
   "python"
2222
   "python-android-stringslib"
2323
   "python-dateutil"
24+
   "python-gitlab"
2425
   "python-lxml"
2526
   "python-neovim"
2627
   "python-polib"

guix.manifest

44
   "python"
55
   "python-android-stringslib"
66
   "python-dateutil"
7+
   "python-gitlab"
78
   "python-lxml"
89
   "python-polib"
910
   "python-pyenchant"

offlate/systems/git.py

3535
    def open(self, basedir):
3636
        self.basedir = basedir
3737
        self.updateURI()
38+
        self.updateFiles()
3839
3940
    def initialize(self, basedir):
4041
        self.basedir = basedir
4142
        self.updateURI()
4243
        self.clone(basedir + "/current")
44+
        self.updateFiles()
4345
    
4446
    def updateURI(self):
4547
        raise Exception("Unimplemented method in concrete class: updateURI")
4648
49+
    def updateFiles(self):
50+
        self.translationfiles = []
51+
4752
    def clone(self, directory):
4853
        print("Cloning {} in {}".format(self.uri, directory))
4954
        pygit2.clone_repository(self.uri, directory, callbacks=Progress())

6267
        raise Exception("Unimplemented method in concrete class: send")
6368
6469
    def save(self):
65-
        raise Exception("Unimplemented method in concrete class: save")
70+
        pass
6671
6772
    def content(self):
6873
        return {'default': []}

offlate/systems/gitlab.py

1717
1818
from .git import GitProject
1919
20+
from urllib.parse import urlparse
21+
import gitlab
22+
2023
class GitlabProject(GitProject):
2124
    def __init__(self, conf, name, lang, data = {}):
2225
        GitProject.__init__(self, conf, name, lang, data)

2427
    def updateURI(self):
2528
        self.uri = self.data['repo']
2629
        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 = '/'.join(self.uri.split('/')[-2:])
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+
            try:
64+
                project.files.get(file_path=mfile, ref=self.branch)
65+
                actions.append({'action': 'update',
66+
                    'file_path': mfile,
67+
                    'content': open(self.basedir + '/current/' + mfile).read()})
68+
            except:
69+
                actions.append({'action': 'create',
70+
                    'file_path': mfile,
71+
                    'content': open(self.basedir + '/current/' + mfile).read()})
72+
        if actions == []:
73+
            return
74+
        project.commits.create({
75+
            'branch': 'translation',
76+
            'commit_message': 'Update \'' + self.lang + '\' translation',
77+
            'actions': actions
78+
            })
79+
        project.mergerequests.create({'source_branch': 'translation',
80+
            'target_branch': self.branch, 'target_project_id': originproject.id,
81+
            'title': 'Update \'' + self.lang + '\' translation'})

offlate/ui/editor.py

4141
        self.qd.exec_()
4242
        return self.value
4343
44+
    def gitlabTokenNotFound(self, server):
45+
        self.qd = QErrorMessage()
46+
        self.qd.showMessage(self.qd.tr("Token for {} not found. Have you added this server to your settings?.").format(server))
47+
        self.qd.exec_()
48+
49+
    def gitlabTokenBranchError(self, branch):
50+
        self.qd = QErrorMessage()
51+
        self.qd.showMessage(self.qd.tr("Error while creating branch {}.").format(branch))
52+
        self.qd.exec_()
53+
4454
class ProjectView(QWidget):
4555
    translationModified = pyqtSignal()
4656

setup.py

1212
    version="0.2.0",
1313
    packages=find_packages(exclude=['.guix-profile*']),
1414
    python_requires = '>=3',
15-
    install_requires=['polib', 'ruamel.yaml', 'python-dateutil', 'PyQt5', 'pygit2'],
15+
    install_requires=['polib', 'ruamel.yaml', 'python-dateutil', 'PyQt5', 'pygit2',
16+
        'python-gitlab'],
1617
    entry_points={
1718
        'gui_scripts': [
1819
            'offlate=offlate.window:main',