Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/9 リポジトリの状態をTrelloに反映する #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added github/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions github/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions github/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class GithubConfig(AppConfig):
name = 'github'
Empty file added github/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions github/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions github/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions github/urls.py
Original file line number Diff line number Diff line change
@@ -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'),
]
61 changes: 61 additions & 0 deletions github/views.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions onigiri/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
1 change: 1 addition & 0 deletions trello/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from . import views

app_name = 'trello'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^callback/$', views.callback, name='callback'),
Expand Down
58 changes: 52 additions & 6 deletions trello/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -45,3 +42,52 @@ 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