Skip to content

Commit

Permalink
gitlab: fix fetching projects with other webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidalgarcia committed Nov 10, 2020
1 parent fc393cd commit fa20996
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changes
=======

Version 0.7.1 (UNRELEASED)
--------------------------

- Fixes problem on GitLab integration preventing the synchronisation of projects with existing webhooks.

Version 0.7.0 (2020-10-20)
--------------------------

Expand Down
15 changes: 5 additions & 10 deletions reana_server/rest/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
Blueprint,
current_app,
jsonify,
make_response,
redirect,
request,
url_for,
Expand All @@ -28,7 +27,6 @@
from reana_commons.k8s.secrets import REANAUserSecretsStore
from werkzeug.local import LocalProxy

from reana_server.api_client import current_rwc_api_client
from reana_server.config import (
REANA_GITLAB_OAUTH_APP_ID,
REANA_GITLAB_OAUTH_APP_SECRET,
Expand Down Expand Up @@ -219,21 +217,18 @@ def gitlab_projects(): # noqa
projects = dict()
if response.status_code == 200:
for gitlab_project in response.json():
hook_id = _get_gitlab_hook_id(
response, gitlab_project["id"], gitlab_token
)
hook_id = _get_gitlab_hook_id(gitlab_project["id"], gitlab_token)
projects[gitlab_project["id"]] = {
"name": gitlab_project["name"],
"path": gitlab_project["path_with_namespace"],
"url": gitlab_project["web_url"],
"hook_id": hook_id,
}
return jsonify(projects), 200
else:
return (
jsonify({"message": "Project list could not be retrieved"}),
response.status_code,
)
return (
jsonify({"message": "Project list could not be retrieved"}),
response.status_code,
)
except ValueError:
return jsonify({"message": "Token is not valid."}), 403
except Exception as e:
Expand Down
13 changes: 8 additions & 5 deletions reana_server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _format_gitlab_secrets(gitlab_response):
}


def _get_gitlab_hook_id(response, project_id, gitlab_token):
def _get_gitlab_hook_id(project_id, gitlab_token):
"""Return REANA hook id from a GitLab project if it is connected.
By checking its webhooks and comparing them to REANA ones.
Expand All @@ -256,11 +256,14 @@ def _get_gitlab_hook_id(response, project_id, gitlab_token):
)
response_json = requests.get(gitlab_hooks_url).json()
create_workflow_url = url_for("workflows.create_workflow", _external=True)
if response.status_code == 200 and response_json:
if response_json:
reana_hook_id = next(
hook["id"]
for hook in response_json
if hook["url"] and hook["url"] == create_workflow_url
(
hook["id"]
for hook in response_json
if hook["url"] and hook["url"] == create_workflow_url
),
None,
)
return reana_hook_id

Expand Down

0 comments on commit fa20996

Please sign in to comment.