Skip to content

Commit

Permalink
feat(gitlab): accept search query to filter gitlab projects (reanahub…
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonadoni committed Mar 19, 2024
1 parent b7cc00a commit 14bc890
Showing 1 changed file with 32 additions and 9 deletions.
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 = {
"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)
projects = dict()
if response.status_code == 200:
for gitlab_project in response.json():
Expand Down

0 comments on commit 14bc890

Please sign in to comment.