From 19c574788bdf6d7b4e7c10f7f2e071f141abface Mon Sep 17 00:00:00 2001 From: farhaanbukhsh Date: Jul 26 2016 07:41:45 +0000 Subject: Fix extra POST request and code cleaning --- diff --git a/pagure/hooks/jenkins_hook.py b/pagure/hooks/jenkins_hook.py index 1d162ba..755f121 100644 --- a/pagure/hooks/jenkins_hook.py +++ b/pagure/hooks/jenkins_hook.py @@ -78,7 +78,7 @@ def get_configs(project_name, service): """ cfg = BASE.metadata.bind.query(PagureCI).filter( service == project_name).all() - if len(cfg) == 0: + if not cfg: raise ConfigNotFound(project_name) return cfg diff --git a/pagure/lib/pagure_ci.py b/pagure/lib/pagure_ci.py index ec5bad3..43d1992 100644 --- a/pagure/lib/pagure_ci.py +++ b/pagure/lib/pagure_ci.py @@ -2,10 +2,14 @@ import os import flask from sqlalchemy.orm import scoped_session, sessionmaker +from sqlalchemy.exc import SQLAlchemyError from sqlalchemy import create_engine + from pagure.hooks import jenkins_hook +import pagure.lib from pagure.lib import model from pagure import APP, SESSION +import pagure.exceptions import json import logging @@ -13,7 +17,6 @@ import logging import requests import jenkins -APP.config.from_envvar('INTEGRATOR_SETTINGS', silent=True) APP.logger.setLevel(logging.INFO) PAGURE_URL = '{base}api/0/{repo}/pull-request/{pr}/flag' @@ -64,14 +67,9 @@ def post_flag(logger, name, base, token, repo, pr, result, url): 'SUCCESS': ('Build successful', 100), 'FAILURE': ('Build failed', 0), }[result] - payload = { - 'username': name, - 'percent': percent, - 'comment': comment, - 'url': url, - } - post_data(logger, PAGURE_URL.format(base=base, repo=repo, pr=pr), payload, - headers={'Authorization': 'token ' + token}) + + pagure_ci_flag(logger, repo=repo, username=name, percent=percent, comment=comment, + url=url, requestid=pr) def post_data(logger, *args, **kwargs): @@ -80,3 +78,38 @@ def post_data(logger, *args, **kwargs): if resp.status_code < 200 or resp.status_code >= 300: logger.error('Network request failed: %d: %s', resp.status_code, resp.text) + + +def pagure_ci_flag(logger, repo, username, percent, comment, url, requestid): + + repo = pagure.lib.get_project(SESSION, repo, user=None) + output = {} + + if repo is None: + raise pagure.exceptions.FileNotFoundException('Repo not found') + + request = pagure.lib.search_pull_requests( + SESSION, project_id=repo.id, requestid=requestid) + + if not request: + raise pagure.exceptions.FileNotFoundException('Request not found') + + try: + message = pagure.lib.add_pull_request_flag( + SESSION, + request=request, + username=username, + percent=percent, + comment=comment, + url=url, + uid=None, + user=repo.user.username, + requestfolder=APP.config['REQUESTS_FOLDER'], + ) + SESSION.commit() + logger.debug('Received response status: %s', message) + output['message'] = message + + except SQLAlchemyError as err: # pragma: no cover + logger.exception(err) + SESSION.rollback() diff --git a/pagure/pagureCI/consumer.py b/pagure/pagureCI/consumer.py index 4ef85de..f45ad93 100644 --- a/pagure/pagureCI/consumer.py +++ b/pagure/pagureCI/consumer.py @@ -4,7 +4,7 @@ from pagure.hooks import jenkins_hook import pagure.lib from pagure.lib import pagure_ci from pagure.lib.model import BASE, Project, User -from pagure import APP +from pagure import APP, SESSION PAGURE_MAIN_REPO = '{base}{name}.git' PAGURE_FORK_REPO = '{base}forks/{user}/{name}.git' @@ -18,11 +18,9 @@ class Integrator(fedmsg.consumers.FedmsgConsumer): config_key = 'integrator.enabled' - SESSION = None def __init__(self, hub): super(Integrator, self).__init__(hub) - SESSION = pagure.lib.create_session(APP.config['DB_URL']) def consume(self, msg): topic, msg = msg['topic'], msg['body'] diff --git a/pagure/templates/plugin.html b/pagure/templates/plugin.html index 193fc63..3015f84 100644 --- a/pagure/templates/plugin.html +++ b/pagure/templates/plugin.html @@ -3,16 +3,16 @@ {% if full %} {% extends "repo_master.html" %} -{% block title %}{{ select.capitalize() }} {{ plugin.name }} - {{ repo.name }} -{% endblock %} {% set tag = "home" %} +{% block title %}{{ select.capitalize() }} {{ plugin.name }} - {{ repo.name }}{% endblock %} +{% set tag = "home" %} {% endif %} {% block repo %} {% if full %} -

{{ plugin.name }} settings

+

{{ plugin.name }} settings

{% endif %} -
{{ plugin.description | markdown | noJS | safe }} @@ -35,7 +35,6 @@ {{ form.csrf_token }}

-
- + {% endblock %} diff --git a/requirements.txt b/requirements.txt index 22c4034..de1cd79 100644 --- a/requirements.txt +++ b/requirements.txt @@ -37,5 +37,5 @@ cryptography py-bcrypt #Required for Pagure CI -moksha +fedmsg python-jenkins