From 78837b8fd2030b19ad5649e4710cf8bc823a5c40 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 07 2016 10:08:22 +0000 Subject: [PATCH 1/3] Make all the hooks use uselist=False This means that when doing project.ci_hook for example, only 1 item will be returned instead of a list (of 1 item) --- diff --git a/pagure/hooks/fedmsg.py b/pagure/hooks/fedmsg.py index e7ed0bb..d671bcf 100644 --- a/pagure/hooks/fedmsg.py +++ b/pagure/hooks/fedmsg.py @@ -42,7 +42,7 @@ class FedmsgTable(BASE): 'Project', remote_side=[Project.id], backref=backref( 'fedmsg_hook', cascade="delete, delete-orphan", - single_parent=True) + single_parent=True, uselist=False) ) diff --git a/pagure/hooks/irc.py b/pagure/hooks/irc.py index f1d26a0..28ca148 100644 --- a/pagure/hooks/irc.py +++ b/pagure/hooks/irc.py @@ -52,7 +52,7 @@ class IrcTable(BASE): 'Project', remote_side=[Project.id], backref=backref( 'irc_hook', cascade="delete, delete-orphan", - single_parent=True) + single_parent=True, uselist=False) ) diff --git a/pagure/hooks/mail.py b/pagure/hooks/mail.py index f6e70c4..a1c3792 100644 --- a/pagure/hooks/mail.py +++ b/pagure/hooks/mail.py @@ -44,7 +44,7 @@ class MailTable(BASE): 'Project', remote_side=[Project.id], backref=backref( 'mail_hook', cascade="delete, delete-orphan", - single_parent=True) + single_parent=True, uselist=False) ) diff --git a/pagure/hooks/pagure_ci.py b/pagure/hooks/pagure_ci.py index fa33272..7a671a3 100644 --- a/pagure/hooks/pagure_ci.py +++ b/pagure/hooks/pagure_ci.py @@ -53,26 +53,26 @@ class PagureCITable(BASE): 'Project', remote_side=[Project.id], backref=backref( 'ci_hook', cascade="delete, delete-orphan", - single_parent=True) + single_parent=True, uselist=False) ) tmpl = """ {% if repo | hasattr('ci_hook') and repo.ci_hook and - repo.ci_hook[0].pagure_ci_token %} + repo.ci_hook.pagure_ci_token %} The token to be used by jenkins to trigger the build is:
-{{ repo.ci_hook[0].pagure_ci_token}}
+{{ repo.ci_hook.pagure_ci_token}}
 
The URL to be used to POST the results of your build is:
 {{ (config['APP_URL'][:-1] if config['APP_URL'].endswith('/')
   else config['APP_URL'])
-  + url_for('api_ns.%s_ci_notification' % repo.ci_hook[0].ci_type,
+  + url_for('api_ns.%s_ci_notification' % repo.ci_hook.ci_type,
     repo=repo.name, username=username,
-    pagure_ci_token=repo.ci_hook[0].pagure_ci_token) }}
+    pagure_ci_token=repo.ci_hook.pagure_ci_token) }}
 
{% else %} diff --git a/pagure/hooks/pagure_force_commit.py b/pagure/hooks/pagure_force_commit.py index 2ef2d3b..e0ab0d0 100644 --- a/pagure/hooks/pagure_force_commit.py +++ b/pagure/hooks/pagure_force_commit.py @@ -45,7 +45,7 @@ class PagureForceCommitTable(BASE): 'Project', foreign_keys=[project_id], remote_side=[Project.id], backref=backref( 'pagure_force_commit_hook', cascade="delete, delete-orphan", - single_parent=True) + single_parent=True, uselist=False) ) diff --git a/pagure/hooks/pagure_hook.py b/pagure/hooks/pagure_hook.py index 541cae5..a207f66 100644 --- a/pagure/hooks/pagure_hook.py +++ b/pagure/hooks/pagure_hook.py @@ -44,7 +44,7 @@ class PagureTable(BASE): 'Project', remote_side=[Project.id], backref=backref( 'pagure_hook', cascade="delete, delete-orphan", - single_parent=True) + single_parent=True, uselist=False) ) diff --git a/pagure/hooks/pagure_request_hook.py b/pagure/hooks/pagure_request_hook.py index ee0529c..32f6123 100644 --- a/pagure/hooks/pagure_request_hook.py +++ b/pagure/hooks/pagure_request_hook.py @@ -46,7 +46,7 @@ class PagureRequestsTable(BASE): 'Project', remote_side=[Project.id], backref=backref( 'pagure_hook_requests', cascade="delete, delete-orphan", - single_parent=True) + single_parent=True, uselist=False) ) diff --git a/pagure/hooks/pagure_ticket_hook.py b/pagure/hooks/pagure_ticket_hook.py index d2379c1..06e6c33 100644 --- a/pagure/hooks/pagure_ticket_hook.py +++ b/pagure/hooks/pagure_ticket_hook.py @@ -45,7 +45,7 @@ class PagureTicketsTable(BASE): 'Project', remote_side=[Project.id], backref=backref( 'pagure_hook_tickets', cascade="delete, delete-orphan", - single_parent=True) + single_parent=True, uselist=False) ) diff --git a/pagure/hooks/pagure_unsigned_commits.py b/pagure/hooks/pagure_unsigned_commits.py index 117139f..39117bd 100644 --- a/pagure/hooks/pagure_unsigned_commits.py +++ b/pagure/hooks/pagure_unsigned_commits.py @@ -42,7 +42,7 @@ class PagureUnsignedCommitTable(BASE): 'Project', foreign_keys=[project_id], remote_side=[Project.id], backref=backref( 'pagure_unsigned_commit_hook', cascade="delete, delete-orphan", - single_parent=True) + single_parent=True, uselist=False) ) diff --git a/pagure/hooks/rtd.py b/pagure/hooks/rtd.py index 5f072b2..66ee001 100644 --- a/pagure/hooks/rtd.py +++ b/pagure/hooks/rtd.py @@ -45,7 +45,7 @@ class RtdTable(BASE): 'Project', remote_side=[Project.id], backref=backref( 'rtd_hook', cascade="delete, delete-orphan", - single_parent=True) + single_parent=True, uselist=False) ) From dcbd998670700c54362737ed8e9955501d4da8fd Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 07 2016 10:08:22 +0000 Subject: [PATCH 2/3] Adjust all the place that were dong *_hook[0] to do *_hook directly --- diff --git a/pagure-ci/pagure_ci_server.py b/pagure-ci/pagure_ci_server.py index fb43952..a3470d9 100644 --- a/pagure-ci/pagure_ci_server.py +++ b/pagure-ci/pagure_ci_server.py @@ -96,7 +96,7 @@ def handle_messages(): log.info("Trigger on %s PR #%s from %s: %s", project.fullname, pr_id, repo, branch) - url = project.ci_hook[0].ci_url + url = project.ci_hook.ci_url if data['ci_type'] == 'jenkins': url = urlparse.urljoin(url, '/buildWithParameters') @@ -104,7 +104,7 @@ def handle_messages(): requests.post( url, data={ - 'token': project.ci_hook[0].pagure_ci_token, + 'token': project.ci_hook.pagure_ci_token, 'cause': pr_id, 'REPO': project.fullname, 'BRANCH': branch diff --git a/pagure/api/ci/jenkins.py b/pagure/api/ci/jenkins.py index 981055e..e0d45ac 100644 --- a/pagure/api/ci/jenkins.py +++ b/pagure/api/ci/jenkins.py @@ -44,7 +44,7 @@ def jenkins_ci_notification(repo, pagure_ci_token, username=None): if not constant_time.bytes_eq( to_bytes(pagure_ci_token), - to_bytes(project.ci_hook[0].pagure_ci_token)): + to_bytes(project.ci_hook.pagure_ci_token)): raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK) data = flask.request.get_json() diff --git a/pagure/hooks/files/pagure_force_commit_hook.py b/pagure/hooks/files/pagure_force_commit_hook.py index a1bacd4..3b07189 100755 --- a/pagure/hooks/files/pagure_force_commit_hook.py +++ b/pagure/hooks/files/pagure_force_commit_hook.py @@ -42,7 +42,7 @@ def run_as_pre_receive_hook(): # Get the list of branches branches = [ branch.strip() - for branch in repo.pagure_force_commit_hook[0].branches.split(',') + for branch in repo.pagure_force_commit_hook.branches.split(',') if repo.pagure_force_commit_hook] # Remove empty branches diff --git a/pagure/hooks/files/rtd_hook.py b/pagure/hooks/files/rtd_hook.py index 98525c6..3803fb1 100755 --- a/pagure/hooks/files/rtd_hook.py +++ b/pagure/hooks/files/rtd_hook.py @@ -43,7 +43,7 @@ def run_as_post_receive_hook(): # Get the list of branches branches = [ branch.strip() - for branch in repo.rtd_hook[0].branches.split(',') + for branch in repo.rtd_hook.branches.split(',') if repo.rtd_hook] # Remove empty branches @@ -53,7 +53,7 @@ def run_as_post_receive_hook(): if branch] url = 'http://readthedocs.org/build/%s' % ( - repo.rtd_hook[0].project_name.strip() + repo.rtd_hook.project_name.strip() ) for line in sys.stdin: @@ -65,11 +65,11 @@ def run_as_post_receive_hook(): if branches: if refname in branches: print 'Starting RTD build for %s' % ( - repo.rtd_hook[0].project_name.strip()) + repo.rtd_hook.project_name.strip()) requests.post(url) else: print 'Starting RTD build for %s' % ( - repo.rtd_hook[0].project_name.strip()) + repo.rtd_hook.project_name.strip()) requests.post(url) diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 0822242..0781098 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -882,7 +882,7 @@ def add_pull_request_comment(session, request, commit, tree_id, filename, if notification and request.status == 'Open' \ and request.project.ci_hook and PAGURE_CI: REDIS.publish('pagure.ci', json.dumps({ - 'ci_type': request.project.ci_hook[0].ci_type, + 'ci_type': request.project.ci_hook.ci_type, 'pr': request.to_json(public=True, with_comments=False) })) @@ -1255,7 +1255,7 @@ def new_pull_request(session, branch_from, # Send notification to the CI server if REDIS and request.project.ci_hook and PAGURE_CI: REDIS.publish('pagure.ci', json.dumps({ - 'ci_type': request.project.ci_hook[0].ci_type, + 'ci_type': request.project.ci_hook.ci_type, 'pr': request.to_json(public=True, with_comments=False) })) diff --git a/pagure/lib/lib_ci.py b/pagure/lib/lib_ci.py index 0e390e6..ecb3913 100644 --- a/pagure/lib/lib_ci.py +++ b/pagure/lib/lib_ci.py @@ -43,8 +43,8 @@ def process_jenkins_build(session, project, build_id, requestfolder): """ import jenkins # Jenkins Base URL - jenk = jenkins.Jenkins(project.ci_hook[0].ci_url.split('/job/')[0]) - jenkins_name = project.ci_hook[0].ci_url.split( + jenk = jenkins.Jenkins(project.ci_hook.ci_url.split('/job/')[0]) + jenkins_name = project.ci_hook.ci_url.split( '/job/', 1)[1].split('/', 1)[0] build_info = jenk.get_build_info(jenkins_name, build_id) result = build_info['result'] @@ -77,7 +77,7 @@ def process_jenkins_build(session, project, build_id, requestfolder): pagure.lib.add_pull_request_flag( session, request=request, - username=project.ci_hook[0].ci_type, + username=project.ci_hook.ci_type, percent=percent, comment=comment, url=url, From 66dd261fa217784fe263aaa898ba6a3ce9aecae3 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 07 2016 10:08:22 +0000 Subject: [PATCH 3/3] Drop the check if the dbobj retrieved is of some length or not --- diff --git a/pagure/ui/plugins.py b/pagure/ui/plugins.py index 2c41394..a8997ba 100644 --- a/pagure/ui/plugins.py +++ b/pagure/ui/plugins.py @@ -101,8 +101,7 @@ def view_plugin(repo, plugin, username=None, full=True): dbobj = getattr(repo, plugin.backref) # There should always be only one, but let's double check - if dbobj and len(dbobj) > 0: - dbobj = dbobj[0] + if dbobj: new = False else: dbobj = plugin.db_object()