Skip to content

Commit

Permalink
feat(gitlab): accept search query to filter gitlab projects (#681)
Browse files Browse the repository at this point in the history
Closes #518
  • Loading branch information
mdonadoni committed Mar 19, 2024
1 parent b7cc00a commit c5f79ee
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
16 changes: 16 additions & 0 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@
"get": {
"description": "Retrieve projects from GitLab.",
"operationId": "gitlab_projects",
"parameters": [
{
"description": "The API access_token of the current user.",
"in": "query",
"name": "access_token",
"required": false,
"type": "string"
},
{
"description": "The search string to filter the project list.",
"in": "query",
"name": "search",
"required": false,
"type": "string"
}
],
"produces": [
"application/json"
],
Expand Down
41 changes: 32 additions & 9 deletions reana_server/rest/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import logging
import traceback
from typing import Optional
from urllib.parse import urljoin

import requests
from flask import (
Expand All @@ -25,6 +27,9 @@
from itsdangerous import BadData, TimedJSONWebSignatureSerializer
from reana_commons.k8s.secrets import REANAUserSecretsStore
from werkzeug.local import LocalProxy
from webargs import fields
from webargs.flaskparser import use_kwargs


from reana_server.config import (
REANA_GITLAB_OAUTH_APP_ID,
Expand Down Expand Up @@ -182,8 +187,9 @@ def gitlab_oauth(user): # noqa


@blueprint.route("/gitlab/projects", methods=["GET"])
@use_kwargs({"search": fields.Str(location="query")})
@signin_required()
def gitlab_projects(user): # noqa
def gitlab_projects(user, search: Optional[str] = None): # noqa
r"""Endpoint to retrieve GitLab projects.
---
get:
Expand All @@ -193,6 +199,17 @@ def gitlab_projects(user): # noqa
Retrieve projects from GitLab.
produces:
- application/json
parameters:
- name: access_token
in: query
description: The API access_token of the current user.
required: false
type: string
- name: search
in: query
description: The search string to filter the project list.
required: false
type: string
responses:
200:
description: >-
Expand Down Expand Up @@ -228,16 +245,22 @@ def gitlab_projects(user): # noqa
try:
secrets_store = REANAUserSecretsStore(str(user.id_))
gitlab_token = secrets_store.get_secret_value("gitlab_access_token")
gitlab_url = (
f"{REANA_GITLAB_URL}/api/v4/projects/"

gitlab_url = urljoin(REANA_GITLAB_URL, "/api/v4/projects")
params = {

Check warning on line 250 in reana_server/rest/gitlab.py

View check run for this annotation

Codecov / codecov/patch

reana_server/rest/gitlab.py#L249-L250

Added lines #L249 - L250 were not covered by tests
"access_token": gitlab_token,
# show projects in which user is at least a `Maintainer`
"?min_access_level=40"
# as that's the minimum access level needed to create webhooks
"min_access_level": 40,
"per_page": 100,
"search": search,
# include ancestor namespaces when matching search criteria
"search_namespaces": "true",
# return only basic information about the projects
"&simple=true"
"&per_page=100"
f"&access_token={gitlab_token}"
)
response = requests.get(gitlab_url)
"simple": "true",
}

response = requests.get(gitlab_url, params=params)

Check warning on line 263 in reana_server/rest/gitlab.py

View check run for this annotation

Codecov / codecov/patch

reana_server/rest/gitlab.py#L263

Added line #L263 was not covered by tests
projects = dict()
if response.status_code == 200:
for gitlab_project in response.json():
Expand Down

0 comments on commit c5f79ee

Please sign in to comment.