Skip to content

Commit

Permalink
Merge pull request #15 from gisce/convert_github-hooks_to_package
Browse files Browse the repository at this point in the history
Convert GitHub hooks to package
  • Loading branch information
ecarreras committed Sep 2, 2016
2 parents 8708ed1 + 373d6bb commit 682ffb2
Show file tree
Hide file tree
Showing 20 changed files with 335 additions and 407 deletions.
4 changes: 1 addition & 3 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
[run]
source =
./hooks
./listener.py
source = ./hookshub
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Our hooks for github
[![Build Status](https://travis-ci.org/gisce/github-hooks.svg?branch=master)](https://travis-ci.org/gisce/github-hooks)
[![Coverage Status](https://coveralls.io/repos/github/gisce/github-hooks/badge.svg?branch=master)](https://coveralls.io/github/gisce/github-hooks?branch=master)

## Installation

There's no official release (yet?), but there's a setup.py, so you can install this project with pip or easy-install.

Use the `pip install .` command within the project folder

On requirements.txt you may need the whole git url.

## References

* [Github hooks](https://developer.github.com/v3/activity/events/types/)
Expand Down
3 changes: 3 additions & 0 deletions hookshub/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
import listener
import hooks
File renamed without changes.
40 changes: 21 additions & 19 deletions hooks/github.py → hookshub/hooks/github.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from json import dumps
from os.path import join, isfile
from os.path import join

from webhook import webhook

Expand Down Expand Up @@ -33,15 +33,19 @@ def __init__(self, data):
super(GitHubWebhook, self).__init__(data)
self.origin = 'github'

@property
def ssh_url(self):
return self.json['repository']['ssh_url']

@property
def http_url(self):
return self.json['repository']['clone_url']

@property
def repo_name(self):
return self.json['repository']['name']

@property
def branch_name(self):
branch = 'None'
try:
Expand All @@ -68,34 +72,32 @@ def branch_name(self):
pass
return branch

@property
def status(self):
if self.event == EVENT_STATUS:
return self.json['state']
return 'None'

def get_exe_action(self, action):
exe_path = join(self.actions_path, action)
if action == EVENT_STATUS:
# Action for 'status' event on repository 'powerp-docs'
if action.startswith('{}-powerp-docs'.format(EVENT_STATUS)):
json = {}
json.update({'ssh_url': self.ssh_url()})
json.update({'http_url': self.http_url()})
json.update({'repo-name': self.repo_name()})
json.update({'branch-name': self.branch_name()})
json.update({'ssh_url': self.ssh_url})
json.update({'http_url': self.http_url})
json.update({'repo-name': self.repo_name})
json.update({'branch-name': self.branch_name})
json.update({'state': self.status})
return [exe_path, dumps(json), self.event]
else:
return super(GitHubWebhook, self).get_exe_action(action)

def get_test_action(self, action):
exe_path = join(self.actions_path, action)
if action == EVENT_STATUS:
elif action.startswith('{}-powerp-docs'.format(EVENT_PUSH)):
json = {}
json.update({'ssh_url': self.ssh_url()})
json.update({'http_url': self.http_url()})
json.update({'repo-name': self.repo_name()})
json.update({'branch-name': self.branch_name()})
json.update({'ssh_url': self.ssh_url})
json.update({'http_url': self.http_url})
json.update({'repo-name': self.repo_name})
json.update({'branch-name': self.branch_name})
return [exe_path, dumps(json), self.event]
else:
return super(GitHubWebhook, self).get_test_action(action)
return super(GitHubWebhook, self).get_exe_action(action)

@property
def event(self):
Expand Down Expand Up @@ -188,10 +190,10 @@ def event_actions(self):
# If they start with {event}-{repository}_{name}
event.startswith('{0}-{1}_'.format(self.event, self.repo_name)) or
# If they are named after {event}-{repository}
event == '{0}-{1}'.format(self.event, self.repo_name) or
event == '{0}-{1}.py'.format(self.event, self.repo_name) or
# If they start with {event}_{name}
event.startswith('{0}_'.format(self.event)) or
# If they are named after {event}
event == '{0}'.format(self.event)
event == '{0}.py'.format(self.event)
]
return events
108 changes: 108 additions & 0 deletions hookshub/hooks/github/push-powerp-docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals

import sys
import json
import tempfile
import shutil
from subprocess import Popen, PIPE
from os.path import join


class TempDir(object):
def __init__(self):
self.dir = tempfile.mkdtemp()

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
shutil.rmtree(self.dir)


# Carrega dels arguments. Conte els detalls del event
def arguments():
with open(sys.argv[1], 'r') as jsf:
payload = json.loads(jsf.read())
event = sys.argv[2]
return payload, event

payload, event = arguments()

output = ''
url = payload['ssh_url']
repo_name = payload['repo-name']
branch_name = payload['branch-name']
output += ('Rebut event de <{}> |'.format(event))

# Directori on tenim la documentació en format html
docs_dir = '/tmp/builtin/powerp'

# Mirem de quina branca es tracta i actualitzem el directori del build:
# Si es master el directori sera /powerp/
# Altrament el directori sera /powerp_XXX/
# on XXX es el nom de la branca
if branch_name != 'master' and branch_name != 'None':
docs_dir += "_{}".format(branch_name)

# Creem un directori temporal que guardarà les dades del clone
# Per actualitzar la pagina de la documentacio
with TempDir() as temp:
output += ('Creat Directori temporal: {} |'.format(temp.dir))

# Primer clonem el repositori

# Canviarà la forma de clonar segons tinguem o no branca:
if branch_name != 'None':
output += "Clonant el repositori '{0}', amb la branca '{1}' ...".format(
repo_name, branch_name
)
command = 'git clone {0} --branch {1}'.format(url, branch_name)
else:
output += "Clonant el repositori '{0}' ...".format(repo_name)
command = 'git clone {}'.format(url)

new_clone = Popen(
command.split(), cwd=temp.dir, stdout=PIPE, stderr=PIPE
)
out, err = new_clone.communicate()

if new_clone.returncode != 0:
# Could not clone >< => ABORT
output += 'FAILED TO CLONE: {0}::{1}'.format(out, err)
print(output)
exit(-1)

output += 'OK |'

# Accedim al directori del clone utilitzant el nom del repositori

clone_dir = join(temp.dir, repo_name)

# Instalem dependencies

output += 'Instal.lant dependencies...'
command = 'pip install -r requirements.txt'
dependencies = Popen(
command.split(), cwd=clone_dir, stdout=PIPE, stderr=PIPE
)
out, err = dependencies.communicate()
output += 'OK |'

# Fem build al directori on tenim la pagina des del directori del clone

command = 'mkdocs build -d {} --clean'.format(docs_dir)
output += 'Building mkdocs on {}...'.format(docs_dir)
new_build = Popen(
command.split(), cwd=clone_dir, stdout=PIPE, stderr=PIPE
)
out, err = new_build.communicate()
if new_build.returncode != 0:
output += 'FAILED TO BUILD: {0}::{1}'.format(out, err)
print(output)
exit(-1)
output += 'OK |'

print(output)
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals

import sys
import json
import tempfile
import shutil
from subprocess import Popen, PIPE
from os.path import join


class TempDir(object):
Expand Down Expand Up @@ -48,7 +50,7 @@ def arguments():
# Si es master el directori sera /powerp/
# Altrament el directori sera /powerp_XXX/
# on XXX es el nom de la branca
if branch_name != 'master':
if branch_name != 'master' and branch_name != 'None':
docs_dir += "_{}".format(branch_name)

# Creem un directori temporal que guardarà les dades del clone
Expand All @@ -58,19 +60,32 @@ def arguments():

# Primer clonem el repositori

output += "Clonant el repositori '{0}', amb la branca '{1}' ...".format(
repo_name, branch_name
)
command = 'git clone {0} --branch {1}'.format(url, branch_name)
# Canviarà la forma de clonar segons tinguem o no branca:
if branch_name != 'None':
output += "Clonant el repositori '{0}', amb la branca '{1}' ...".format(
repo_name, branch_name
)
command = 'git clone {0} --branch {1}'.format(url, branch_name)
else:
output += "Clonant el repositori '{0}' ...".format(repo_name)
command = 'git clone {}'.format(url)

new_clone = Popen(
command.split(), cwd=temp.dir, stdout=PIPE, stderr=PIPE
)
out, err = new_clone.communicate()

if new_clone.returncode != 0:
# Could not clone >< => ABORT
output += 'FAILED TO CLONE: {}'.format(out, err)
print(output)
exit(-1)

output += 'OK |'

# Accedim al directori del clone utilitzant el nom del repositori

clone_dir = '{0}/{1}'.format(temp.dir, repo_name)
clone_dir = join(temp.dir, repo_name)

# Instalem dependencies

Expand All @@ -92,4 +107,4 @@ def arguments():
out, err = new_build.communicate()
output += 'OK |'

print (output)
print(output)
8 changes: 6 additions & 2 deletions hooks/gitlab.py → hookshub/hooks/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ def __init__(self, data):
super(GitLabWebhook, self).__init__(data)
self.origin = 'gitlab'

@property
def ssh_url(self):
return self.json['repository']['git_ssh_url']

@property
def http_url(self):
return self.json['repository']['git_http_url']

@property
def event(self):
return self.json['object_kind']

@property
def repo_name(self):
return self.json['repository']['name']

@property
def branch_name(self):
branch = 'None'
try:
Expand Down Expand Up @@ -63,10 +67,10 @@ def event_actions(self):
# If they start with {event}-{repository}_{name}
event.startswith('{0}-{1}_'.format(self.event, self.repo_name)) or
# If they are named after {event}-{repository}
event == '{0}-{1}'.format(self.event, self.repo_name) or
event == '{0}-{1}.py'.format(self.event, self.repo_name) or
# If they start with {event}_{name}
event.startswith('{0}_'.format(self.event)) or
# If they are named after {event}
event == '{0}'.format(self.event)
event == '{0}.py'.format(self.event)
]
return events
File renamed without changes.
6 changes: 1 addition & 5 deletions hooks/webhook.py → hookshub/hooks/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,4 @@ def event_actions(self):

def get_exe_action(self, action):
exe_path = join(self.actions_path, action)
if action == self.event:
return [exe_path, dumps(self.json), self.event]

def get_test_action(self, action):
return self.get_exe_action(action)
return [exe_path, dumps(self.json), self.event]
File renamed without changes.
Loading

0 comments on commit 682ffb2

Please sign in to comment.