retour résultat API GitLab

This commit is contained in:
nyanloutre 2018-11-04 14:51:04 +01:00
parent 0ac55a792e
commit 1ea5329cd0
1 changed files with 23 additions and 9 deletions

30
server.py Normal file → Executable file
View File

@ -1,18 +1,23 @@
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p "python3.withPackages(ps: [ps.pyramid ps.python-gitlab])"
from wsgiref.simple_server import make_server from wsgiref.simple_server import make_server
from pyramid.config import Configurator from pyramid.config import Configurator
from pyramid.view import view_config, view_defaults from pyramid.view import view_config, view_defaults
from pyramid.httpexceptions import HTTPNotFound from pyramid.httpexceptions import HTTPNotFound
from subprocess import check_call from subprocess import check_call, CalledProcessError
import urllib.request import urllib.request
import tarfile import tarfile
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from multiprocessing import Pool from multiprocessing import Pool
from gitlab import Gitlab
whitelist = ['nyanloutre/site-musique'] whitelist = ['nyanloutre/site-musique']
secret = 'xxxxxxxxxxxxxxxxxxxxxx' secret = open('secret', 'r').readline().splitlines()[0]
gitlab_token = open('gitlab_token', 'r').readline().splitlines()[0]
out_dir = './run' out_dir = './run'
def build(payload): def build(payload, gl):
gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': 'running'})
print("push from " + payload['user_name']) print("push from " + payload['user_name'])
print("repo: " + payload['project']['path_with_namespace']) print("repo: " + payload['project']['path_with_namespace'])
print("commit: " + payload['checkout_sha']) print("commit: " + payload['checkout_sha'])
@ -24,13 +29,19 @@ def build(payload):
with tarfile.open(fileobj=gitlab_archive, mode='r|gz') as gitlab_repo_files: with tarfile.open(fileobj=gitlab_archive, mode='r|gz') as gitlab_repo_files:
gitlab_repo_files.extractall(path=temp_dir.name) gitlab_repo_files.extractall(path=temp_dir.name)
check_call(['ls', '-lha', repo_dir]) check_call(['ls', '-lha', repo_dir])
try:
check_call(['nix-build', '-o', out_dir + '/' + payload['project']['path_with_namespace'], repo_dir]) check_call(['nix-build', '-o', out_dir + '/' + payload['project']['path_with_namespace'], repo_dir])
except CalledProcessError:
return {'payload': payload, 'gitlab': gl, 'status': 'failed'}
def build_success(payload): return {'payload': payload, 'gitlab': gl, 'status': 'success'}
print("build complete")
def build_success(result):
gl = result['gitlab']
payload = result['payload']
gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': result['status']})
print("build: " + result['status'])
def build_failed(payload):
print("build failed")
@view_defaults( @view_defaults(
route_name="gitlab_payload", renderer="json", request_method="POST" route_name="gitlab_payload", renderer="json", request_method="POST"
@ -40,11 +51,13 @@ class PayloadView(object):
def __init__(self, request): def __init__(self, request):
self.request = request self.request = request
self.payload = self.request.json self.payload = self.request.json
self.gl = Gitlab('https://gitlab.com', private_token=gitlab_token)
@view_config(header="X-Gitlab-Event:Push Hook") @view_config(header="X-Gitlab-Event:Push Hook")
def payload_push(self): def payload_push(self):
if self.payload['project']['path_with_namespace'] in whitelist and self.request.headers['X-Gitlab-Token'] == secret: if self.payload['project']['path_with_namespace'] in whitelist and self.request.headers['X-Gitlab-Token'] == secret:
pool.apply_async(build, (self.payload,), callback=build_success, error_callback=build_failed) self.gl.projects.get(self.payload['project']['path_with_namespace']).commits.get(self.payload['checkout_sha']).statuses.create({'state': 'pending'})
pool.apply_async(build, (self.payload, self.gl), callback=build_success)
return "build started" return "build started"
else: else:
raise HTTPNotFound raise HTTPNotFound
@ -60,4 +73,5 @@ if __name__ == "__main__":
app = config.make_wsgi_app() app = config.make_wsgi_app()
server = make_server("127.0.0.1", 52350, app) server = make_server("127.0.0.1", 52350, app)
print('listening ...')
server.serve_forever() server.serve_forever()