Compare commits
3 Commits
main
...
wip/github
Author | SHA1 | Date |
---|---|---|
Emmanuel Cazenave | 2773962b43 | |
Emmanuel Cazenave | e249fd54d2 | |
Emmanuel Cazenave | c9be406aad |
|
@ -0,0 +1,34 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.18 on 2019-04-07 17:35
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('base', '0012_job'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='GithubConnector',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=50, verbose_name='Title')),
|
||||
('description', models.TextField(verbose_name='Description')),
|
||||
('slug', models.SlugField(unique=True, verbose_name='Identifier')),
|
||||
('api_url', models.URLField(help_text='Github API endpoint', max_length=400, verbose_name='Github API endpoint')),
|
||||
('github_user', models.URLField(max_length=400, verbose_name='Github user')),
|
||||
('github_repo', models.CharField(max_length=128, verbose_name='Github repo')),
|
||||
('github_token', models.CharField(max_length=128, verbose_name='Github token')),
|
||||
('users', models.ManyToManyField(blank=True, to='base.ApiUser')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Github connector',
|
||||
},
|
||||
),
|
||||
]
|
|
@ -0,0 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.18 on 2019-04-07 17:45
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('github', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='githubconnector',
|
||||
name='github_user',
|
||||
field=models.CharField(max_length=400, verbose_name='Github user'),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,106 @@
|
|||
# passerelle.contrib.iws
|
||||
# Copyright (C) 2016 Entr'ouvert
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Affero General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from passerelle.base.models import BaseResource
|
||||
from passerelle.utils.api import endpoint
|
||||
from passerelle.utils.jsonresponse import APIError
|
||||
|
||||
|
||||
SCHEMA = {
|
||||
"$schema": "http://json-schema.org/draft-03/schema#",
|
||||
"title": "Planitech create issue",
|
||||
"description": "",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {
|
||||
"description": "title",
|
||||
"type": "string",
|
||||
"required": True
|
||||
},
|
||||
"description": {
|
||||
"description": "Description",
|
||||
"type": "string",
|
||||
"required": True
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class GithubConnector(BaseResource):
|
||||
api_url = models.URLField(
|
||||
max_length=400, verbose_name=_('Github API endpoint'),
|
||||
help_text=_('Github API endpoint'))
|
||||
github_user = models.CharField(
|
||||
max_length=400, verbose_name=_('Github user'))
|
||||
github_repo = models.CharField(max_length=128, verbose_name=_('Github repo'))
|
||||
github_token = models.CharField(max_length=128, verbose_name=_('Github token'))
|
||||
|
||||
category = _('Business Process Connectors')
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Github connector')
|
||||
|
||||
@endpoint(methods=['get'], perm='can_access')
|
||||
def getissue(self, request, issue_num):
|
||||
url = '%s/repos/%s/%s/issues/%s' % (
|
||||
self.api_url, self.github_user, self.github_repo, issue_num)
|
||||
response = self.requests.get(url, auth=(self.github_user, self.github_token))
|
||||
|
||||
try:
|
||||
response.raise_for_status()
|
||||
except Exception:
|
||||
raise APIError('Something terrible happened')
|
||||
|
||||
data = response.json()
|
||||
res = {
|
||||
'data': {
|
||||
'title': data['title'],
|
||||
'state': data['state']
|
||||
}
|
||||
}
|
||||
return res
|
||||
|
||||
@endpoint(perm='can_access',
|
||||
post={
|
||||
'description': _('Create github issue'),
|
||||
'request_body': {
|
||||
'schema': {
|
||||
'application/json': SCHEMA
|
||||
}
|
||||
}
|
||||
})
|
||||
def createissue(self, request, post_data):
|
||||
url = '%s/repos/%s/%s/issues' % (
|
||||
self.api_url, self.github_user, self.github_repo)
|
||||
|
||||
to_send = {
|
||||
'title': post_data['title'],
|
||||
'body': post_data['description']
|
||||
}
|
||||
response = self.requests.post(
|
||||
url, auth=(self.github_user, self.github_token), json=to_send)
|
||||
res = response.json()
|
||||
|
||||
return {
|
||||
'data': {
|
||||
'number': res['number'],
|
||||
'state': res['state']
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue