Add gitlab API support for creating merge requests
guix-full.manifest
| 21 | 21 | "python" | |
| 22 | 22 | "python-android-stringslib" | |
| 23 | 23 | "python-dateutil" | |
| 24 | + | "python-gitlab" | |
| 24 | 25 | "python-lxml" | |
| 25 | 26 | "python-neovim" | |
| 26 | 27 | "python-polib" |
guix.manifest
| 4 | 4 | "python" | |
| 5 | 5 | "python-android-stringslib" | |
| 6 | 6 | "python-dateutil" | |
| 7 | + | "python-gitlab" | |
| 7 | 8 | "python-lxml" | |
| 8 | 9 | "python-polib" | |
| 9 | 10 | "python-pyenchant" |
offlate/systems/git.py
| 35 | 35 | def open(self, basedir): | |
| 36 | 36 | self.basedir = basedir | |
| 37 | 37 | self.updateURI() | |
| 38 | + | self.updateFiles() | |
| 38 | 39 | ||
| 39 | 40 | def initialize(self, basedir): | |
| 40 | 41 | self.basedir = basedir | |
| 41 | 42 | self.updateURI() | |
| 42 | 43 | self.clone(basedir + "/current") | |
| 44 | + | self.updateFiles() | |
| 43 | 45 | ||
| 44 | 46 | def updateURI(self): | |
| 45 | 47 | raise Exception("Unimplemented method in concrete class: updateURI") | |
| 46 | 48 | ||
| 49 | + | def updateFiles(self): | |
| 50 | + | self.translationfiles = [] | |
| 51 | + | ||
| 47 | 52 | def clone(self, directory): | |
| 48 | 53 | print("Cloning {} in {}".format(self.uri, directory)) | |
| 49 | 54 | pygit2.clone_repository(self.uri, directory, callbacks=Progress()) | |
… | |||
| 62 | 67 | raise Exception("Unimplemented method in concrete class: send") | |
| 63 | 68 | ||
| 64 | 69 | def save(self): | |
| 65 | - | raise Exception("Unimplemented method in concrete class: save") | |
| 70 | + | pass | |
| 66 | 71 | ||
| 67 | 72 | def content(self): | |
| 68 | 73 | return {'default': []} | |
offlate/systems/gitlab.py
| 17 | 17 | ||
| 18 | 18 | from .git import GitProject | |
| 19 | 19 | ||
| 20 | + | from urllib.parse import urlparse | |
| 21 | + | import gitlab | |
| 22 | + | ||
| 20 | 23 | class GitlabProject(GitProject): | |
| 21 | 24 | def __init__(self, conf, name, lang, data = {}): | |
| 22 | 25 | GitProject.__init__(self, conf, name, lang, data) | |
… | |||
| 24 | 27 | def updateURI(self): | |
| 25 | 28 | self.uri = self.data['repo'] | |
| 26 | 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 = '/'.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
| 41 | 41 | self.qd.exec_() | |
| 42 | 42 | return self.value | |
| 43 | 43 | ||
| 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 | + | ||
| 44 | 54 | class ProjectView(QWidget): | |
| 45 | 55 | translationModified = pyqtSignal() | |
| 46 | 56 |
setup.py
| 12 | 12 | version="0.2.0", | |
| 13 | 13 | packages=find_packages(exclude=['.guix-profile*']), | |
| 14 | 14 | 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'], | |
| 16 | 17 | entry_points={ | |
| 17 | 18 | 'gui_scripts': [ | |
| 18 | 19 | 'offlate=offlate.window:main', |