Add github system

Julien LepillerSun Nov 10 11:44:57+0100 2019

ee023bb

Add github system

guix.scm

44
  (guix download)
55
  (guix git-download)
66
  (guix packages)
7+
  (guix utils)
78
  (gnu packages aspell)
89
  (gnu packages check)
910
  (gnu packages enchant)

368369
      ("python-polib" ,python-polib)
369370
      ("python-pyenchant" ,python-pyenchant)
370371
      ("python-pygit2" ,python-pygit2)
372+
      ("python-pygithub" ,python-pygithub)
371373
      ("python-pyqt" ,python-pyqt)
372374
      ("python-requests" ,python-requests)
373375
      ("python-ruamel.yaml" ,python-ruamel.yaml)

offlate/manager.py

1818
from .systems.tp import TPProject
1919
from .systems.transifex import TransifexProject
2020
from .systems.gitlab import GitlabProject
21+
from .systems.github import GithubProject
2122
from .formats.formatException import UnsupportedFormatException
2223
from .systems.list import *
2324

127128
            for s in self.settings.conf["Generic"].keys():
128129
                settings[s] = self.settings.conf["Generic"][s]
129130
            proj = GitlabProject(settings, name, lang, data)
131+
        if system == GITHUB:
132+
            if not 'Github' in self.settings.conf:
133+
                self.settings.conf['Github'] = {}
134+
            settings = self.settings.conf['Github']
135+
            for s in self.settings.conf["Generic"].keys():
136+
                settings[s] = self.settings.conf["Generic"][s]
137+
            proj = GithubProject(settings, name, lang, data)
130138
        self.project_list[name] = proj
131139
        return proj
132140

198206
                    self.settings.conf["Generic"][s] = None
199207
                settings[s] = self.settings.conf["Generic"][s]
200208
            return GitlabProject.isConfigured(settings)
209+
        elif system == GITHUB:
210+
            if not "Github" in self.settings.conf:
211+
                self.settings.conf["Github"] = {}
212+
            settings = self.settings.conf["Github"]
213+
            for s in self.settings.conf["Generic"].keys():
214+
                if not s in self.settings.conf["Generic"]:
215+
                    self.settings.conf["Generic"][s] = None
216+
                settings[s] = self.settings.conf["Generic"][s]
217+
            return GithubProject.isConfigured(settings)
201218
        else:
202219
            return False

offlate/systems/github.py unknown status 1

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+
from github import Github
22+
from pathlib import Path
23+
24+
class GithubProject(GitProject):
25+
    def __init__(self, conf, name, lang, data = {}):
26+
        GitProject.__init__(self, conf, name, lang, data)
27+
28+
    def updateURI(self):
29+
        repo = self.data['repo']
30+
        if repo.startswith('https://github.com/'):
31+
            self.uri = repo
32+
        elif repo.startswith('github.com/'):
33+
            self.uri = 'https://' + repo
34+
        else:
35+
            self.uri = 'https://github.com/' + repo
36+
        self.branch = self.data['branch']
37+
38+
    def send(self, interface):
39+
        gh = Github(self.conf['token'])
40+
41+
        currentUser = gh.get_user().login
42+
        projectname = self.uri.split('/')[-1]
43+
        projectfullname = urlparse(self.uri).path[1:]
44+
45+
        originproject = gh.get_repo(projectfullname)
46+
        try:
47+
            project = gl.get_repo(currentUser + "/" + projectname)
48+
        except:
49+
            project = gh.get_user().create_fork(originproject)
50+
51+
        try:
52+
            sha = project.get_git_ref('heads/' + self.data['branch']).object.sha
53+
            project.create_git_ref('refs/heads/translation', sha)
54+
        except:
55+
            interface.githubBranchError('translation')
56+
            return
57+
58+
        for mfile in self.translationfiles:
59+
            mfile = mfile['filename']
60+
            try:
61+
                # workaround a bug, where we cannot get the content hash of a single file, by looking
62+
                # for every file in the parent directory.
63+
                contents = project.get_dir_contents(str(Path(mfile).parent), ref='translation')
64+
                for c in contents:
65+
                    if c.path == mfile:
66+
                        sha = c.sha
67+
                        break
68+
                project.update_file(path=mfile, message='Update ' + self.lang + ' translation',
69+
                        content=open(self.basedir + '/current/' + mfile).read(),
70+
                        sha=sha, branch='translation')
71+
            except:
72+
                project.create_file(path=mfile, message='Add ' + self.lang + ' translation',
73+
                        content=open(self.basedir + '/current/' + mfile).read(),
74+
                        branch='translation')
75+
        originproject.create_pull(title='Update ' + self.lang + ' translation',
76+
                body='Automatically submitted using Offlate. Report translation issues to the submitter, and Offlate issues to @roptat, thanks!',
77+
                head=currentUser+':translation',
78+
                base=self.data['branch'],
79+
                maintainer_can_modify=True)
80+
81+
    @staticmethod
82+
    def isConfigured(conf):
83+
        res = GitProject.isConfigured(conf)
84+
        res = res and 'token' in conf and conf['token'] != ''
85+
        return res

offlate/systems/list.py

1313
#   You should have received a copy of the GNU Affero General Public License
1414
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.
1515
####
16-
""" The listo of system connectors. """
16+
""" The list of system connectors. """
1717
1818
TRANSLATION_PROJECT = 0
1919
TRANSIFEX = 1
2020
GITLAB = 2
21+
GITHUB = 3

offlate/ui/editor.py

6060
        self.qd.showMessage(self.qd.tr("Error while creating branch {}.").format(branch))
6161
        self.qd.exec_()
6262
63+
    def githubBranchError(self, branch):
64+
        self.qd = QErrorMessage()
65+
        self.qd.showMessage(self.qd.tr("Error while creating branch {}.").format(branch))
66+
        self.qd.exec_()
67+
6368
class ProjectView(QWidget):
6469
    translationModified = pyqtSignal()
6570

offlate/ui/new.py

6868
        self.combo.addItem(self.tr("The Translation Project"))
6969
        self.combo.addItem(self.tr("Transifex"))
7070
        self.combo.addItem(self.tr("Gitlab"))
71+
        self.combo.addItem(self.tr("Github"))
7172
        self.combo.setCurrentIndex(self.system)
7273
        self.formLayout.addRow(self.combo)
7374

8990
        self.additionalFields.append([])
9091
        self.additionalFields.append([])
9192
        self.additionalFields.append([])
93+
        self.additionalFields.append([])
9294
        
9395
        # Transifex
9496
        self.transifexOrganisation = QLineEdit()

114116
            self.gitlabRepo.setText(self.info['repo'])
115117
            self.gitlabBranch.setText(self.info['branch'])
116118
119+
        # Github
120+
        self.githubRepo = QLineEdit()
121+
        self.githubRepo.textChanged.connect(self.modify)
122+
        githubRepoLabel = QLabel(self.tr('repository'))
123+
        self.additionalFields[GITHUB].append({'label': githubRepoLabel,
124+
            'widget': self.githubRepo})
125+
        self.githubBranch = QLineEdit()
126+
        self.githubBranch.textChanged.connect(self.modify)
127+
        githubBranchLabel = QLabel(self.tr('branch'))
128+
        self.additionalFields[GITHUB].append({'label': githubBranchLabel,
129+
            'widget': self.githubBranch})
130+
        if self.system == GITHUB:
131+
            self.githubRepo.setText(self.info['repo'])
132+
            self.githubBranch.setText(self.info['branch'])
133+
117134
        self.setLayout(hbox)
118135
119136
        self.predefinedprojects.currentItemChanged.connect(self.fill)

138155
        if data['system'] == GITLAB:
139156
            self.gitlabRepo.setText(data['repo'])
140157
            self.gitlabBranch.setText(data['branch'])
158+
        if data['system'] == GITHUB:
159+
            self.githubRepo.setText(data['repo'])
160+
            self.githubBranch.setText(data['branch'])
141161
142162
    def filter(self):
143163
        search = self.searchfield.text()

179199
        if self.getProjectSystem() == GITLAB:
180200
            return {'repo': self.additionalFields[GITLAB][0]['widget'].text(),
181201
                    'branch': self.additionalFields[GITLAB][1]['widget'].text()}
202+
        if self.getProjectSystem() == GITHUB:
203+
            return {'repo': self.additionalFields[GITHUB][0]['widget'].text(),
204+
                    'branch': self.additionalFields[GITHUB][1]['widget'].text()}
182205
        return {}
183206
184207
    def othersystem(self):

setup.py

1313
    packages=find_packages(exclude=['.guix-profile*']),
1414
    python_requires = '>=3',
1515
    install_requires=['polib', 'ruamel.yaml', 'python-dateutil', 'PyQt5', 'pygit2',
16-
        'python-gitlab', 'translation-finder', 'android-stringslib', 'watchdog'],
16+
        'python-gitlab', 'translation-finder', 'android-stringslib', 'watchdog',
17+
        'PyGithub'],
1718
    entry_points={
1819
        'gui_scripts': [
1920
            'offlate=offlate.ui.main:main',