build/buildutils/svn2change.pl
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 #!/bin/perl
       
     2 #
       
     3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 # All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 #
       
    10 # Initial Contributors:
       
    11 # Nokia Corporation - initial contribution.
       
    12 #
       
    13 # Contributors:
       
    14 #
       
    15 # Description:
       
    16 #
       
    17 # script to process one or more changes implemented via a single Synergy task importing code from an external system (e.g., Subversion or  a zip file):
       
    18 #	- checks that the task is in completed state
       
    19 #	- associates the task to the change object
       
    20 #	- (optionally) sets "external task id(s)" -attribute to a value supplied
       
    21 #	- (optionally) moves the change to the next state (provided on the command line), including setting modifiable_in to integration_db
       
    22 #
       
    23 # Usage 1: 	If a change id is provided as argument, the script process a single change object as above
       
    24 #			perl change2released.pl -user username -change "xx1xx1#23456" -task 213 
       
    25 #
       
    26 # Usage 2: 	If an input file is provided as arguments, the script will read change ids (and optional external task ids) from the filw and
       
    27 #		process all the changes listed in the file
       
    28 #		with those tasks and transition the change to the target state 
       
    29 #
       
    30 #
       
    31 
       
    32 my $program_name;
       
    33 BEGIN {
       
    34     # Get the name of the program executable:
       
    35     ($program_name) = $0 =~ m{([^/]+)$};
       
    36 }
       
    37 
       
    38 my $VERSION = do { (my $v = q%version: 1 %) =~ s/.*://; sprintf("%d.%02d", 0, split(/\./, $v)) };
       
    39 
       
    40 
       
    41 use FindBin qw($Bin $Script);
       
    42 use lib "$Bin/lib";
       
    43 
       
    44 use ChangeSynergy::csapi;
       
    45 use Getopt::Long;
       
    46 use Data::Dumper;
       
    47 
       
    48 my $username = "";
       
    49 my $password = "";
       
    50 my $crId = "";
       
    51 my $taskId = "";
       
    52 my $newState = "";
       
    53 my $protocol = "https";
       
    54 #my $host = "sachswww.nmp.nokia.com";
       
    55 my $host = "trchswww.nmp.nokia.com";
       
    56 #my $port = "58613";
       
    57 my $port = "58602";
       
    58 #my $db = "/nokia/sa_nmp/groups/gscm/dbs/sa1trqa2";
       
    59 my $db = "/nokia/tr_nmp/groups/gscm/dbs/tr1jrt";
       
    60 my $inFile = "";
       
    61 
       
    62 # read options
       
    63 my $result = GetOptions ("user=s" => \$username,
       
    64                          "password=s" => \$password,
       
    65                          "debug" => \$debug,
       
    66                          "preview" => \$preview,
       
    67                          "version" => \$show_version,
       
    68                          "protocol=s" => \$protocol,
       
    69                          "host=s" => \$host,
       
    70                          "port=s" => \$port,
       
    71                          "db=s" => \$db,
       
    72                          "in=s" => \$inFile,
       
    73                          "state=s" => \$newState,
       
    74                          "task=s" => \$taskId,
       
    75                          "change=s" => \$crId);
       
    76 
       
    77 print "$program_name ver. $VERSION\n";
       
    78 
       
    79 # Handle standard command line parameters
       
    80 if ($show_version) {
       
    81 	# Exit immediately after having printed version information
       
    82 	exit 0;
       
    83 } 
       
    84 
       
    85 if (!$taskId)
       
    86 {
       
    87 	die "Error: No Task Id provided\n";
       
    88 }
       
    89 
       
    90 # Get the password from the terminal unless already provided on the command line
       
    91 unless ($password) {
       
    92 	eval {
       
    93 		require Term::ReadKey;
       
    94 		print STDERR "CIM Password for user '$username' (input will be hidden): "; 
       
    95 		Term::ReadKey::ReadMode('noecho');
       
    96 		$password = Term::ReadKey::ReadLine(0);
       
    97 		Term::ReadKey::ReadMode('normal'); 
       
    98 		print STDERR "\n";
       
    99 	};
       
   100 	if ($@) {
       
   101 		print STDERR "Warning: Perl module Term::ReadKey not available." .
       
   102 			" Password echo cannot be switched off!\n";
       
   103 		print STDERR "CIM Password for user '$username': "; 
       
   104 		$password = <STDIN>;
       
   105 	}		
       
   106 	chomp $password;
       
   107 }
       
   108 
       
   109 
       
   110 print "Using $protocol://$host:$port, database $db\n";
       
   111 
       
   112 my $externalTaskAttribute = "external_task_ids";
       
   113 
       
   114 my $aUser;
       
   115 
       
   116 my $csapi = new ChangeSynergy::csapi();
       
   117 my $globals = new ChangeSynergy::Globals();
       
   118 
       
   119 sub displayCRData
       
   120 {
       
   121 	my $cr = shift;
       
   122 	my $j = $cr->getDataSize();
       
   123 	for($i=0;$i<$j;$i++)
       
   124 	{
       
   125 		print "\t" . $cr->getDataObject($i)->getName() . ": " . $cr->getDataObject($i)->getValue() . "\n";
       
   126 	}
       
   127 } 
       
   128 
       
   129 sub associateTaskToChange
       
   130 {
       
   131 	my $crId = shift;
       
   132 	my $taskId = shift;
       
   133 	
       
   134 	# check if the task exists
       
   135 	my $tmp = $csapi->QueryStringData($aUser, "Basic Summary", "cvtype='task' and task('$taskId\')", "task_synopsis|status|release");
       
   136 	my $j = $tmp->getDataSize();
       
   137 	if ($j)
       
   138 	{
       
   139 		my $taskData = $tmp->getDataObject(0);
       
   140 		my $taskStatus = $taskData->getDataObjectByName("status")->getValue();
       
   141 		my $taskRelease = $taskData->getDataObjectByName("release")->getValue();
       
   142 		if ($debug)
       
   143 		{
       
   144 			for($i=0;$i<$j;$i++)
       
   145 			{
       
   146 				print "\tinstance:\t" . $taskData->getDataObjectByName("instance")->getValue() . "\n";
       
   147 				print "\tstatus:  \t" . "$taskStatus\n";
       
   148 				print "\trelease:  \t" . "$taskRelease\n";
       
   149 				print "\tsynopsis:\t" . $taskData->getDataObjectByName("task_synopsis")->getValue() . "\n";
       
   150 				#my $j2 = $taskData->getDataSize();
       
   151 				#for($i2=0;$i2<$j2;$i2++)
       
   152 				#{
       
   153 				#	print "\t" . $taskData->getDataObject($i2)->getName() . ": " . $taskData->getDataObject($i2)->getValue() . "\n";
       
   154 				#}
       
   155 			}
       
   156 		}
       
   157 
       
   158 		if ($taskStatus ne 'completed')
       
   159 		{
       
   160 			die "Error: task is not completed\n";
       
   161 		}
       
   162 		
       
   163 		if (!$preview)
       
   164 		{
       
   165 			print "Associating Task \'$taskId\' to Change \'$crId\'\n";
       
   166 
       
   167 			my $result = $csapi->CreateRelation($aUser, "TRUE", $crId, $taskId, "associated_task", $globals->{CCM_PROBLEM_TASK});
       
   168 			print $result->getResponseData() . "\n";
       
   169 		}
       
   170 		else
       
   171 		{
       
   172 			print "Would associate Task \'$taskId\' to Change \'$crId\'\n";
       
   173 		}
       
   174 	}
       
   175 	else
       
   176 	{
       
   177 		die "Error: task \'$taskId\' does not exist\n";
       
   178 	}
       
   179 } 
       
   180 
       
   181 # main program 
       
   182 
       
   183 eval
       
   184 {
       
   185 	$csapi->setUpConnection("$protocol", "$host", "$port");
       
   186 
       
   187 	$aUser = $csapi->Login("$username", "$password", "User", "$db");
       
   188 
       
   189   print "eval11\n";
       
   190 
       
   191 	# 1. find all the change objects to process
       
   192 	
       
   193 	my @crTable = ();
       
   194 	my %extTaskIdTable = ();
       
   195 
       
   196 	my $query = "";
       
   197 	if ($crId)
       
   198 	{
       
   199 		if ($debug)
       
   200 		{
       
   201 			print "Using Change id: \"$crId\"\n";
       
   202 		}
       
   203         push(@crTable, $crId);
       
   204 	}
       
   205 	elsif ($inFile)
       
   206 	{
       
   207 		my $fh;
       
   208 		my $extTaskId = "";
       
   209 		
       
   210 		open($fh, $inFile) or die "Failed to read input file $inFile: $!\n";
       
   211 		if ($debug)
       
   212 		{
       
   213 			print "Using input file: \"$inFile\"\n";
       
   214 		}
       
   215 		while (<$fh>) {
       
   216 			chomp;
       
   217 
       
   218 			# Skip comments and blanks
       
   219 			next if (substr($_, 0, 1) eq '#' || /^\s*$/);
       
   220 
       
   221 			($crId, $extTaskId) = split(/[\s]+/, $_);
       
   222 			
       
   223 			if ($debug)
       
   224 			{
       
   225 				print "Read Change id \"$crId\"\n";
       
   226 				if ($extTaskId) 
       
   227 				{
       
   228 					print "- external task id \"$extTaskId\"\n";
       
   229 				}
       
   230 			}
       
   231 			push(@crTable, $crId);
       
   232 			if ($extTaskId) 
       
   233 			{
       
   234 				$extTaskIdTable{$crId} = $extTaskId;
       
   235 			}
       
   236 		}
       
   237 	}
       
   238 	else
       
   239 	{
       
   240         die "No change id or input file provided\n";
       
   241 	}
       
   242 
       
   243 	# 2. Loop over the change objects and process them
       
   244 	
       
   245 	foreach $crId (@crTable)
       
   246 	{
       
   247 		my @taskList = ();
       
   248 		
       
   249 		print "Processing change: \'$crId\'\n";
       
   250 	
       
   251 		# 3. find the tasks to associate with this change object
       
   252 
       
   253 		if ($taskId)
       
   254 		{
       
   255 			# use the one from the command line
       
   256 			print "Using task: \'$taskId\'\n";
       
   257 			push(@taskList, $taskId);
       
   258 		}
       
   259 
       
   260 		# 4: 	associate all tasks and change the state
       
   261 		# 	bail out on failure and move to the next CR
       
   262 		eval
       
   263 		{
       
   264 			foreach $t (@taskList)
       
   265 			{
       
   266 				print "Processing task \'$t\'\n";
       
   267 				&associateTaskToChange($crId, $t);
       
   268 			}
       
   269 
       
   270 			my $externalTaskId = $extTaskIdTable{$crId};
       
   271 
       
   272 			my $crData = $csapi->GetCRData($aUser, $crId, "crstatus|integration_db|modifiable_in|$externalTaskAttribute");
       
   273 			
       
   274 			if ($newState)
       
   275 			{
       
   276 				my $crStatus = "change_" . $newState;
       
   277 
       
   278 				if (!$preview)
       
   279 				{
       
   280 					print "Transitioning Change \'$crId\' to $crStatus\n";
       
   281 				}
       
   282 				else
       
   283 				{
       
   284 					print "Would transition Change \'$crId\' to $crStatus\n";
       
   285 				}
       
   286 
       
   287 				# get the transition form
       
   288 				
       
   289 				# my $crData = $csapi->GetCRData($aUser, $crId, "crstatus|integration_db|modifiable_in|$externalTaskAttribute");
       
   290 				my $tmp = $csapi->TransitionCRData($aUser, $crId, $crData->getTransitionLink(1)->getTransition());
       
   291 				if ($debug)
       
   292 			 	{
       
   293 					&displayCRData($tmp);
       
   294 				}
       
   295 
       
   296 				# set the status on the form
       
   297 				$tmp->getDataObjectByName("crstatus")->setValue($crStatus);
       
   298 		  		# $tmp->getDataObjectByName("crstatus")->setValue($cr->getTransitionLink(1)->getToState());
       
   299 
       
   300 				if (!$preview)
       
   301 				{
       
   302 					# transition the CR
       
   303 					$result = $csapi->TransitionCR($aUser, $tmp);
       
   304 					print $result->getResponseData() . "\n";
       
   305 				}
       
   306 
       
   307 				# and finally, set the modifiable in
       
   308 				
       
   309 				my $integration_db = $crData->getDataObjectByName("integration_db")->getValue();
       
   310 				if (!$preview)
       
   311 				{
       
   312 					print "Setting modifiable_in to " . $integration_db . "\n";
       
   313 
       
   314 					$crData->getDataObjectByName("modifiable_in")->setValue($integration_db);
       
   315 				}
       
   316 				else
       
   317 				{
       
   318 					print "Would set modifiable_in to " . $integration_db . "\n";
       
   319 				}
       
   320 			}
       
   321 
       
   322 			if (!$preview)
       
   323 			{
       
   324 				if ($externalTaskId)
       
   325 				{
       
   326 					print "Setting $externalTaskAttribute to " . $externalTaskId . "\n";
       
   327 
       
   328 					$crData->getDataObjectByName($externalTaskAttribute)->setValue($externalTaskId);
       
   329 				}
       
   330 
       
   331 				$result = $csapi->ModifyCR($aUser, $crData);
       
   332 				print $result->getResponseData() . "\n";
       
   333 			}
       
   334 			else
       
   335 			{
       
   336 				if ($externalTaskId)
       
   337 				{
       
   338 					print "Would set $externalTaskAttribute to " . $externalTaskId . "\n";
       
   339 				}
       
   340 			}
       
   341 		};
       
   342 
       
   343 		if ($@)
       
   344 		{
       
   345 			print $@;
       
   346 		}
       
   347 	}
       
   348 };
       
   349 
       
   350 if ($@)
       
   351 {
       
   352 	print $@;
       
   353 }
       
   354