gitlab api equivalent to git log (compare branch with tag) and list commit message json - rest

i need some equivalent command to the following command:
git log --pretty="* %s%n%b" BRANCH2...TAGNAME1
i tried already the compare feature of the api but sadly without the result i expected:
curl -s --header "PRIVATE-TOKEN: XXXXXXX" "https://gitlab.localhost/api/v4/projects/3/repository/compare?from=BRANCH2&to=TAGNAME1&straight=true" | jq .
what i get is:
"commit": null,
"commits": [],
but git log gives me the following:
* commit 1 description
* commit 2 description
so what i search and need is to compare BRANCH2 with TAGNAME1 and list the different commits.
this is later on needed in order to generate a changelog file which is pushed to a new tag.

This would be the compare API
Using the gitlab-org/gitlab repo as an example, to compare the tags/branches v14.8.0-ee to v14.9.0-ee:
curl https://gitlab.com/api/v4/projects/278964/repository/compare?from=v14.8.0-ee&to=v14.9.0-ee
If you're not getting the response you're expecting, try swapping the from and to arguments and/or omitting the straight argument.

thank you!
the solution in the end was change the from and to so in the end it works like that:
curl -s --header "PRIVATE-TOKEN: XXXXXXX" "https://gitlab.localhost/api/v4/projects/3/repository/compare?from=TAGNAME1&to=BRANCH2&straight=true" | jq .

Related

finding when a file was introduced on github

This curl command works as expected and shows when the repository was first created.
curl https://api.github.com/repos/RaRe-Technologies/gensim | grep created
"created_at": "2011-02-10T07:43:04Z",
But this does not show when a file in that repo was created.
curl
https://api.github.com/repos/RaRe-Technologies/gensim/blob/develop/gensim/scripts/make_wikicorpus.py
| grep created
Is there any way to find the date on which the file was introduced?
You can use https://api.github.com/repos/OWNER/REPO/commits?path=<path/to/file>, as described here.
The results of this request can then be parsed by jq, with the following options .[-1].commit.author.date.
This tells jq to get the last item of the array ([-1]), and then parse the value of commit, then author and then the date, which is the date of the commit.
So using the follwing command
curl "https://api.github.com/repos/RaRe-Technologies/gensim/commits?path=gensim/scripts/make_wikicor
pus.py" | jq -r ".[-1].commit.author.date"
will result in
2012-07-21T20:00:29Z
The alternative, using GitHub CLI gh, and its gh api command:
# Windows
gh api -X GET repos/RaRe-Technologies/gensim/commits \
-f path="gensim/scripts/make_wikicorpus.py" \
--jq ".[-1].commit.author.date"
# Linux
gh api -X GET repos/RaRe-Technologies/gensim/commits \
-f path='gensim/scripts/make_wikicorpus.py' \
--jq '.[-1].commit.author.date'
2012-07-21T20:00:29Z
You don't even need jq if you have gh installed.

Using github api command line gh command, how to find the branch then pull request for a commit sha?

For a repo https://github.com/org/project
My commit sha fa92b5dcd9748abceab00e290c2ba5c7713d854f
How to find the branch that this commit is in, by using the gh command line?
How to find the pull request that this commit is in, by using the gh command line?
You can use gh api -H "Accept: application/vnd.github.v3+json" /repos/OWNER/REPO/commits/COMMIT_SHA/pulls
It will give you list of PRs the commit was a part of. You will have to filter the json using tools like jq to get the suitable portion from the output.
I tried this command for my repo and it worked.
For more information, check this doc page. https://docs.github.com/en/rest/commits/commits#list-pull-requests-associated-with-a-commit

how to use filter parameters and Grep command together for listing all GITHUB pull request from a repository

I am looking for an option to get all pull requests with ID, Name, Date, and count of files committed from GitHub.
I managed to get all pull requests using API, but listing only the required parameter is not working. Can anyone help?
curl -X GET -u :https://api.github.com/repos/XXXX/XXXX/pulls?state=all&page=1&per_page=100| egrep -w 'title|number|state|created_at|updated_at'
Grep is working if the query is like below, without multiple filter parameters (state=all&page=1&per_page=100)
curl -X GET -u :https://api.github.com/repos/XXXX/XXXX/pulls?state=all| egrep -w 'title|number|state|created_at|updated_at'
Can anyone help how to list only the required data with multipe filter parameter.
try grep with something like this:
grep -Pa 'state'

how to download specific commit of the file on github

Given a short sha, I want to be able to download the file content of that commit using the github api.
currently i am using following to fetch the content, and it works fine, but how can i download the content for specific sha?
curl -u 'username:password' \
-H 'Accept: application/vnd.github.v3.raw' \
-o output.yaml
-L https://mycompany/api/v3/repos/myorg/myrepo/contents/services/serviceA/helm/manifest.yaml
apparently there is a query parameter ref can be passed to the url
The name of the commit/branch/tag. Default: the repository’s default branch (usually master)

How do you create a project in a specific group via GitLab API?

I tried to use GitLab API to create a new project. This worked, but this is in my user space:
curl \
--header "Authorization: Bearer ${GITLAB_API_TOKEN}" \
--request POST \
"https://gitlab.com/api/v4/projects/?name=$test-proj"
But I wanted to do it under a specific group with group_id <group_id> (I blanked it here). The most sensible approach that occured to me was:
curl \
--header "Authorization: Bearer ${GITLAB_API_TOKEN}" \
--request POST \
"https://gitlab.com/api/v4/groups/<group_id>/projects/?name=test-proj
But this did not work. Are there any suggestions on how I could achieve this?
I consulted the following references
https://forum.gitlab.com/t/create-a-new-project-in-a-group-using-api/1552
https://docs.gitlab.com/ee/api/projects.html#create-project
The GitLab documentation mentions the path or namespace_id attribute (although I would actually be in search of a group_id attribute). I am not sure about path and how to specify that. I tried - without success - to retrieve the namespace_id via
curl --header "PRIVATE-TOKEN: ${GITLAB_API_TOKEN}" "https://gitlab.example.com/api/v4/namespaces"
It is well possible that I might not have the rights to do the required operation. Therefore a reference to an official (GitLab) documentation together with a test curl command that works would be very helpful to me - thank you!
For anyone looking for the direct command:
STEP 1:
Navigate as below and identify the group ID from GUI:
Admin Area -> Groups -> GroupName
STEP 2:
Construct the command with two parameters viz., name, namespace_id
curl --header "PRIVATE-TOKEN: <myprivatetoken>" -X POST "https://gitlab.com/api/v4/projects?name=myexpectedrepo&namespace_id=38"
Both users and groups are considered "namespaces" as far as the Gitlab API is concerned, so you can use either a group ID or a username in the namespace_id field. You can see this in use by getting a single namespace with either a Group ID (that you can see in the /groups API call, or from a Group's "profile" page) or a username:
# this will show the namespace details of the Group with ID 54
curl --header "PRIVATE-TOKEN: ${TOKEN}" "https://gitlab.com/api/v4/namespaces/54
# this will show the namespace details of the User with username my-username
curl --header "PRIVATE-TOKEN: ${TOKEN}" "https://gitlab.com/api/v4/namespace/my-username
If you have the appropriate access level, you can assign a git remote to your local repository that includes your group name. Then, the push event will trigger GitLab to create the remote repository.
$ mkdir project_name && cd $_
$ echo "# project_name" > README.md
$ git init; git add .; git commit -m initial
$ GITLAB_HOST="gitlab.com"
$ git remote add origin git#${GITLAB_HOST}:group_name/project_name.git
$ git push origin HEAD
Then, point your browser to ${GITLAB_HOST}/group_name/project_name
https://docs.gitlab.com/ee/user/group/#specify-who-can-add-projects-to-a-group