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/24 スケジューラー機能を実装する #25

Open
wants to merge 9 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")
Empty file added jobs/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions jobs/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 jobs/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class JobsConfig(AppConfig):
name = 'jobs'
Empty file added jobs/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions jobs/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 jobs/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.
27 changes: 27 additions & 0 deletions jobs/views.py
Original file line number Diff line number Diff line change
@@ -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')
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
78 changes: 70 additions & 8 deletions trello/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@
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")

@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']
Expand All @@ -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