-
Notifications
You must be signed in to change notification settings - Fork 4
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
Api #7
Merged
Api #7
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8d43047
controllers listening for hooks
cd3cd10
travis fixture
798d469
webhook fixture payloads
5e17536
add serve command
f21411d
Merge remote-tracking branch 'origin/master' into api
c2bc21a
cp, gimme that travis payload pls
ecd405b
working mapped payloads
d4d7cb7
missing dependency
0c447fa
check webhook security
283c1c9
flatten form structure
dda8821
only build when tagged by travis
13b4a4a
explore using parameterised tests, fixes #6
998d34e
actually run the builder
2c80180
note hack
9893bae
beginnings of a way to stream log output
960d810
run build in background thread
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import unicode_literals | ||
|
||
import pyramid.threadlocal | ||
|
||
from thed import api | ||
|
||
from . import hooks | ||
|
||
|
||
registry = pyramid.threadlocal.get_current_registry() | ||
settings = registry.settings | ||
|
||
|
||
def includeme(config): | ||
hooks.HookController.scan(config) | ||
|
||
|
||
def create(**overrides): | ||
return api.Application.create( | ||
{}, | ||
includes=['thed.api.resources', 'thed.api.controllers'], | ||
**overrides | ||
) | ||
|
||
|
||
def create_app(**overrides): | ||
def hook(config): | ||
includeme(config) | ||
config.add_view_predicate('resource', api.predicates.ResourcePredicate) | ||
config.scan() | ||
|
||
return create(hook=hook, **overrides) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from __future__ import unicode_literals | ||
|
||
import github | ||
import travis | ||
from github import GithubForm | ||
from travis import TravisForm | ||
|
||
from bob.builders.ubuntu import UbuntuBuilder | ||
|
||
|
||
def build(github_organization, github_repo, commit_hash_or_tag): | ||
from bob.api import settings | ||
|
||
# HACK: until we figure out settings.ini | ||
settings = settings or {} | ||
working_dir = settings.get('working_dir', '~/work') | ||
output_dir = settings.get('output_dir', '~/out') | ||
builder = UbuntuBuilder( | ||
github_repo, working_dir, output_dir | ||
) | ||
builder.prepare_workspace( | ||
github_organization, github_repo, commit_hash_or_tag | ||
) | ||
|
||
builder.parse_options() | ||
try: | ||
builder.prepare_system() | ||
builder.build() | ||
package_name = builder.package(commit_hash_or_tag) | ||
builder.upload(package_name) | ||
builder.notify_success(commit_hash_or_tag) | ||
except Exception as ex: | ||
builder.notify_failure(commit_hash_or_tag, ex) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from __future__ import unicode_literals | ||
|
||
import pilo | ||
|
||
|
||
class GithubForm(pilo.Form): | ||
|
||
commit = pilo.fields.String('ref') | ||
|
||
@commit.filter | ||
def commit(self, value): | ||
if 'refs/tags' not in value: | ||
return pilo.NONE | ||
return value | ||
|
||
name = pilo.fields.String('repository.name') | ||
|
||
organization = pilo.fields.String('repository.organization') | ||
|
||
build = pilo.fields.Boolean(default=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import unicode_literals | ||
from hashlib import sha256 | ||
|
||
import semantic_version | ||
import pilo | ||
|
||
|
||
user_tokens = { | ||
'mjallday': 'irgy9pphBTydWzVYskRq' | ||
} | ||
|
||
|
||
class TravisForm(pilo.Form): | ||
|
||
commit = pilo.fields.String() | ||
|
||
success = pilo.fields.String('result_message') | ||
|
||
name = pilo.fields.String('repository.name') | ||
|
||
organization = pilo.fields.String('repository.owner_name') | ||
|
||
branch = pilo.fields.String() | ||
|
||
@success.munge | ||
def success(self, value): | ||
return value in ('Passed', 'Fixed') | ||
|
||
build = pilo.fields.Boolean(default=False) | ||
|
||
@build.compute | ||
def build(self): | ||
# branch is the tag on travis. since the tag should look like 1.0.0 | ||
# we can check if it is parseable as a semver to decide if this test | ||
# run was for a build (tag) or a commit. on tag we can say build = True | ||
# if the tests passed | ||
commit = self['branch'] | ||
if commit and commit[0] == 'v': | ||
commit = commit[1:] | ||
try: | ||
semantic_version.Version(commit) | ||
except ValueError: | ||
return False | ||
else: | ||
return self['success'] | ||
|
||
|
||
def compute_travis_security(headers, form): | ||
auth_header = headers['Authorization'] | ||
organization = form['organization'] | ||
repo_name = form['name'] | ||
is_secure = any( | ||
sha256( | ||
'{}/{}{}'.format(organization, repo_name, token) | ||
).hexdigest() == auth_header | ||
for token in user_tokens.itervalues() | ||
) | ||
return is_secure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import unicode_literals | ||
|
||
from thed import api | ||
|
||
from . import forms | ||
|
||
|
||
@api.Resource.nest('hooks') | ||
class HookResource(api.Resource): | ||
|
||
pass | ||
|
||
|
||
@api.RestController.register('hooks', context=HookResource) | ||
class HookController(api.RestController): | ||
|
||
@api.decorators.view_config(name='github', request_method='POST') | ||
def github(self): | ||
result = forms.GithubForm(self.request.json) | ||
return api.Response('github.created') | ||
|
||
@api.decorators.view_config(name='travis', request_method='POST') | ||
def travis(self): | ||
result = forms.TravisForm(self.request.json) | ||
if result['build']: | ||
forms.build( | ||
result['organization'], result['name'], result['commit'] | ||
) | ||
|
||
def iterate_response(): | ||
for c in 'travis.created': | ||
import time | ||
yield str(c) | ||
time.sleep(0.1) | ||
|
||
return api.Response(app_iter=iterate_response()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
from __future__ import unicode_literals | ||
|
||
from . import build | ||
from . import build, serve | ||
|
||
|
||
def add_commands(group): | ||
group.add_command(build.group) | ||
group.add_command(serve.serve) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import unicode_literals | ||
import logging | ||
import wsgiref.simple_server | ||
|
||
import click | ||
|
||
from bob import api | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Dispatcher(object): | ||
|
||
def __init__(self, app): | ||
self.app = app | ||
|
||
def __call__(self, environ, start_response): | ||
return self.app(environ, start_response) | ||
|
||
|
||
@click.command('serve') | ||
@click.option('--port', default=6543) | ||
@click.option('--host', default='0.0.0.0') | ||
def serve(port, host): | ||
dispatcher = Dispatcher(api.create_app()) | ||
server = wsgiref.simple_server.make_server( | ||
host, port, dispatcher, | ||
) | ||
logger.info('serving on %s:%s ...', *server.server_address) | ||
server.serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__author__ = 'marshall' | ||
from __future__ import unicode_literals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from __future__ import unicode_literals | ||
import json | ||
import os | ||
|
||
|
||
def get_for_reals_path(file_name): | ||
return os.path.join( | ||
os.path.dirname( | ||
os.path.realpath(__file__) | ||
), | ||
file_name | ||
) | ||
|
||
|
||
def load_json(path): | ||
with open(path) as f: | ||
return json.load(f) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1, use venusian.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not sure i understand. can you expand or clarify?