PyGithub - How to get a list of all commits for a particular branch? - github-api

I need to check all commits made to a particular branch on github.
I am aware of
repo.get_commits()
but this returns commits for all branches of that repository I guess. I haven't found any branch attribute neither in Commit class nor in Gitcommit class.
Also there is not sth like .get_commits() in Branch class
What is the proper way to solve this problem?

As discussed here, this should utimately use the GitHub API List Commits
get /repos/{owner}/{repo}/commits
With as parameter the sha or branch name from which you want to list commits.
Ib PyGitHub, that is github.Repository.Repository.get_commits(), with sha – string being the name of the branch.
That will return a github.PaginatedList.PaginatedList of github.Commit.Commit

Related

Gitlab - Merged branch not marked as merged

Sorry for this "how to" question, however I really couldn't find anything in the doc, nor on the Web.
Let's take the following scenario:
I have a branch called "main"
From the "main" branch, I create a new one called "f1" (using the GUI)
I do some changes on the "f1" branch, and push them.
From the GUI, I create a new merge request to mnerges changes done on "f1" into "main".
From the GUI, I merge the merge request.
then, when I list branches from the GUI, I would have expected to see my "f1" branch marked as "merged", but that's not the case ...
Am I missing anthing obvious?

Gitlab Artifcacts Store Previous Artifacts

Using Gitlab Job Artifacts: https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html
How do I store the previous run's artifacts along with the current run?
I know I can use the name keyword in order to give my artifacts a unique name. This will allow me to store the unique artifacts of every single pipeline I ever ran.
But I'd like to only store say the last 3 builds. How can I ensure that the 3rd build replaces the current build?
First, use special folder for your artifact and give them a unique name (for example you can add a commit or build number to the name)

How do I add a group reviewer in Gitlab?

I periodically need to add more than 10 people to MR and I do it manually. I add only one person at a time, through the search. Can I somehow add the whole team to the reviewers?
This does not seem to be supported, and could be added as a feature request, to be referenced in Epic: Merge request reviewers.
The alternative is to use quick actions, with GitLab 13.7+ (issue 241244)
But that presupposes you know the users' names beforehand.

What are the Gitlab references listed under /refs?

I am working on a script which collects information about commits via the Gitlab API. Among others, I need to know which tag(s) are pointing to a certain commit. I was using
GET /repository/commits/<sha>/refs until I noticed that there are multiple tags listed in the response and different commits can have the same tags, according to this endpoint, which is impossible. So my questions are:
What information is shown by this endpoint as type "branch" and "tag"?
Which endpoint would show me the correct tag for a given commit?
Thank you!
1. From GitLab Docs, REST API, Commits:
Get references a commit is pushed to
Get all references (from branches or tags) a commit is pushed to. ...
GET /projects/:id/repository/commits/:sha/refs
So it shows all refs where given commit is included, and it works perfectly as documented, several tags could have particular commit in their history tree. By default it shows both branches and tags, but you can filter to include either branches or tags.
2. To find a tag with the tip at specific commit, you may enumerate all tags first, as (reference to tags endpoint doc):
Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
GET /projects/:id/repository/tags
Then manually find the tags at the target commit by commit id (SHA). It's perfectly correct that several tags could have the same commit at their tip. The tags endpoint by default enumerates them from latest to oldest, so if there are many tags at the same commit, you may pick for example latest.

How to get branch name when running pipeline on tag?

I run pipeline from a tag (let's say v1.0.0) on a branch (let's say staging).
My output file is created with ${CI_PROJECT_NAME}-${CI_COMMIT_REF_NAME}.apk.
The result is a file named MyProject-v1.0.0.apk.
I wish add branch name in the output filename to get MyProject-staging-v1.0.0.apk.
From the gitlab documentation, i could use CI_COMMIT_TAG and CI_COMMIT_BRANCH like this ${CI_PROJECT_NAME}-${CI_COMMIT_BRANCH}-${CI_COMMIT_TAG}.apk.
But the documentation says:
CI_COMMIT_BRANCH : The commit branch name. Present only when building
branches.
CI_COMMIT_TAG : The commit tag name. Present only when building tags.
So how to get the branch name?
You can find which branch a tag is part of.
The issue is: a tag can be referenced (part of the history of) multiple branch.
So, as in here, your gitlab.yml could call a script setting that branch (settings an environment variable), provided you have a convention in place to select the branch you want out of the (possibly) more than one branch which could refer said tag.
There is an env var called which references either the branch name or tag name called CI_COMMIT_REF_NAME

Resources