How to get GitHub REST API to restore a deleted branch - github-api

I've been looking for a way to automate a task, one manual part that needs automation is this restore branch button, is there a way to do this using Github API? I've read these two documents and couldn't find anything, Thank you.
https://docs.github.com/en/rest/reference/pulls
https://docs.github.com/en/rest/reference/branches

Following off the advice from here: git can I view the reflog of a remote?
I was able to restore a PR branch via the GitHub CLI/API by...
Getting the SHA of the HEAD of the branch to restore with:
gh pr view {pr_number} --json commits
where the last commit in the "commits" array will have a "oid" value (which I believe stands for orphan id), and that can be used to create the restored branch with
gh api repos/{owner}/{repo}/git/refs -f ref=refs/heads/{new_branch_name} -f sha={oid}
# set new_branch_name to the name of the PR branch to keep same behavior as button
and now the branch can be fetch'd when within a cloned instance of the repository:
git fetch
but is also available via API as there is now a new remote branch
Sorry if you can't use gh. This worked for me and I don't mind the dependency. If someone likes they can figure out a full REST API only way.

Related

git pull in Azure Data Factory

When working with the regular source code, (Java, C++, etc..) there are things like
git pull ..
git fetch ..
git push ..
to synch your remote git repo branch with your local branch.
What is the equivalent of such in the Azure Data Factory world ?
So, I am using azure data factory with the Azure git repo.
I am working in the particular feature branch - "fefature branch"
And my pipeline has a copy activity that hits a data set in its "Sink" stage.
Here is a screen shot but .. it's pretty simple and seems right
I see that my code for Data set definition (Json) in the remote Git repository is different from what I see in the Azure portal gui (being pointed to that same remote branch). ADF Gui in the Azure Portal is correct, the one in the git repo contains some stuff that I already deleted, but it does not gets deleted there (Why??)
So, when I 'Debug' pipeline I get errors which indicate this discrepancy as a problem. I want ty sync the environments and .. given that I do not understand how the discrepancies came about, I don't know how to fix an issue?. Any help is appreciated.
In the ADF world, we use publish and create a new pull request to merge the new changes from a feature branch to the main branch.
it seems like your git repository version is not up to date with the live ADF.
If there are any pending changes in your main branch, then you can click on Publish button to merge the changes
And if you are working on the feature branches, you can merge the changes using the new pull request.
If you have multiple feature branches, then you will need to manually compare the different versions to resolve these conflicts.

Gitlab API branch listing is caching results / does not display new branches instantly after creation

This problem occurs on a self-hosted instance of GitLab Enterprise Edition (v14.7.2ee). All projects are set to "Internal".
Steps to reproduce:
List all branches using the API
Create a new branch using the API
List all branches using the API
The new branch created in step 2 is missing unless we put a delay of about 30 seconds between step 2 and step 3.
However, if we do this instead:
Create a new branch using the API
List all branches using the API
we instantly see the new branch there.
The command to get all branches is: curl -s -k --header 'PRIVATE-TOKEN:xxx' 'https://gitlab.xxx.com/api/v4/projects/xx/repository/branches'
Does this mean GitLab is somehow caching branch list results or there is some configuration we need to apply in order to get the new branch instantly after creation?
The problem doesn't seem to be happening on another self-hosted instance of GitLab Community Edition v13.6.4 where all projects are set to "Public".
I am not sure if there is a fault in my setup or GitLab itself. Any help is greatly appreciated.
I ran into the same issue. There was a requirement to avoid cloning the repo because it's huge. The only workaround I could come up with was to use git ls-remote command instead of the API:
git ls-remote <git_project_url> refs/heads/*

ADF source integration issues with multiple developers

We have two developers using the same ADF. Each developer creates a git branch and starts working on it. Each developer can save the changes to their own git branch but there can only be one collaboration branch and this branch decides the publishing branch. This is causing a blockade (for one of the developer. How can we solve this ?
ADF publish branch can be set using a publish_config.json but now there is an option to set this in the adf itself. which one takes precedence? What is the best practice here?
You need to manage the work of each developer with standard git branch/merge processes. When one dev is done with work in their feature branch, then they will create a pull request to merge changes into your collaboration branch.
If the second dev has not created a feature branch yet, they can just do so after the pull request from the first dev is complete and then continue work from there. If the second dev has already created a feature branch, then they will need to merge the new changes from the collaboration branch into their feature branch to continue work before later committing to git and creating a pull request to merge changes from their feature branch back into the collaboration branch. From there, you can publish as needed.
This git work can be done through the ADF editor as well as through any other git interface you have. It's up to you.
This article discusses the process in specific detail using the ADF editor.
EDIT:
I believe you now have answers for this from 3 of the other 5 questions you posted about this same topic in the past day.
ADF publish confusion in git mode
Azure data factory working-branch confusion
When ADF publish branch is git protected how to publish?
Here is another article which describes the fundamental git process for ADF to help bring you up to speed with the fundamentals of how the different branches work, and how you can switch publish branches on the fly if needed.

How to use git namespace to hide branches

Background
I'm working with a large team using git for version control. The normal flow is:
People selecting a ticket from the "backlog queue".
Working on the issue via a local branch (i.e. git checkout -b my_feature_branch).
Making several commits as they go (i.e. git commit).
Pushing local changes to a remote branch in order to "backup" their work so it lives on more than one machine, in case the laptop is damaged or stolen (i.e. git push -u origin my_feature_branch).
Eventually creating a code review on our private github page, and doing a squashed merge from the feature branch to master.
In addition to the remote feature branches created by employees on an as-needed basis, we have several dozen release branches that are used to create the "gold builds" we ship to customers, i.e. 1.00, 1.01, 2.00, 2.01, 2.02, etc.
Problem
Some developers have begun to complain that there are too many branches, and I tend to agree. Some developers haven't been diligent about cleaning up old branches when they are no longer needed (even though github provides a one-button delete feature for this once the code review is complete).
Question
Is there a way to configure our company github deployment so that, when people use git branch via the CLI:
Only our "important/release/gold" branches appear.
The one-off developer (temporary) branches only appear via git branch -a?
The main goal of this is to reduce clutter.
Edit: I found a similar question, but the only answer is not at all applicable (don't use remote branches), which violates my key constraint of allowing people to push to remote branches as a form of data backup. The concept of private namespaces, as hinted by #Mort, seems to be exactly what I'm looking for. Now, how do I accomplish that?
Long story short: you can - but it may be a bit tricky.
You should use the namespace concept (give a look here: gitnamespaces)
Quoting from the docs:
Git supports dividing the refs of a single repository into multiple namespaces, each of which has its own branches, tags, and HEAD. Git can expose each namespace as an independent repository to pull from and push to, while sharing the object store
and
Storing multiple repositories as namespaces of a single repository avoids storing duplicate copies of the same objects, such as when storing multiple branches of the same source.
To activate a namespace you can simply:
export GIT_NAMESPACE=foo
or
git --namespace=foo clone/pull/push
When a namespace is active, through git remote show origin you can see only the remote branches created in the current namespace. If you deactivate it (unset GIT_NAMESPACE), you will see again the main remote branches.
A possible workflow in your situation may be:
Create a feature branch and work on it
export GIT_NAMESPACE=foo
git checkout -b feature_branch
# ... do the work ...
git commit -a -m "Fixed my ticket from backlog"
git push origin feature_branch # (will push into the namespace and create the branch there)
Merging upstream
unset GIT_NAMESPACE
git checkout master
git pull (just to have the latest version)
git merge --squash --allow-unrelated-histories feature_branch
git commit -a -m "Merged feature from backlog"
git push # (will push into the main refs)
The tricky part
Namespace provides a complete isolation of branches, but you need to activate and to deactivate namespace each time
Pay attention
Pay attention when pushing. Git will push in the current namespace. If you are working in the feature branch and you forgot to activate the namespace, when pushing, you will create the feature branch in the main refs.
It seems as if the simplest solution here, since you're using GitHub and a pull-request workflow, is that developers should be pushing to their own fork of the repository rather than to a shared repository. This way their remote feature branches aren't visible to anybody else, so any "clutter" they see will be entirely their own responsibility.
If everything lives in a single repository, another option would be to set up a simple web service that receives notifications from github when you close a pull request (responding to the PullRequest event). You could then have the service delete the source branch corresponding to the pull request.
This is substantially less simple than the previous solution, because it involves (a) writing code and (b) running a persistent service that is (c) accessible to github webooks and that (d) has appropriate permissions on the remote repository.
The first answers are good. If you can fork repositories and use pull-requests or just keep the branches for yourself, do it.
I will however put my 2 cents in case you are in my situation : a lot of WIP branches that you have to push to a single repository since you work on multiple workstations, don't have fork possibilities, and don't want to annoy your fellow developers.
Make branches starting with a specific prefix, i.e. wip/myuser/, fetch from / push to a custom refspec, i.e. refs/x-wip/myuser/*.
Here is a standard remote configuration after a clone:
[remote "origin"]
url = file:///c/temp/remote.git
fetch = +refs/heads/*:refs/remotes/origin/*
To push branches starting with wip/myuser/ to refs/x-wip/myuser/, you will add:
push = refs/heads/wip/myuser/*:refs/x-wip/myuser/*
This will however override the default push rule for the normal branches. To restore it, you will add:
push = refs/heads/*:refs/heads/*
Finally, to fetch you WIP branches that are now outside the conventional refs/heads/* refspec, you will add:
fetch = +refs/x-wip/myuser/*:refs/remotes/origin/wip/myuser/*
You will end up with this 2nd remote configuration:
[remote "origin"]
url = file:///c/temp/remote.git
fetch = +refs/x-wip/myuser/*:refs/remotes/origin/wip/myuser/*
fetch = +refs/heads/*:refs/remotes/origin/*
push = refs/heads/wip/myuser/*:refs/x-wip/myuser/*
push = refs/heads/*:refs/heads/*
(Git evaluates fetch / push rules from the top to the bottom, and stops as soon as one matches; this means you want to order your rules from the most to the less specific rule.)
People using the standard remote configuration will only fetch branches from refs/heads/*, while you will fetch branches from both refs/heads/* and refs/x-wip/myuser/* with the 2nd configuration.
When your branch is ready to be "public", remove the wip/myuser/ prefix.
The refspec internal documentation was useful to make it.
Please note that once you have push rules in your remote configuration, running the command...
git push
... with no arguments will no longer only push your current branch, nor use any strategy defined with the push.default configuration. It will push everything according to your remote push rules.
You will either need to always specify the remote and the branch you want to push, or use an alias as suggested in this answer.

Creating a branch from issue in GitLab

I've just started using GitLab, and have created a set of issues, in order to keep an overview of what needs to be done for my application. I was wondering if it was possible to create a branch from these issues, such that the branch and issues are linked, similar as in jira and Stash from atlassian?
If you create a branch with the name <issue-number>-issue-description and push that branch to gitlab, it will automatically be linked to that issue. For instance, if you have an issue with id 654 and you create a branch with name 654-some-feature and push it to gitlab, it will be linked to issue 654.
Gitlab will even ask you if you want to create a merge request and will automatically add Closes #654 to the merge request description which will close issue 654 when the merge request is accepted.
Also if you go to a given issue page on gitlab, you should see a New Branch button which will automatically create a branch with a name of the form <issue-number>-issue-description.
On the Issue screen, you see a green button labeled "Create merge request", with a down-arrow to its right.
That's not a button, that's a drop down list of buttons.
Click on the down arrow
Choose "Create branch"
Click on "Create branch"
A branch is created from the issue number, plus the title of the branch
For example, my issue #2, with title "Test repoSearch with no repos"
will have a branch called:
2-test-reposearch-with-no-repos
Notice that it changes case to all-lower-case
TLDR: do a merge request add #2 in the title and/or in the comment box and/or the commit message and it will link the issue to the branch and commit, you could just do a MR right from the start to link it.
It seems the only option for Gitlab is name your branch following this format: <issue-number-some-branch> like 2-bad-ai this will autolink the branch to the issue.
However, I organize my branches so they live nicely in the .git/ref/heads folder structure like this feature/2-<some-branch> then when you do a merge request add #2 in the title and/or in the comment box and/or the commit message and it will link the issue to the branch and commit, you could just do a MR right from the start to link it.
$ ls .git/refs/heads/; ls .git/refs/remotes/upstream/
2-bad-ai dev feature/ hotfix/ master release/
2-bad-ai dev feature/ hotfix/ master release/
I'd much rather have feature/2-bad-ai in the above output...
From what I can tell on Github you can link pull-request to feature/2-<some-branch> but doesn't have the autolinking of 2-<some-branch> like GitLab
2017:
If you create a branch with the name -issue-description and push that branch to GitLab, it will automatically be linked to that issue.
This is now customizable, with GitLab 15.6 (November 2022):
Configure default names for branches created from issues
Define a custom template for naming branches created from issues. The previous setting {issue ID}-{issue-title-hyphenated} remains the default.
To define a custom template for your project, go to Repository Settings > Branch defaults.
See Documentation and Issue.

Resources