Get the `id` of gitlab project via gitlab api or gitlab-cli
Asked Answered
D

5

7

Is there any way to get an id of the project via GitLab API .

I created a project using GitLab API providing the project name -

curl -kX POST --header "PRIVATE-TOKEN: <access_token>" https://gitlab.example.com/api/v4/projects/my_app&visibility=internal

It returns expected result with project information like links, id, user, namespace etc. Now I want to create issue or create a branch. I need the project id now. So how can I retrieve the id of my project my_app. (Not from UI).

I didn't find any API query that I can create issue or branch without using project id.

I always need id Issues API

POST /projects/:id/issues

As well I didn't find any API query that I can retrieve the id using the project name

Only thing I can use by Search API and use project name curl -kX GET --header "PRIVATE-TOKEN: <access_token>" https://gitlab.example.com/api/v4/search?scope=projects&search=my_app

But it provides whole lot of information. I only need to retrieve project id. No idea how to get the project id?

There is python-gitlab lib by which I can do, similar stuff like using curl.

  • Get projects ( gives most recent 20 projects' name and id in yaml format)
    • gitlab project list
  • Create project
    • gitlab -o yaml project create --name "my_app_cli" --visibility "internal"
    • Create Issue of a project
    • gitlab -o yaml project create-issue --id ID Here also I need to provide id.

But in this library also, there is no option to retrieve project id. Same as the API, to create any branch, issue etc, I need to use id of the created project. I don't want to put manually the id in the api or while using cli

Is there any workaround to get the id via api or cli using project name?

Or Is there any workaround to create issue, branch etc. without using the id via api or cli?

Daedalus answered 15/2, 2019 at 21:6 Comment(3)
Would https://mcmap.net/q/153469/-where-do-i-find-the-project-id-for-the-gitlab-api help?Selfpropelled
I think it only works with api v3. I tried but not get the desired result.Daedalus
You should try this cli tool: github.com/profclems/glabSuck
G
9

You can use the Get single project endpoint. But more generally, you are not limited to using the numerical ID, you can actually use the project name in API calls:

id: The ID or URL-encoded path of the project

In your particular case, this should work as both an example of getting the ID and to demonstrate that you don't necessarily need the ID :)

GET /api/v4/projects/my_namespace%2Fmy_app
Glabella answered 22/2, 2019 at 9:56 Comment(4)
But to create issue or branch, I need id. No!!! Or can I use the my_namespace%2Fmy_app to create project issue or branchDaedalus
What is the api to create project using namespaceDaedalus
You can use the URL-encoded path of the project to create a new issue: docs.gitlab.com/ee/api/issues.html#new-issue The same applies for creating a new branch: docs.gitlab.com/ee/api/branches.html#create-repository-branch The API to create a new project is docs.gitlab.com/ee/api/projects.html#create-projectGlabella
You should try this cli tool: github.com/profclems/glab . You can get project issues with glab issue listSuck
J
0

Why don't you read the ID from results of project create call? You can do it easly for example with https://stedolan.github.io/jq/

Jahnke answered 22/2, 2019 at 8:30 Comment(0)
M
0

Use the search API with the projects scope, it is described here: https://docs.gitlab.com/ee/api/search.html#scope-projects

Menken answered 22/2, 2019 at 16:47 Comment(0)
M
0

In Python we have modules which uses the Gitlab APIs to get the details. You can get the id of the projects using the below python code.Here we are using the gitlab module for making the call.

import os
import gitlab
    
gl = gitlab.Gitlab('http://gitlab_hostname.com', 'your_private_token')
all_projects = gl.projects.list(all=True)
print("All projects id are:",all_projects)
Madriene answered 16/7, 2019 at 6:15 Comment(0)
S
-1

When using the official glab cli, you can get the project id of the current project using:

glab repo view -F json | jq '.id'
Season answered 30/8, 2024 at 9:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.