Reputation: 1693
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
.
name
and id
in yaml
format)
gitlab project list
gitlab -o yaml project create --name "my_app_cli" --visibility "internal"
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
?
Upvotes: 8
Views: 12409
Reputation: 2353
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
Upvotes: 9
Reputation: 211
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)
Upvotes: 0
Reputation: 1451
When using the official glab
cli, you can get the project id of the current project using:
glab repo view -F json | jq '.id'
Upvotes: -1
Reputation: 1350
Use the search API with the projects scope, it is described here: https://docs.gitlab.com/ee/api/search.html#scope-projects
Upvotes: 0
Reputation: 2001
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/
Upvotes: 0