diff --git a/.gitignore b/.gitignore index f95d164a6..0bc3db67f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ venv staticfiles .env db.sqlite3 +.venv/* diff --git a/README.md b/README.md index 30cc47c48..acf0abc4e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ -# python-getting-started +# OLCF CI Thing +Whatup scott A barebones Python app, which can easily be deployed to Heroku. This application supports the [Getting Started with Python on Heroku](https://devcenter.heroku.com/articles/getting-started-with-python) article - check it out. diff --git a/client/__init__.py b/client/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/client/__main__.py b/client/__main__.py new file mode 100644 index 000000000..1d579ba39 --- /dev/null +++ b/client/__main__.py @@ -0,0 +1,16 @@ +#!/bin/env python +import time +from client.util import shell +from client import push_event +from client import run_ci + +def try_ci(): + pe = push_event.next() + if pe: + run_ci.run(pe) + else: + print("No push events available right now") + +while True: + try_ci() + time.sleep(10) diff --git a/client/push_event.py b/client/push_event.py new file mode 100644 index 000000000..324a4c651 --- /dev/null +++ b/client/push_event.py @@ -0,0 +1,19 @@ +import requests + + +def next(): + response = requests.get("https://olcf-ci-service.herokuapp.com/github-push-events/pop").json() + if 'data' in response: + return PushEvent.fromGithubEvent(response['data']) + + +class PushEvent(object): + def __init__(self, clone_url, commit): + self.clone_url = clone_url + self.commit = commit + + @classmethod + def fromGithubEvent(cls, event): + clone_url = event['repository']['clone_url'] + commit = event['head_commit']['id'] + return cls(clone_url, commit) diff --git a/client/run_ci.py b/client/run_ci.py new file mode 100644 index 000000000..e8c051a51 --- /dev/null +++ b/client/run_ci.py @@ -0,0 +1,11 @@ +import os +import uuid +from client.util import shell + + +def run(push_event): + repo_dir = "olcf-ci-%s" % uuid.uuid4() + shell("git clone %s %s" % (push_event.clone_url, repo_dir)) + os.chdir(repo_dir) + shell("git checkout %s" % push_event.commit) + shell("./test.exe") diff --git a/client/util.py b/client/util.py new file mode 100644 index 000000000..539c8a8a6 --- /dev/null +++ b/client/util.py @@ -0,0 +1,7 @@ +import subprocess +import shlex + + +def shell(command): + print("$ %s" % command) + subprocess.call(shlex.split(command)) diff --git a/gettingstarted/urls.py b/gettingstarted/urls.py index 608147ae5..87c296180 100644 --- a/gettingstarted/urls.py +++ b/gettingstarted/urls.py @@ -13,4 +13,6 @@ url(r'^$', hello.views.index, name='index'), url(r'^db', hello.views.db, name='db'), url(r'^admin/', include(admin.site.urls)), + url(r'^github-push-events/pop', hello.views.github_pop, name='github_pop'), + url(r'^github-push-events/', hello.views.github, name='github') ] diff --git a/hello/migrations/0002_auto_20161230_1825.py b/hello/migrations/0002_auto_20161230_1825.py new file mode 100644 index 000000000..7d0058b86 --- /dev/null +++ b/hello/migrations/0002_auto_20161230_1825.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.7 on 2016-12-30 18:25 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('hello', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='PushEvent', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('when', models.DateTimeField(auto_now_add=True, verbose_name='date created')), + ('payload', models.TextField()), + ('new', models.BooleanField()), + ], + ), + migrations.AlterField( + model_name='greeting', + name='when', + field=models.DateTimeField(auto_now_add=True, verbose_name='date created'), + ), + ] diff --git a/hello/models.py b/hello/models.py index 89d76fd1b..06a1cb27b 100644 --- a/hello/models.py +++ b/hello/models.py @@ -3,3 +3,8 @@ # Create your models here. class Greeting(models.Model): when = models.DateTimeField('date created', auto_now_add=True) + +class PushEvent(models.Model): + when = models.DateTimeField('date created', auto_now_add=True) + payload = models.TextField() + new = models.BooleanField() diff --git a/hello/templates/push_events.html b/hello/templates/push_events.html new file mode 100644 index 000000000..71c1fe2ed --- /dev/null +++ b/hello/templates/push_events.html @@ -0,0 +1,21 @@ +{% extends "base.html" %} +{% load staticfiles %} + +{% block content %} +
+ + +

Page View Report

+ + + + +
+ +{% endblock %} diff --git a/hello/views.py b/hello/views.py index 8558d8451..df33b822e 100644 --- a/hello/views.py +++ b/hello/views.py @@ -1,7 +1,9 @@ from django.shortcuts import render -from django.http import HttpResponse +from django.http import HttpResponse, JsonResponse +from django.views.decorators.csrf import csrf_exempt +import json -from .models import Greeting +from .models import Greeting, PushEvent # Create your views here. def index(request): @@ -18,3 +20,34 @@ def db(request): return render(request, 'db.html', {'greetings': greetings}) +@csrf_exempt +def github(request): + if request.method == 'GET': + return _show_requests(request) + else: + return _new_request(request) + + +def _new_request(request): + push_event = PushEvent() + push_event.payload = request.body + push_event.new = True + push_event.save() + return HttpResponse("This is the github route") + + +def _show_requests(request): + push_events = PushEvent.objects.all() + json_events = [json.loads(pe.payload) for pe in push_events] + return JsonResponse({'data': json_events}) + + +@csrf_exempt +def github_pop(request): + push_event = PushEvent.objects.order_by('when').filter(new=True).first() + if push_event: + json_event = json.loads(push_event.payload) + push_event.delete() + return JsonResponse({'data': json_event}) + else: + return JsonResponse({}) diff --git a/test.exe b/test.exe new file mode 100755 index 000000000..fc45109e7 --- /dev/null +++ b/test.exe @@ -0,0 +1,2 @@ +#!/bin/bash +echo "It worked!"