Makistos
10/28/2013 - 7:43 AM

Creates a defect list from ClearQuest that can be used in the release note as such. #clear-quest #scm #perl #cqperlext

Creates a defect list from ClearQuest that can be used in the release note as such. #clear-quest #scm #perl #cqperlext

use CQPerlExt; 

############
# Read command line parameters
############

my $path = ".";
my $old_file = "";
my $new_file = "";
my $restrict_val= "SW_PHY";
my $restrict_col = "Defect_Area";
my $show_help = "";
my $login_name = "";
my $login_password = "";
my $database = "Prod";

while($parm = shift)
{
	$switch = substr($parm,1,1);
	$val=substr($parm,2);
	SWITCH: for ($switch) {
		/u/ && do {$login_name=$val; last;};
		/w/ && do {$login_password=$val; last;};
		/d/ && do {$database=$val; last;};
		/p/ && do {$path=$val; last;};
		/i/ && do {$old_file=$val; last;};
		/o/ && do {$new_file=$val; last;};
		/r/ && do {$restrict_val=$val; last;};
		/h/ && do {$show_help="1"; last;};
		do {$show_help="1";last;};
	}
}

if ($show_help ne "" || $old_file eq "" || $new_file eq "")
{
	print_help(); # and exit
}

# If user supplied the \ character at the end of path name, remove it
if ($path =~ /\\$/)
{
	chop $path;
}

# Set file names
$old_file = $path . "\\defects_" . $old_file . ".txt";
$new_file = $path . "\\defects_" . $new_file . ".txt";

# If user supplied a different restriction field, set $restrict_col accordingly
if ($restrict_val =~ /\=/)
{
	my @tmp_arr = split($restrict_val, /\=/);
	$restrict_col = $tmp_arr[0];
	$restrict_val = $tmp_arr[1];
}

print "Path: $path\n";
print "Old defect file: $old_file\n";
print "New defect file: $new_file\n";
print "Restricting column: $restrict_col\n";
print "Restricting value: $restrict_val\n";
print "Login name: $login_name\n";
print "Database: $database\n";

unless (-e $new_file)
{
	die "ClearQuest ID and password required!\n" unless ($login_name ne "" && $login_password ne "") ;

	# Input file does not exist -> get info from CQ
	open (NEWREL, ">", $new_file) or die "Can not create new defect file!\n";

	###########
	# Get Defect Info from CQ
	###########
        print "Reading from CQ\n";
	my $session = CQSession::Build();
	$session->UserLogon ($login_name, $login_password, $database, "");
	my $queryDef = $session->BuildQuery("defect");
	$queryDef->BuildField("id");
	$queryDef->BuildField("headline");
	my $resultSet = $session->BuildResultSet($queryDef);
	$resultSet->Execute();

	# Read info
	while(($resultSet->MoveNext()) == 1)
	{
		$id= $resultSet->GetColumnValue(1);
		$rec = $session->GetEntity("Defect",$id);
		$head = $rec->GetFieldValue("Headline")->GetValue();
		$severity = $rec->GetFieldValue("Severity")->GetValue();
		$state = $rec->GetFieldValue("State")->GetValue();
		$restrict = $rec->GetFieldValue("$restrict_col")->GetValue();
		if ($restrict eq $restrict_val)
		{
			print NEWREL "$id\t$head\t$severity\t$state\n";
			print "+";
		}
		else {
			print "-";
		}
	}

	# Let's just re-open the new defect file, simpler that way
	close NEWREL;
}

open (LASTREL, "<", $old_file) or die "Old defect file does not exist!\n";
open (NEWREL, "<", $new_file)  or die "Unable to open new defect file!\n";

##############
# Read defect info from both files to hashes
##############

# Read old release info
while(<LASTREL>)
{
	chop;
	@arr = split(/\t/, $_);
	if(/^Prod/)
	{
		# For old defects, we will only need their state 
		# (ID is used as the hash key)
		$old_def{$arr[0]} = $arr[3];
	}
}

# Read new release info
while(<NEWREL>)
{
	chop;
	@arr = split(/\t/, $_);
	if(/^Prod/)
	{
		# Insert entire string
		$new_def{$arr[0]} = join('@@@', @arr); # For some reason \t did not work here
	}
}
close LASTREL;
close NEWREL;

##########################
# Analyze defects, determine what to do with them
##########################

for my $key(keys %new_def)
{
	if(exists $old_def{$key})
	{
		# Check if the defect was already in the old list
		$old_val = $old_def{$key};
	}
	else
	{
		# It wasn't
		$old_val = "";
	}

	# Get defect info from hash containing new defects
	$new_val = $new_def{$key};
	# Get string for this defect, split in useful chunks
	@tmp_arr = split(/@@@/,$new_val); 

	# Determine what to do with the defect
	if($tmp_arr[3] eq "Assigned" || $tmp_arr[3] eq "Opened" ||
		$tmp_arr[3] eq "Postponed")
	{
		# Open defect
		push @open_defects, "$tmp_arr[0]\t$tmp_arr[1]";
	}

	if($tmp_arr[3] eq "Resolved")
	{
		# Resolved defect
		push @resolved_defects, "$tmp_arr[0]\t$tmp_arr[1]";
	}

	if($tmp_arr[3] eq "Closed" || $tmp_arr[3] eq "Duplicate" ||
		$tmp_arr[3] eq "Verified" || $tmp_arr[3] eq "Rejected")
	{
		if($old_val eq "Assigned" || $old_val eq "Opened" ||
			$old_val eq "Resolved" || $old_val eq "")
		{
			# Closed defect
			push @closed_defects, "$tmp_arr[0]\t$tmp_arr[1]";
		}
	}
}

##############
# Create output to STDOUT
##############

open (OUTFILE, ">", "$path\\output.txt")  or die "Unable to create output.txt!\n";

# Sort defects by id
@closed_defects = sort(@closed_defects);
@resolved_defects = sort(@resolved_defects);
@open_defects = sort(@open_defects);

foreach $defect (@closed_defects)
{
	$defect_c = $defect_c . "\t$defect\n";
}

foreach $defect	(@resolved_defects)
{
	$defect_r = $defect_r . "\t$defect\n";
}

foreach $defect (@open_defects)
{
	$defect_o = $defect_o . "\t$defect\n";
}

# Actually print the data to the output file

print OUTFILE "    
    2.3 CLOSED DEFECTS
$defect_c

    2.4 RESOLVED DEFECTS;
$defect_r

    2.5 OTHER CHANGES
	-

    2.6 KNOWN PROBLEMS
$defect_o
";

close OUTFILE;

########
# Sub functions
########

sub print_help
{
print "
Usage
$0 -iv1 -ov2 [-ppath] [-rcol=val] [-uuserid] [-wpassword] [-bdatabase]
Where v1       - old version file,
      v2       - new version file
      path     - path where all the files are located (default current dir),
      col      - column used to restrict which defects to include
                  (default \"Defect_Area\"),
      val      - value used to restrict which defects to include
                 (default \"SW\"),
	  userid   - CQ user name,
	  password - CQ password,
	  database - CQ database (default \"Prod\").

-i and -o parameters must be supplied. If file supplied with -o parameter does
not exist, information is loaded from ClearQuest.

These parameters defines the version number, the actual files are named as
defects_xxx.txt where xxx = the parameter supplied.

-r parameter defines what defects to include. col should be the column
name in CQ and val the value to match on that column.
E.g. -rDefect_Area=SW

-u and -w are required if fetching defects from CQ.

";
	exit();	
}