I am trying to setup a pre-receive hook in github that I used to use on STASH. In STASH, I had a pre-receive hook that used to enforce "A custom commit message that should have a JIRA number included in it".
Now, I am trying to understand what would be the best way to do something similar on github. If I split it up, it would be:
Requiring a custom commit message.
Every commit should include an existing JIRA.
Enforce this on any pull request as well.
Eg: TEST-1 Adding the first commit message.
Can anybody here help me, how can this be done ?
GitHub only offers webhooks, which allows you to listen to and react to certain events, including the push.
But that only allows you to react to a push (like a post-receive hook would), not to prevent it.
You could build a listener to that push event which would:
examine the latest commit just pushed
reset to HEAD~1 if the commit doesn't follow the expected policy (push --force)
But that would be tricky for the user who initially pushed that commit to realize that said commit just disappeared from the GitHub repo.
A better solution would be to setup a bare repo in a server where you could setup that pre-receive hook: if that commit passes, then a post-receive hook would push it to the intended GitHub repo.
But depending on your team, it might be difficult to setup a repo which is accessible by everyone.
Related
Is there any way to have the full source code repo after each commit?
I mean for example using
https://api.github.com/repos/highcharts/highcharts/commits
would give me a list of commits, but I want to realize what was the effect of that commit to whole repo (I want to check whether any code duplication is added to whole project or not using some automatic tools). Is that possible?
I want to see the code effect, so having repo even after merging each commit would be fine.
Simply implement a listener to a webhook (see "Creating a Webhook"). Set it up to ball your listener at each push event.
You can then do a pull when called by the webhook, and get a fresh updated copy of the repo locally.
For other repos you don't have any control or whose owner is in your team, that webhook approach is not possible.
You would need to implement a scheduled polling approach, through a regular cron job for instance.
That would possible multiple commits, so you need to wrap that pull with a git log as in here.
git checkout master
git fetch
refs=$(git log --format='%H' ..origin/master)
for ref in ${refs}; do
# do your analysis commit by commit
done
git merge origin/master
As my browser was crashing I was not able to create a pull request from GitHub UI, I was trying to create a pull request from git console but didn't found any way to create a pull request.
I have also searched for the same over the net but most of the tutorials suggested to create a pull request from UIs only. I just want to be able to handle the pull request like creation, rejection, approved and merging without the browser. Please suggest.
git and github are separate products. github is a service that happens to use git, but it is not part of git, its UI is not a git interface, and git does not have any special support for github functionality.
There is a little potential confusion here, because there are "pull requests" - an integrated feature of github having to do with branch workflow - and there is git request-pull, a seemingly lesser-known feature of git which creates a text message you could send to another repository maintainer to request that they pull changes from your repository.
Naming confusion aside, the "pull request" you want is a feature of github, not git; and so git itself (including git console and any git UI tool) have no command for this. You have to use a github-supplied interface, and AFAIK that means the web UI.
To open a pull request from the command line, you can install the hub set of command-line tools that GitHub supports. This will allow you to create a pull request from a repository with:
hub pull-request
I'm not aware of any similar tools for managing pull requests, though.
git add -p
then
git commit -m "message"
then
git push origin "your_branch"
the pull request will be created in github
I properly set up YouTrack & GitLab integration and it seems to be working. I used this tutorial: https://www.jetbrains.com/help/youtrack/standalone/Integration-with-Version-Control-Systems.html
But when I mention issue ID in commit message
$ git commit -m "my message #PROJ-1"
after push, it doesn't show in YouTrack at VCS Changes tab. I have to add the commit hash manually (via YouTrack) and only then it's ok.
Am I missing something?
For this integration to work, YouTrack server has to be accessible by GitLab server, because the later notifies the former about commits. Manually adding changes work another way: YouTrack initiates connections to GitLab.
I am noticing that when i create a new local branch, it doesn't get picked up in Github, and therefore the Jira webhook also doesn't create a transition trigger:
git branch -b new-local-branch
The Jira trigger is supposed to recognize "Branch created" event from Github webhook, but that doesn't exist until I push, which also then shows the new branch for the first time in the <>Code tab.
So, is this performing as expected, or is there another way to create a branch which gets pickedup without a push?
So, is this performing as expected
Yes: a webhook reacts to an event (here a push event).
As long as your branch remains local, said webhook has no reason to be activated.
But once created, you could push your branch immediately (before making any new commit in it): that would allow the webhook and Jira to pick up on it.
Is there another way to create a branch which gets picked-up without a push?
If you are creating your local branch from a commit which is already pushed, you could consider the reverse approach: creating your branch directly from GitHub web GUI. But that might not trigger the webhook though.
As a GitHub administrator, I would like to lock a particular branch in GitHub for all users.
For e.g. if I do not want anyone to push to Master/Production or a Project branch, how can I do that.
Instead of using any client side hooks, is there a way to lock a branch on GitHub server directly ? Are there any third party tools/api's/scripts which can help achieve this ?
#Saurabh, I have done a similar thing according to your requirement on GitHub:
Navigate to Settings
Navigate to Branches
Tap on Add Rule near "Branch protection rules"
Tick the Require pull request reviews before merging checkbox
These steps apply a lock on, for example to master, so that no collaborators can push code to this branch. Code only be merged using pull requests.
Link to documentation
Screenshots:
Note: Protected branches are available to Pro, Team, and Enterprise users
The easiest solution is to have that branch in its own repo (for which no collaborators) are declared.
Otherwise, GitHub doesn't provide any native "branch protection" feature, as mentioned in "How to protect “master” in github?"
You could setup a webhook which on a push event can refuse the push if the branch has a given name.
An example would be terite/pull-to-master which protects master:
if (json.ref != 'refs/heads/master')
return cb([200, 'Skipping, not master']);
This is not a client-side hook, but it does require a client to listen to the JSON payload of the push event in order to react to it.
Since Oct. 2022, there is a simpler option:
New Branch Protections: Last Pusher and Locked Branch (Oct. 2022)
Push protection enabled.
This allows for branches to be locked, prohibiting changes.
You can lock a branch allowing you to have a maintenance window and prevent changes, or to protect a fork so it only receives changes from its upstream repository.
To use this feature in a branch protection rule, enable Lock branch.
For more information, read About protected branches in the GitHub documentation.
We appreciate feedback on this and other topics in GitHub's public feedback discussions.