#
# Copyright (c) 2011, Karl Wirth, Perforce Software. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# User contributed content on the Perforce Public Depot is not supported by Perforce,
# although it may be supported by its author. This applies to all contributions
# even those submitted by Perforce employees.
#=====================================================================================
#
# Author: K.Wirth
# Date: 2014/11/04
# Version: 1
#
# Description:
# Example trigger script to write changelist and list of files that fixed job to a
# job field so they can be copied to JIRA by P4DTG. If this feeld is configured
# to render using the 'WIKI style' renderer. More information on this can be
# found in:
# https://confluence.atlassian.com/display/JIRA/Configuring+Renderers
#
# Important Note:
# This is only an example script to demonstrate how you could use P4DTG to
# replicate data and have it displayed as an active URL to p4web in JIRA.
# It has not been tested and is not ready to be installed in a production
# environment.
#
# Example usage:
# (1) Create a custom Text field in the Perforce jobspec.
# 135 P4WebURLS text 0 optional
#
# (2) Create a custom 'Free Text Field (unlimited text)' in JIRA and configure
# it to render with a custom renderer.
#
# (3) Modify 'jira-config.xml' and set the JIRA custom field to be
# type 'TEXT'. For example:
#
#
# (4) Use P4DTG-Config to 'Check Connection' to the JIRA source, click
# 'Apply' then map P4WebURLS-->CustomTextField.
#
# (5) Test the trigger at the command line by output a job that has fixes
# to a text file, then running the script against this file. For
# example:
# p4 job -o job001234 > tmp.txt
# perl jira6_p4web_url_trigger.pl tmp.txt job001234
# After running the script 'tmp.txt' should contain the change and list
# of files in P4Web URL format.
#
# (6) Modify the environment section below to match your Perforce and
# P4Web settings.
#
# (7) Creat a 'form-out' trigger definition for the script.
# Triggers:
# jira form-out job "perl ./jira6_p4web_url_trigger.pl %formfile% %formname%"
#
#
#=====================================================================================
#
# EDIT THIS SECTION TO MATCH YOUR ENVIRONMENT
#
#=====================================================================================
# Define location of P4 binary
# $p4 = "C:\\Program Files\\Perforce\\p4.exe";
$p4 = "/usr/local/bin/p4";
# Define Perforce connection details
$p4user ="test";
$p4password = "Password";
$p4port = "localhost:6789";
$p4client = "test_ws";
# Define job field to contain URLS (must be in jobspec)
$urljobfield="P4WebURLS";
# Define P4WEB server name
$p4weburl = "http://localhost:1234/";
# Set debugging on (0=off,1=On)
$debug=0;
#=====================================================================================
# Check for 2 arguments then set them
if ($#ARGV != 1)
{
print "usage: $0 tmp-file jobname";
exit(1);
}
$tmpfile = $ARGV[0];
$jobname = $ARGV[1];
$p4cmd = "$p4 -u $p4user -P $p4password -p $p4port -c $p4client";
@fixes_output=`$p4cmd fixes -j $jobname`;
print @fixes_output if $debug;
# Build up list of files that fixed change
if (@fixes_output)
{
foreach $change (@fixes_output)
{
@changelist=split(' ',$change);
$fix=@changelist[4];
@revlist=`$p4cmd -Ztag describe -s $fix`;
print @revlist if $debug;
# Loop through each change extracting revision
foreach $line (@revlist)
{
if (($line =~ m/\.\.\. change/) && !($line =~ m/\.\.\. changeType/))
{
# Get Changelist Number, create heading and link
$line =~ s/\.\.\. change /$1/;
$line =~ s/\r|\n//g;
#$urlline = " --\n Change:${line} ${p4weburl}/7?ac=${line}\n";
$urlline = " \n Change:[${line}|${p4weburl}/7?ac=${line}]\n";
push (@weburls,$urlline);
print "urlline:".$urlline if $debug;
}
if ($line =~ m/depotFile/)
{
# Remove ... depotFileNNN
$line =~ s/\.\.\. depotFile\d* /$1/;
# Add URL and replace spaces
$line =~ s/\/\//$p4weburl/g;
# Clean string (replace space and line end)
$line =~ s/ /\%20/g;
$line =~ s/\r|\n//g;
print "line:".$line if $debug;
$urlline = $line;
}
if ($line =~ m/\.\.\. rev/)
{
# Remove ... revNNN
$line =~ s/\.\.\. rev\d* /$1/;
# Append to URL
$urlline = " [${urlline}?ac=22&rev1=${line}|${urlline}?ac=22&rev1=${line}]";
$urlline =~ s/\\|\R//g;
print "urlline:".$urlline if $debug;
push (@weburls,$urlline);
}
}
print "weburls:\n" if $debug;
print @weburls if $debug;
}
# Replace P4WebURLs in job
open (FILE,"$tmpfile") || die("Could not open temp file $tmpfile\n");
@jobform = ;
print @jobform if $debug;
$webblock=0;
foreach $formline (@jobform)
{
if ($formline =~ /^$/) { $webblock=0;}
if ($formline =~ /$urljobfield/) {$webblock=1;}
if ($webblock == 0) {push(@newform,$formline);}
}
close (FILE);
push (@newform,"$urljobfield:\n");
push (@newform,@weburls);
print @newform if $debug;
# Overwrite tmp file with new contents
open(FILE,">$tmpfile") || die ("Could not write to temp file $tmpfil\n");
print FILE @newform;
close (FILE);
}#If array contains data