Mandatory check for Red Mine issue # during checkin in svn

Added by dinesh kumar over 9 years ago

Dear Reader ,

I need favour. while commit or checkin in svn with issue Id , is it possible to check issue Id's existance in redmine? 
if issue Id does n't exists, commit or check in should not be happened, it should show error message.

Replies (12)

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by Jonathan Monahan over 9 years ago

You need to go and learn about SVN hook scripts.

Here's a piece of my pre-commit SVN hook script that will help you:

REPOS="$1" 
TXN="$2" 

cd `dirname $0`

# Note that we can rely on PATH containing /usr/local/bin:/bin:/usr/bin
# so take advantage of this.
SVNLOOK=svnlook
MYSQL=mysql

MESSAGE=$($SVNLOOK log -t "$TXN" "$REPOS")

# Make sure that the log message contains some text.
[[ $MESSAGE =~ "[a-zA-Z0-9]" ]] || \
   { echo "No log message." 1>&2; exit 1; }

# Make sure that the log message references a Redmine issue.
[[ $MESSAGE =~ "^Issue\s*#([0-9]+)\s*" ]] || \
   { echo "You didn't specify a Redmine issue number on the first line, e.g.:
    Issue #1234
    message..." 1>&2; exit 1; }

REDMINE_ISSUE=${BASH_REMATCH[1]}
REDMINE_ISSUE_OPEN=$(${MYSQL} -N -u redmine_readonly redmine -e "SELECT COUNT(*) FROM issues I INNER JOIN issue_statuses S ON S.id = I.status_id WHERE S.is_closed = 0 AND I.id = ${REDMINE_ISSUE};")

if [[ ${REDMINE_ISSUE_OPEN} -eq 0 ]]
then
  echo "Redmine issue #${REDMINE_ISSUE} is not in an open state." 1>&2; exit 1;
fi

The above code checks that:

  1. There is a commit message
  2. The message starts with "Issue #nnnn"
  3. The issue number nnnn is in the Redmine issues table in MySQL
  4. The referenced issue is not closed

Obviously, this will need modifying to suit your environment, e.g. different database, database not on the same machine as SVN, different requirements on the commit message or on the state of the issue, etc.

BTW, the SVN pre-commit hook script is also a good place to put other checks such as:

  1. controlling who can check in to which modules
  2. syntax checking source code to suit company coding guidelines
  3. checking things like making sure no-one checks in 2 Rails migrations with the same number
  4. etc.

Cheers,
Jonathan.

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by Jonathan Monahan over 9 years ago

Note there is a typo in the code above - a problem with textile that I couldn't seem to workaround - the bit in red (the last 'if' statement) should have double-square brackets around it.

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by dinesh kumar over 9 years ago

hai Jonathan Monahan,

Thanks for the solution . Can you lend me email id for further, if it is possible .

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by Jonathan Monahan over 9 years ago

Hi Dinesh,
I'd rather not put my email address on the forum. I do watch posts on redmine.org, so I will see any further comments though, so please feel free to post further comments.

Cheers,
Jonathan.

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by dinesh kumar over 9 years ago

hai jonathan,

I have installed the Collabnet subversion (/opt/Collabnet_subversion/). In that directory structure, can't find hook directory for changing the pre-commit-script.tmpl. can u guide me to implement the hook script with steps (I am learner plz) ..

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by Thomas Löber over 9 years ago

It's part of your repository: /path/to/repository/hooks

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by Jonathan Monahan over 9 years ago

Hi Dinesh,
Take a look at http://svnbook.red-bean.com/en/1.0/svn-book.html (or buy the book from O'Reilly: "Version Control with Subversion" by Ben Collins-Sussman Brian W. Fitzpatrick and C. Michael Pilato, ISBN-10: 0-596-00448-06, ISBN-13: 978-0-596-00448-4 ), and in particular, chapter 5: http://svnbook.red-bean.com/en/1.0/svn-book.html#svn-ch-5-sect-2.1

This will teach you about SVN hooks. Also search online for SVN hooks - there is quite a lot of examples of pre-commit and post-commit hooks for subversion to do various things, and there is a set of example hook scripts on the subversion website at http://subversion.tigris.org.

Cheers,
Jonathan.

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by Fabrice Dominguez over 8 years ago

Hello,

I have adapted this script into ruby for a windows configuration, with the couple VisualSVN server and redmine.
I have also considered this page http://snippets.dzone.com/posts/show/2908.

The pre-commit hook configuration is
ruby.exe e:\subversion\svn_scripts\pre-commit.rb %1 %2

And the source for the pre-commit.rb file :

repo_path = ARGV[0]
transaction = ARGV[1]
svnlook = 'svnlook.exe'
mysql = 'mysql.exe'

commit_log = `#{svnlook} log #{repo_path} -t #{transaction}`

if (commit_log == nil || commit_log.length < 2)
  STDERR.puts("Log message cannot be empty.")
  exit(1)
end

if (commit_log =~ /Issue\s*#(\d+)\s*/)
  issue_number = $1
  redmine_issue_open = `#{mysql} --database=redmine --user=your_user --password=your_password -e "SELECT COUNT(*) AS result FROM issues I INNER JOIN issue_statuses S ON S.id = I.status_id WHERE S.is_closed = 0 AND I.id = #{issue_number};" --skip-column-names`.strip()
  if (redmine_issue_open.eql?("0"))
    STDERR.puts("Redmine issue #{issue_number} is not in an open state.")
    exit(1)
  end
else
  STDERR.puts("You didn't specify a Redmine issue number on the first line, e.g.:
    Issue #1234
    message...")
  exit(1)
end

I guess this would help.

Fabrice.

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by Brad Rushworth over 8 years ago

This is slightly improved UNIX version of the code above:

#!/usr/local/bin/ruby

repo_path = ARGV[0]
transaction = ARGV[1]
svnlook = '/usr/local/bin/svnlook'
mysql = '/usr/local/bin/mysql'

commit_log = `#{svnlook} log #{repo_path} -t #{transaction}`

if (commit_log == nil || commit_log.length < 2)
  STDERR.puts("Log message cannot be empty.")
  exit(1)
end

if (commit_log =~ /^(refs|references|IssueID|fixes|closes)\s*#(\d+)/i)
  issue_number = $2
  sql = "SELECT COUNT(*) AS result FROM issues I INNER JOIN issue_statuses S ON S.id = I.status_id WHERE S.is_closed = 0 AND I.id = #{issue_number};" 
  redmine_issue_open = `#{mysql} --database=redmine --user=redmine --password=blahblah -e "#{sql};" --skip-column-names`.strip()
  if (redmine_issue_open.eql?("0"))
    STDERR.puts("Redmine issue #{issue_number} is not in an open state.")
    exit(1)
  end
else
  STDERR.puts("You didn't specify a Redmine issue number on the first line, e.g.:
    Refs #1234
    Your message goes here...")
  exit(1)
end

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by Brad Rushworth over 8 years ago

I took my code posted above, and made it compatible with Windows XP and SQLite v3. Hope this helps someone.

pre-commit.bat:

SET DIR=%1/hooks
SET REPOS="%1" 
SET TXN="%2" 

c:\ruby\bin\ruby.exe %DIR%\pre-commit.rb %REPOS% %TXN% %DIR%

pre-commit.rb

#!/usr/local/bin/ruby

repo_path = ARGV[0]
transaction = ARGV[1]
svnlook = 'C:\Program Files\CollabNet Subversion Server\svnlook.exe'
database = 'C:\ruby\bin\sqlite3.exe C:\Data\redmine\db\production.db'

commit_log = `#{svnlook} log #{repo_path} -t #{transaction}`

if (commit_log == nil || commit_log.length < 2)
  STDERR.puts("Log message cannot be empty.")
  exit(1)
end

if (commit_log =~ /^(refs|references|IssueID|fixes|closes)\s*#(\d+)/i)
  issue_number = $2
  sql = "SELECT COUNT(*) AS result FROM issues I INNER JOIN issue_statuses S ON S.id = I.status_id WHERE S.is_closed = 'f' AND I.id = #{issue_number};" 
  redmine_issue_open = `echo #{sql} | #{database}`.strip()
  if (redmine_issue_open.eql?("0"))
    STDERR.puts("Redmine issue #{issue_number} is not in an open state.")
    exit(1)
  end
else
  STDERR.puts("You didn't specify a Redmine issue number on the first line, e.g.:
    Refs #1234
    Your message goes here...")
  exit(1)
end

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by Tom DeMay almost 7 years ago

Is there a way to validate the issue # through wget call? I don't have mysql on the svn machine and I rather not mess with the machine.

i was hoping to get 404 back when I executed wget http://myhost/issues/9999999 (invalid number)

but since I don't allow anonymous logins and it's not a public project the resulting page is login form.

RE: Mandatory check for Red Mine issue # during checkin in svn - Added by Cristian Lopez over 6 years ago

Hello, is there any plugin for show the commits in redmine ?

For example .. if you have commited relationing the commit with issue number X, in the issue number X shows the change log in the description or another side.

Thanks a lot!

(1-12/12)