From 624979d1b09f9cea78e9e5ea3b92afea1844f584 Mon Sep 17 00:00:00 2001 From: chanyou0311 Date: Wed, 1 Nov 2017 14:35:53 +0900 Subject: [PATCH 1/6] [add]start github app --- github/__init__.py | 0 github/admin.py | 3 +++ github/apps.py | 5 +++++ github/migrations/__init__.py | 0 github/models.py | 3 +++ github/tests.py | 3 +++ github/views.py | 19 +++++++++++++++++++ 7 files changed, 33 insertions(+) create mode 100644 github/__init__.py create mode 100644 github/admin.py create mode 100644 github/apps.py create mode 100644 github/migrations/__init__.py create mode 100644 github/models.py create mode 100644 github/tests.py create mode 100644 github/views.py diff --git a/github/__init__.py b/github/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/github/admin.py b/github/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/github/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/github/apps.py b/github/apps.py new file mode 100644 index 0000000..b912db3 --- /dev/null +++ b/github/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class GithubConfig(AppConfig): + name = 'github' diff --git a/github/migrations/__init__.py b/github/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/github/models.py b/github/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/github/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/github/tests.py b/github/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/github/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/github/views.py b/github/views.py new file mode 100644 index 0000000..8a61471 --- /dev/null +++ b/github/views.py @@ -0,0 +1,19 @@ +from django.shortcuts import render +from django.http import HttpResponse +from django.views.decorators.csrf import csrf_exempt + +import json +import requests + +def index(request): + return HttpResponse("Hello GitHub") + +@csrf_exempt +def callback(request): + try: + data = json.loads(request.body.decode('utf-8')) + from pprint import pprint + pprint(data) + except Exception as e: + pass + return HttpResponse("callback") From 2a90763baf7b29c34194380284d42b770e2eb667 Mon Sep 17 00:00:00 2001 From: chanyou0311 Date: Wed, 1 Nov 2017 14:40:40 +0900 Subject: [PATCH 2/6] [clean] add app_name in urls.py --- github/urls.py | 9 +++++++++ onigiri/urls.py | 1 + trello/urls.py | 1 + 3 files changed, 11 insertions(+) create mode 100644 github/urls.py diff --git a/github/urls.py b/github/urls.py new file mode 100644 index 0000000..7e08f10 --- /dev/null +++ b/github/urls.py @@ -0,0 +1,9 @@ +from django.conf.urls import url + +from . import views + +app_name = 'github' +urlpatterns = [ + url(r'^$', views.index, name='index'), + url(r'^callback/$', views.callback, name='callback'), +] diff --git a/onigiri/urls.py b/onigiri/urls.py index 05730ae..64b6fd2 100644 --- a/onigiri/urls.py +++ b/onigiri/urls.py @@ -21,5 +21,6 @@ url(r'^$', lambda r: HttpResponse('response ok.')), url(r'^line/', include('line.urls')), url(r'^trello/', include('trello.urls')), + url(r'^github/', include('github.urls')), url(r'^admin/', admin.site.urls), ] diff --git a/trello/urls.py b/trello/urls.py index 8d55e2c..dc4f780 100644 --- a/trello/urls.py +++ b/trello/urls.py @@ -2,6 +2,7 @@ from . import views +app_name = 'trello' urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^callback/$', views.callback, name='callback'), From 18f506210851efa2b374876b610c5657bea7e5da Mon Sep 17 00:00:00 2001 From: chanyou0311 Date: Wed, 1 Nov 2017 17:56:42 +0000 Subject: [PATCH 3/6] [add] add trello card if issue is opened --- github/views.py | 22 ++++++++++++++++++++-- trello/views.py | 29 +++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/github/views.py b/github/views.py index 8a61471..b81d3f7 100644 --- a/github/views.py +++ b/github/views.py @@ -2,9 +2,16 @@ from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt +from trello.views import add_card + +import os import json import requests +TODO_ID = os.getenv('TODO_ID') +DOING_ID = os.getenv('DOING_ID') +DONE_ID = os.getenv('DONE_ID') + def index(request): return HttpResponse("Hello GitHub") @@ -12,8 +19,19 @@ def index(request): def callback(request): try: data = json.loads(request.body.decode('utf-8')) - from pprint import pprint - pprint(data) + event_type = request.META['HTTP_X_GITHUB_EVENT'] + action = data['action'] + if event_type == 'issues': + if action == 'opened' or action == 'reopened': + title = data['issue']['title'] + body = data['issue']['body'] + url = data['issue']['html_url'] + description = url + '\n\n' + body + add_card(TODO_ID, name=title, desc=description) + elif action == 'closed': + pass + elif event_type == 'label': + pass except Exception as e: pass return HttpResponse("callback") diff --git a/trello/views.py b/trello/views.py index 61181f9..497f0a9 100644 --- a/trello/views.py +++ b/trello/views.py @@ -8,14 +8,11 @@ from line.views import push_message -REPLY_ENDPOINT = 'https://api.line.me/v2/bot/message/reply' -ACCESS_TOKEN = os.getenv('LINE_ACCESS_TOKEN') LINE_USERID = os.getenv('LINE_USERID') LINE_GROUPID = os.getenv('LINE_GROUPID') -HEADER = { - "Content-Type": "application/json", - "Authorization": "Bearer " + ACCESS_TOKEN -} + +TRELLO_KEY = os.getenv('TRELLO_KEY') +TRELLO_TOKEN = os.getenv('TRELLO_TOKEN') def index(request): return HttpResponse("Hello World") @@ -45,3 +42,23 @@ def callback(request): pass return HttpResponse("callback") +def add_card(idList, closed=False, desc='', due='', fileSource='', idAttachmentCover='', idBoard='', idCardSource='', idLabels='', idMembers='', keepFromSource='', labels='', name='', pos='top', urlSource=''): + query = { + "closed": closed, + "desc": desc, + "due": due, + "fileSource": fileSource, + "idAttachmentCover": idAttachmentCover, + "idBoard": idBoard, + "idCardSource": idCardSource, + "idLabels": idLabels, + "idList": idList, + "idMembers": idMembers, + "keepFromSource": keepFromSource, + "labels": labels, + "name": name, + "pos": pos, + "urlSource": urlSource, + } + r = requests.post("https://api.trello.com/1/cards", json=query, params={"key": TRELLO_KEY, "token": TRELLO_TOKEN}) + print(r.text) From a71075b2db46e5fe10ccb31821739db42e6d5d33 Mon Sep 17 00:00:00 2001 From: chanyou0311 Date: Wed, 1 Nov 2017 19:41:31 +0000 Subject: [PATCH 4/6] [add]github function --- github/views.py | 28 ++++++++++++++++++++++++++-- trello/views.py | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/github/views.py b/github/views.py index b81d3f7..2b3036e 100644 --- a/github/views.py +++ b/github/views.py @@ -2,7 +2,7 @@ from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt -from trello.views import add_card +from trello.views import add_card, get_card_id, move_card import os import json @@ -12,6 +12,11 @@ DOING_ID = os.getenv('DOING_ID') DONE_ID = os.getenv('DONE_ID') +ISSUES_LIST_ID = os.getenv('ISSUES_LIST_ID') +PULLREQUESTS_LIST_ID = os.getenv('PULLREQUESTS_LIST_ID') +WIP_LIST_ID = os.getenv('WIP_LIST_ID') +CLOSED_ID = os.getenv('CLOSED_LIST_ID') + def index(request): return HttpResponse("Hello GitHub") @@ -27,11 +32,30 @@ def callback(request): body = data['issue']['body'] url = data['issue']['html_url'] description = url + '\n\n' + body - add_card(TODO_ID, name=title, desc=description) + add_card(ISSUES_LIST_ID, name=title, desc=description) elif action == 'closed': pass elif event_type == 'label': pass + elif event_type == 'pull_request': + title = data['pull_request']['title'] + body = data['pull_request']['body'] + url = data['pull_request']['html_url'] + pr_id = data['pull_request']['id'] + description = url + '\n\n' + body + '\n\nid:' + pr_id + if action == 'opened' or action_type == 'reopened': + add_card(PULLREQUESTS_LIST_ID, name=title, desc=description) + elif action == 'labeled': + for label in data['pull_request']['labels'] + if label['name'] == 'WIP': + card_id = get_card_id('id:'+pr_id) + move_card(card_id, WIP_LIST_ID) + else: + pass + else: + pass + else: + pass except Exception as e: pass return HttpResponse("callback") diff --git a/trello/views.py b/trello/views.py index 497f0a9..6f6c542 100644 --- a/trello/views.py +++ b/trello/views.py @@ -42,9 +42,8 @@ def callback(request): pass return HttpResponse("callback") -def add_card(idList, closed=False, desc='', due='', fileSource='', idAttachmentCover='', idBoard='', idCardSource='', idLabels='', idMembers='', keepFromSource='', labels='', name='', pos='top', urlSource=''): +def add_card(idList, desc='', due='', fileSource='', idAttachmentCover='', idBoard='', idCardSource='', idLabels='', idMembers='', keepFromSource='', labels='', name='', pos='top', urlSource=''): query = { - "closed": closed, "desc": desc, "due": due, "fileSource": fileSource, @@ -62,3 +61,33 @@ def add_card(idList, closed=False, desc='', due='', fileSource='', idAttachmentC } r = requests.post("https://api.trello.com/1/cards", json=query, params={"key": TRELLO_KEY, "token": TRELLO_TOKEN}) print(r.text) + +def move_card(idCard, idList, closed='', desc='', due='', fileSource='', idAttachmentCover='', idBoard='', idCardSource='', idLabels='', idMembers='', keepFromSource='', labels='', name='', pos='top', urlSource='', dueComplete='', closed=''): + query = { + "closed": closed, + "desc": desc, + "due": due, + "fileSource": fileSource, + "idAttachmentCover": idAttachmentCover, + "idBoard": idBoard, + "idCardSource": idCardSource, + "idLabels": idLabels, + "idList": idList, + "idMembers": idMembers, + "keepFromSource": keepFromSource, + "labels": labels, + "name": name, + "pos": pos, + "urlSource": urlSource, + "dueComplete": dueComplete + } + r = requests.put(f'https://api.trello.com/1/cards/{idCard}', json=query, params={"key": TRELLO_KEY, "token": TRELLO_TOKEN}) + print(r.text) + + +def get_card_id(search_id): + query = {"query": search_id} + cards = request.get("https://api.trello.com/1/search", json=query, params={"key": TRELLO_KEY, "token": TRELLO_TOKEN}).json()['cards'] + card_id = cards[0]['id'] + return card_id + From e9ce035da2ca48df18032fed29da90bce81efce4 Mon Sep 17 00:00:00 2001 From: chanyou0311 Date: Thu, 2 Nov 2017 04:43:52 +0900 Subject: [PATCH 5/6] =?UTF-8?q?[fix]=20closed=E5=BC=95=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E9=87=8D=E8=A4=87=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trello/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trello/views.py b/trello/views.py index 6f6c542..d0780a6 100644 --- a/trello/views.py +++ b/trello/views.py @@ -62,7 +62,7 @@ def add_card(idList, desc='', due='', fileSource='', idAttachmentCover='', idBo r = requests.post("https://api.trello.com/1/cards", json=query, params={"key": TRELLO_KEY, "token": TRELLO_TOKEN}) print(r.text) -def move_card(idCard, idList, closed='', desc='', due='', fileSource='', idAttachmentCover='', idBoard='', idCardSource='', idLabels='', idMembers='', keepFromSource='', labels='', name='', pos='top', urlSource='', dueComplete='', closed=''): +def move_card(idCard, idList, closed='', desc='', due='', fileSource='', idAttachmentCover='', idBoard='', idCardSource='', idLabels='', idMembers='', keepFromSource='', labels='', name='', pos='top', urlSource='', dueComplete=''): query = { "closed": closed, "desc": desc, From 8c69396399d510841aa7d5df0903573faaa6b19d Mon Sep 17 00:00:00 2001 From: chanyou0311 Date: Thu, 2 Nov 2017 04:46:45 +0900 Subject: [PATCH 6/6] =?UTF-8?q?[fix]=E3=83=A9=E3=83=99=E3=83=AB=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=82=A2?= =?UTF-8?q?=E3=82=A6=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- github/views.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/github/views.py b/github/views.py index 2b3036e..3d6c804 100644 --- a/github/views.py +++ b/github/views.py @@ -45,13 +45,13 @@ def callback(request): description = url + '\n\n' + body + '\n\nid:' + pr_id if action == 'opened' or action_type == 'reopened': add_card(PULLREQUESTS_LIST_ID, name=title, desc=description) - elif action == 'labeled': - for label in data['pull_request']['labels'] - if label['name'] == 'WIP': - card_id = get_card_id('id:'+pr_id) - move_card(card_id, WIP_LIST_ID) - else: - pass + # elif action == 'labeled': + # for label in data['pull_request']['labels'] + # if label['name'] == 'WIP': + # card_id = get_card_id('id:'+pr_id) + # move_card(card_id, WIP_LIST_ID) + # else: + # pass else: pass else: