Skip to content

Commit

Permalink
Merge pull request #1 from jpopelka/github-app
Browse files Browse the repository at this point in the history
Changes suggested in review
  • Loading branch information
marusinm authored Oct 10, 2019
2 parents c6935e3 + ccd9b32 commit 49d0d5d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 39 deletions.
19 changes: 6 additions & 13 deletions Dockerfile.app → Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
FROM registry.fedoraproject.org/fedora:30

ENV LANG=en_US.UTF-8
# nicer output from the playbook run
ENV ANSIBLE_STDOUT_CALLBACK=debug
ENV LANG=en_US.UTF-8 \
ANSIBLE_STDOUT_CALLBACK=debug \
USER=release-bot \
HOME=/home/release-bot

# Ansible doesn't like /tmp
COPY files/ /src/files/

USER 0

RUN mkdir /home/release-bot && chmod 0776 /home/release-bot

# Install packages first and reuse the cache as much as possible
RUN dnf install -y ansible \
&& cd /src/ \
Expand All @@ -24,9 +22,4 @@ COPY release_bot/ /src/release_bot/
RUN cd /src/ \
&& ansible-playbook -vv -c local -i localhost, files/recipe.yaml


USER 1001
ENV USER=release-bot
ENV HOME=/home/release-bot

CMD ["release-bot -c /home/release-bot/.config/conf.yaml"]
CMD ["/usr/bin/run.sh"]
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
.PHONY: test

IMAGE_NAME := usercont/release-bot
IMAGE_NAME_DEV := usercont/release-bot:dev
IMAGE_NAME := usercont/release-bot:dev
TEST_IMAGE_NAME := release-bot-tests

image: files/install-rpm-packages.yaml files/recipe.yaml
docker build --rm -f Dockerfile.app --tag=$(IMAGE_NAME) .
docker build --rm -f Dockerfile --tag=$(IMAGE_NAME) .

image-test:
docker build --tag=$(TEST_IMAGE_NAME) -f Dockerfile.test .
Expand Down
20 changes: 8 additions & 12 deletions files/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
hosts: all
vars:
home_path: "{{ lookup('env', 'HOME') }}"
ansible_bender:
base_image: FROM registry.fedoraproject.org/fedora:30
#base_image: fedora:29
target_image:
name: docker.io/usercont/release-bot
environment:
FLASK_APP: releasebot.service.web_hook
cmd: flask-3 run -h 0.0.0.0
working_container:
volumes:
- '{{ playbook_dir }}:/src:Z'
tasks:
- name: Create home
file:
state: directory
path: "{{ home_path }}"
mode: 0776
- name: Create some home dirs
file:
state: directory
Expand Down Expand Up @@ -52,4 +46,6 @@
name: /src
executable: pip3
- name: Clean all the cache files (especially pip)
shell: rm -rf ~/.cache/*
file:
state: absent
path: ~/.cache/
4 changes: 1 addition & 3 deletions files/run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/bash

export RELEASE_BOT_HOME=/home/release-bot

exec release-bot -c /home/release-bot/.config/conf.yaml &
exec celery -A release_bot.celery_task worker -l info
exec celery -A release_bot.celery_task worker -l debug
20 changes: 14 additions & 6 deletions release_bot/celery_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from release_bot.releasebot import ReleaseBot

DEFAULT_CONF_FILE = "/home/release-bot/.config/conf.yaml"
logger = configuration.logger


@celery_app.task(name="task.celery_task.parse_web_hook_payload")
Expand All @@ -44,7 +45,6 @@ def parse_web_hook_payload(webhook_payload):
installation_id = webhook_payload['installation']['id']
repositories_added = webhook_payload['repositories_added']
save_new_installations(installation_id, repositories_added, db)

if webhook_payload['action'] == 'removed': # detect when repo uninstall app
repositories_removed = webhook_payload['repositories_removed']
delete_installations(repositories_removed, db)
Expand Down Expand Up @@ -88,7 +88,7 @@ def set_configuration(webhook_payload, db, issue=True):
f'{configuration.github_token}@github.com/' \
f'{configuration.repository_owner}/{configuration.repository_name}.git'

return ReleaseBot(configuration), configuration.logger
return ReleaseBot(configuration)


def handle_issue(webhook_payload, db):
Expand All @@ -98,7 +98,7 @@ def handle_issue(webhook_payload, db):
:param db: Redis instance
:return:
"""
release_bot, logger = set_configuration(webhook_payload, db=db, issue=True)
release_bot = set_configuration(webhook_payload, db=db, issue=True)

logger.info("Resolving opened issue")
release_bot.git.pull()
Expand All @@ -122,7 +122,7 @@ def handle_pr(webhook_payload, db):
:param db: Redis instance
:return:
"""
release_bot, logger = set_configuration(webhook_payload, db=db, issue=False)
release_bot = set_configuration(webhook_payload, db=db, issue=False)

logger.info("Resolving opened PR")
release_bot.git.pull()
Expand Down Expand Up @@ -150,11 +150,15 @@ def save_new_installations(installation_id, repositories_added, db):
:param db: Redis instance
:return: True if data was saved successfully into Redis
"""
logger.debug(f"Saving: {installation_id} added to {repositories_added}")
with db.pipeline() as pipe:
for repo in repositories_added:
pipe.set(repo["full_name"], installation_id)
pipe.execute()
return db.save()
success = db.save()
if not success:
logger.error(f"Failed to save {installation_id}:{repositories_added}")
return success


def delete_installations(repositories_removed, db):
Expand All @@ -164,8 +168,12 @@ def delete_installations(repositories_removed, db):
:param db: Redis instance
:return: True if data was deleted successfully into Redis
"""
logger.debug(f"Removing: {repositories_removed}")
with db.pipeline() as pipe:
for repo in repositories_removed:
pipe.delete(repo["full_name"])
pipe.execute()
return db.save()
success = db.save()
if not success:
logger.error(f"Failed to remove {repositories_removed}")
return success
1 change: 1 addition & 0 deletions release_bot/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def load_release_conf(self, conf):
self.logger.warning(msg)
parsed_conf['trigger_on_issue'] = False

self.logger.debug(f"Loaded release-conf.yaml: {parsed_conf}")
return parsed_conf

def set_pypi_project(self, parsed_conf, setup_cfg=None):
Expand Down
4 changes: 2 additions & 2 deletions release_bot/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def __init__(self, conf):
self.logger = conf.logger

def dispatch_request(self):
self.logger.info(f'New github webhook call from detected')
self.logger.info('New github webhook call detected')
if request.is_json:
celery_app.send_task(name="task.celery_task.parse_web_hook_payload",
kwargs={"webhook_payload": request.get_json()})
kwargs={"webhook_payload": request.json})
else:
self.logger.error("This webhook doesn't contain JSON")
return jsonify(result={"status": 200})

0 comments on commit 49d0d5d

Please sign in to comment.