forked from herber523/devopsdays-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
102 lines (88 loc) · 2.94 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import json
import time
import glob
from urllib.request import urlretrieve
import yaml
from flask import Flask
from flask import request
from github import Github
from flask import render_template
GITHUB_ACCESS_TOKEN = os.environ.get('GITHUB_ACCESS_TOKEN', '')
app = Flask(__name__, static_url_path='/static')
app.config['FREEZER_DESTINATION'] = 'docs'
g = Github(GITHUB_ACCESS_TOKEN)
def check_pr(pr):
print(pr)
files = [file for file in pr.get_files()]
# check number of file
print(files)
if len(files) != 1:
return False
# check file status
print(files[0].status)
if files[0].status != 'added':
print(files[0].status)
return False
# check filename
login = pr.user.login
filename = files[0].filename.replace("messages/", "")
if 'yaml' in filename:
login_yml = "{login}.yaml".format(login=login)
else:
login_yml = "{login}.yml".format(login=login)
print(filename)
print(login_yml)
if filename != login_yml:
return False
# check yaml format
print(files[0].raw_url)
urlretrieve(files[0].raw_url, filename)
with open(filename, 'r') as stream:
try:
data = yaml.load(stream)
if not data.get('displayname') or not data.get('message'):
return False
except yaml.YAMLError as exc:
print(exc)
return False
os.remove(filename)
return True
@app.route('/', methods=["GET", "POST"])
def index():
if request.method == 'POST':
payload = json.loads(request.data)
if payload.get('action') and payload['action'] in ['opened', 'reopened']:
repo = g.get_repo(payload['repository']['id'])
pr_number = payload['number']
pr = repo.get_pull(pr_number)
if check_pr(pr):
pr.as_issue().create_comment('This is good.')
pr.merge()
else:
pr.as_issue().create_comment('Something wrong.')
return ''
messages = []
for f in glob.glob("messages/*.yml"):
with open(f, 'r') as stream:
data = yaml.load(stream)
try:
messages.append({
'message': data['message'],
'display_name': data['displayname'],
'username': f.replace("messages/", "").replace(".yml", "")
})
except yaml.YAMLError as exc:
print(exc)
for f in glob.glob("messages/*.yaml"):
with open(f, 'r') as stream:
data = yaml.load(stream)
try:
messages.append({
'message': data['message'],
'display_name': data['displayname'],
'username': f.replace("messages/", "").replace(".yml", "")
})
except yaml.YAMLError as exc:
print(exc)
return render_template('index.html', messages=messages)