How to pull custom branch to another branch via gitlab api - gitlab

I need to pull BRANCH-1 to BRANCH-2 using the GitLab API. I don't see any similar features in the API. Merge Request only allows you to merge MR with the ability to compare SHA with HEAD. Rebase has no parameters for selecting a branch. Is there any way to do this?

If you're trying to overwrite BRANCH-2 so it exactly matches BRANCH-1, then you can do that by deleting BRANCH-2 and then recreating BRANCH-2 from BRANCH-1.
This is obviously a destructive operation, so don't do it if BRANCH-2 has unique changes that you need to keep. With that warning in mind, here's how you do it.
Delete BRANCH-2 with this API request:
DELETE /projects/:id/repository/branches/:branch
where :id is the ID or URL-encoded path of the project and :branch is the name of BRANCH-2.
Then recreate BRANCH-2 with this API request:
POST /projects/:id/repository/branches?branch=:branch&ref=:ref
where :id is the ID or URL-encoded path of the project, :branch is the name of BRANCH-2, and :ref is the name of BRANCH-1.

Related

How to check if repository mirror is up to date in Gitlab?

I am trying to use repository mirroring to streamline my workflow.
I have a repository that uses pull mirroring from the upstream.
I am trying to trigger mirror update via api
My questions:
Can I find out that the mirror update I triggered if finished?
Alternatively, can I find out if the mirror (or a particular branch on a mirror) is up-to-date with the upstream?
To give you more context, I want to:
refresh the mirror
create a branch on the mirror
create a merge request to the upstream
I refresh the mirror to ensure that the merge request can be merged without conflicts. My pipeline is the only source of merge requests to the upstream. I am afraid of race condition between refresh mirror request and create branch request.
There are two fields ton the get single project endpoint GET /projects/:id
import_status (with "finished" as a marker of success)
import_error (with null as a marker of success)
I wasn't able to find out the timestamp of last mirror update though.

Azure REST API Get Branches returning 0 branches

I am trying to get all branches for a project with
https://dev.azure.com/$organization/$project/_apis/tfvc/branches?&api-
version=5.0
I use the credentials, organization, and project variables in other queries that are working. The request format is based off the example here
https://learn.microsoft.com/en-us/rest/api/azure/devops/tfvc/branches/get?view=azure-devops-rest-5.0
The response I am getting is
count value
----- -----
0 {}
when there are many branches in the repository.
Should I be specifying the repository somewhere? Or what else? Thanks!
First you need to confirm which type of repository you need to use rest API to get branch from, git or tfvc.
If you want to get branches from a tfvc repo, then use the rest api as you refer to in your case.
But, you need to be aware that if the count you want to return is not 0, you should ensure that there are branches in your tfvc repo, not just folders and files.
To convert the folder in tfvc repo to branch, you can convert it in vs source control explorer. For details, please refer to this docs.
If you want to get branches from a git repo, you need to use the rest API as follows.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=5.0

Construct GitLab URL without slug

Is there any way to build a GitLab URL for a milestone or project based on its id property instead of the slug?
Context:
I have an app that I use as a GitLab web hook, and from its front end would like to link back to GitLab. I'm keeping the project and milestone ids, as they are unique, but can't find a way to link back to them. Ideally something like: http://gitlab.example.com/project/83/milestone/113 or even http://gitlab.example.com/milestone/113 would work for me (even if they do a redirect).
Examining rake routes and config/routes.rb tells me that such routes do not exist.
The only options I can see are:
store just the slugs which are also unique. Your request and memory usage will be slightly larger, but it's worth it.
make an extra API request to get the slugs. This requires 2 requests, so it is worse than having a larger request.
For new routes of form /something to be created in gitlab, something needs to be blacklisted at https://github.com/gitlabhq/gitlabhq/blob/199029b842de7c9d52b5c95bdc1cc897da8e5560/lib/gitlab/blacklist.rb, and interestingly projects is already blacklisted, but it is currently only used for project creation.
milestones however is not blacklisted: so a user could be called milestiones and that would generate ambiguity.
I would also take a close look at how GitHub organizes its API and paths, as it is likely to be optimal: is ID web access possible in GitHub?

How to update a fork from it's original via the Github API

I've created a fork of a github repository via the github API. Now, later on, I want to pull any updates from the origin repository into the fork. This should always be a fast-forward in my use case. I have read access to the origin repository and read-write to the fork.
I thought of maybe creating a Pull Request then accepting (both of which you can do via the API) but this creates noise (Pull Requests being created and destroyed) and just doesn't seem right.
Is there any way to do this via the API?
I don't have the inside scoop on this, so this might be a miss-feature that will be removed at some point. Until then:
Github makes available all commits across (I assume) the entire fork network; So APIs that accept commit hashes will be happy to work on hashes from the upstream, or across other forks (This is explicitly documented for repos/commits/compare and creating a pull requst).
So there are a couple of ways to update via APIs only:
Using Git data api: This will usually be the best option, if you don't change your fork's master.
Get upstream ref /repos/upstream/repo/git/refs/heads/master, and get the hash from it
Update your fork PATCH /repos/my/repo/git/refs/heads/master with the same hash.
Using a higher-level merge api: This will create a merge commit, which some people like.
Get the upstream ref like before
Create a merge to branch master in your repo.
Pull-request to yourself and merge it via api: This will end up creating not only a merge commit, but a PR as well.
Create PR: POST to /repos/your/repo/pulls with head = "upstream:master"
Get the PR url from the response,
Merge it: PUT to /repos/your/repo/pulls/number/merge
It's possible that the "upstream:master" notation would also work for options 1 & 2, saving an API call.
Not possible currently, but I've gone ahead and added that to our API wishlist. :)
This that work for me, because I needed update from upstream but without a merge request commit. My ref is master.
Create a pull request POST /repos/:myUsername/:myRepo/pulls
INPUT: {title, head: 'ownerFromUpStream:master', base: 'master', ...}
Get sha from pull request (ex. response.data.head.sha)
PATCH /repos/:myUsername/:myRepo/git/refs/master
PARAMS: {sha: shaFromPullRequest}
DOC.
Update ref
Create pull request
This is now possible in the GitHub API; documentation here, and announcement here.
In summary, make a POST request to /repos/{owner}/{repo}/merge-upstream with the proper authentication and the payload of { "branch": "branch-name" }.

Using GitHub API to retrieve all versions of a specific file

I am currently trying to read through the (GitHub API)[http://developer.github.com/v3/repos/contents/] to figure out how I can programmatically retrieve all versions of a specific file in a single repository.
I see that one can get the list of commits, and the current version of a single file easily. But, is there a way to list all the commits relevant for a specific file and then iterate through all the versions of that file?
To get the list of commits relevant for a specific file, use this API endpoint and specify the path parameter:
GET https://api.github.com/repos/:owner/:repo/commits?path=FILE_PATH
You'll get back an array of commit objects, each of which has a sha attribute.
Now that you have all the commit SHAs, you can fetch all the different versions of the file using this API endpoint and by specifying the ref query parameter to set the SHA. So, for each commit SHA, make a request to:
GET https://api.github.com/repos/:owner/:repo/contents/:FILE_PATH?ref=SHA
and read the content attribute. Notice that the content is Base64 encoded, but you can also request a raw version by setting the relevant Accept HTTP header.

Resources