2022-02-09 18:17:00 +01:00
|
|
|
from cmath import log
|
|
|
|
from flask.logging import default_handler
|
2020-05-13 13:13:33 +02:00
|
|
|
from urllib.parse import urlparse, parse_qs
|
2022-02-09 18:17:00 +01:00
|
|
|
import flask
|
2020-05-13 13:13:33 +02:00
|
|
|
import json
|
2022-02-09 18:17:00 +01:00
|
|
|
import logging
|
|
|
|
import logging.handlers
|
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
|
2020-05-13 13:13:33 +02:00
|
|
|
|
|
|
|
buildkite_api_token = os.getenv("BUILDKITE_API_TOKEN", "")
|
|
|
|
|
2022-02-09 18:17:00 +01:00
|
|
|
app = flask.Flask(__name__)
|
|
|
|
app.config["DEBUG"] = False
|
|
|
|
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
|
|
|
|
errHandler = logging.FileHandler('error.log', encoding='utf-8',)
|
|
|
|
errHandler.setLevel(logging.ERROR)
|
|
|
|
errHandler.setFormatter(formatter)
|
|
|
|
app.logger.addHandler(errHandler)
|
|
|
|
rotatingHandler = logging.handlers.TimedRotatingFileHandler('info.log', when='D', encoding='utf-8', backupCount=8)
|
|
|
|
rotatingHandler.setFormatter(formatter)
|
|
|
|
app.logger.addHandler(rotatingHandler)
|
|
|
|
app.logger.setLevel(logging.INFO)
|
|
|
|
stdoutLog = logging.StreamHandler()
|
|
|
|
stdoutLog.setFormatter(formatter)
|
|
|
|
app.logger.addHandler(stdoutLog)
|
|
|
|
app.logger.removeHandler(default_handler)
|
2020-05-13 13:13:33 +02:00
|
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
def home():
|
|
|
|
return "Hi LLVM!"
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/build', methods=['POST', 'GET'])
|
|
|
|
def build():
|
|
|
|
app.logger.info('request: %s %s', flask.request, flask.request.url)
|
|
|
|
app.logger.info('headers: %s', flask.request.headers)
|
|
|
|
if flask.request.method == 'POST':
|
|
|
|
app.logger.info('data: %s', flask.request.data)
|
|
|
|
app.logger.info('form: %s', flask.request.form)
|
|
|
|
url = urlparse(flask.request.url)
|
|
|
|
params = parse_qs(url.query)
|
|
|
|
build_env = {}
|
|
|
|
for k, v in params.items():
|
|
|
|
if len(v) == 1:
|
|
|
|
build_env['ph_' + k] = v[0]
|
2020-12-09 17:23:01 +01:00
|
|
|
refspec = 'main'
|
2020-09-29 17:49:38 +02:00
|
|
|
if 'ph_scripts_refspec' in build_env:
|
|
|
|
refspec = build_env['ph_scripts_refspec']
|
2020-05-13 13:13:33 +02:00
|
|
|
build_request = {
|
|
|
|
'commit': 'HEAD',
|
2020-09-29 17:49:38 +02:00
|
|
|
'branch': refspec,
|
2020-05-13 13:13:33 +02:00
|
|
|
'env': build_env,
|
2020-06-04 15:04:42 +02:00
|
|
|
'message': f'D{build_env["ph_buildable_revision"]}',
|
2020-05-13 13:13:33 +02:00
|
|
|
}
|
|
|
|
app.logger.info('buildkite request: %s', build_request)
|
|
|
|
headers = {'Authorization': f'Bearer {buildkite_api_token}'}
|
|
|
|
response = requests.post(
|
|
|
|
'https://api.buildkite.com/v2/organizations/llvm-project'
|
2020-05-14 12:57:38 +02:00
|
|
|
'/pipelines/diff-checks/builds',
|
2020-05-13 13:13:33 +02:00
|
|
|
json=build_request,
|
|
|
|
headers=headers)
|
|
|
|
app.logger.info('buildkite response: %s %s', response.status_code, response.text)
|
|
|
|
rjs = json.loads(response.text)
|
2020-05-25 16:42:40 +02:00
|
|
|
return rjs['web_url']
|
2020-05-13 13:13:33 +02:00
|
|
|
else:
|
|
|
|
return "expected POST request"
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host='0.0.0.0:8080')
|