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/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/github/views.py b/github/views.py new file mode 100644 index 0000000..3d6c804 --- /dev/null +++ b/github/views.py @@ -0,0 +1,61 @@ +from django.shortcuts import render +from django.http import HttpResponse +from django.views.decorators.csrf import csrf_exempt + +from trello.views import add_card, get_card_id, move_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') + +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") + +@csrf_exempt +def callback(request): + try: + data = json.loads(request.body.decode('utf-8')) + 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(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/jobs/__init__.py b/jobs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jobs/admin.py b/jobs/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/jobs/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/jobs/apps.py b/jobs/apps.py new file mode 100644 index 0000000..14c323a --- /dev/null +++ b/jobs/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class JobsConfig(AppConfig): + name = 'jobs' diff --git a/jobs/migrations/__init__.py b/jobs/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jobs/models.py b/jobs/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/jobs/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/jobs/tests.py b/jobs/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/jobs/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/jobs/views.py b/jobs/views.py new file mode 100644 index 0000000..6504952 --- /dev/null +++ b/jobs/views.py @@ -0,0 +1,27 @@ +from django.shortcuts import render +from django.http import HttpResponse + +import json +import requests +import os + +from line.views import push_message +from trello.views import get_all_cards + +LINE_USERID = os.getenv('LINE_USERID') +LINE_GROUPID = os.getenv('LINE_GROUPID') + +TRELLO_KEY = os.getenv('TRELLO_KEY') +TRELLO_TOKEN = os.getenv('TRELLO_TOKEN') + +def index(request): + return HttpResponse('jobs app') + + +def trello_to_line(request): + behavior = request.GET.get('behavior') + if behavior == 'due': + get_due() + else: + pass + return HttpResponse('trello to line') 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'), diff --git a/trello/views.py b/trello/views.py index 61181f9..86f69b0 100644 --- a/trello/views.py +++ b/trello/views.py @@ -7,15 +7,14 @@ import os from line.views import push_message +from pprint import pprint -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') +TRELLO_BOARDID = os.getenv('TRELLO_BOARDID') def index(request): return HttpResponse("Hello World") @@ -23,8 +22,7 @@ def index(request): @csrf_exempt def callback(request): try: - action = json.loads(request.body.decode('utf-8'))['action'] - from pprint import pprint + action = json.loads(request.body.decode('utf-8'))['action'] pprint(action) entities = action['display']['entities'] action_type = action['display']['translationKey'] @@ -45,3 +43,67 @@ def callback(request): pass return HttpResponse("callback") +def add_card(idList, desc='', due='', fileSource='', idAttachmentCover='', idBoard='', idCardSource='', idLabels='', idMembers='', keepFromSource='', labels='', name='', pos='top', urlSource=''): + query = { + "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) + +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, + "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 + + +def get_cards(board_id, fields): + r = requests.get(f'https://api.trello.com/1/{board_id}/id/cards') + pprint(r.content) + +def get_due(): + board_id = get_board_id() + fields = ['due'] + get_cards(board_id, fields) + + +def get_board_id(): + return TRELLO_BOARDID + +