Server side pre-release hook to enforce issue numbers in commit messages

I am new to GitLab and I need a server side (client side is not an option) pre-release hook to enforce adding issue numbers in commit messages.

Any idea?

Thanks,
Rick

If anyone is interested.

Created an update hook on the git server for each project requiring an issue number in the push message.

#!/bin/sh
PATH=/usr/sbin:/usr/bin:/sbin:/bin

issue_number=$(git log --pretty=%s $2…$3 | egrep -m 1 ‘#[0-9]+’)
if [ -z “$issue_number” ]; then
echo “Error: Commit message must include issue number.”
echo " $issue_number"
exit 1
fi

2 Likes

Where did you add this hook in server machine? I need to restrict to committing the binary files like pdf, txt, dll… How could I do this?

This would be better done with the .gitattributes file restricting binary files.

I could not get the hook below to work because it does not account for new branches. I created a new hook referenced from Git Enforcement Policy as well as issues with new branches. Mine looks for JIRA numbers but yours could look for anything.

#!/usr/bin/env ruby


$refname = ARGV[0]
$oldrev  = ARGV[1]
$newrev  = ARGV[2]
$user    = ENV['USER']

puts "Enforcing Policies..."
puts "(#{$refname}) (#{$oldrev[0,6]}) (#{$newrev[0,6]})"

$regex = /\[JIRA:(\d+)\]/

if  $oldrev == '0000000000000000000000000000000000000000'
    # list everything reachable from newrev but not any heads
    missed_revs = `git rev-list $(git for-each-ref --format='%(refname)' refs/heads/* | sed 's/^/\^/') #{$newrev}`.split("\n")
    missed_revs.each do |rev|
    message = `git cat-file commit #{rev} | sed '1,/^$/d'`
    puts "#{message}"
    if !$regex.match(message)
      puts "[POLICY] Your message is not formatted correctly "
      exit 1
    end
  end

else
# enforced custom commit message format
  missed_revs = `git rev-list #{$oldrev}..#{$newrev}`.split("\n")
  missed_revs.each do |rev|
    message = `git cat-file commit #{rev} | sed '1,/^$/d'`
    puts "#{message}"
    if !$regex.match(message)
      puts "[POLICY] Your message is not formatted correctly"
      exit 1
    end
  end
end