6

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?

royki
  • 1,593
  • 3
  • 25
  • 45

4 Answers4

6

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
David Planella
  • 2,313
  • 2
  • 25
  • 30
  • But to create `issue` or `branch`, I need `id`. No!!! Or can I use the `my_namespace%2Fmy_app` to create project `issue` or `branch` – royki Feb 23 '19 at 21:44
  • What is the api to create project using `namespace` – royki Feb 25 '19 at 10:48
  • You can use the URL-encoded path of the project to create a new issue: https://docs.gitlab.com/ee/api/issues.html#new-issue The same applies for creating a new branch: https://docs.gitlab.com/ee/api/branches.html#create-repository-branch The API to create a new project is https://docs.gitlab.com/ee/api/projects.html#create-project – David Planella Feb 25 '19 at 19:40
  • 1
    You should try this cli tool: https://github.com/profclems/glab . You can get project issues with `glab issue list` – Clement Sam Aug 06 '20 at 04:58
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/

Izydorr
  • 1,926
  • 3
  • 23
  • 40
0

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

Louis Caron
  • 1,043
  • 1
  • 11
  • 17
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)
mohit sehrawat
  • 171
  • 1
  • 12