Source

Bamboo JIRA Release Notes / bamboo-jira-releasenotes.php

Full commit
<?php
	DEFINE("BAMBOO", "https://confluence-bamboo.atlassian.com/rest/api/latest/"); //Must have /rest/api/latest at the end

	//Check and set script input
	if(defined('STDIN')){
		if (sizeof($argv) >= 8 || in_array($argv[1], array('--help', '-help', '-h', '-?', '?'))) {
			echo "\nJIRA Bamboo Release Notes Script Help:";
			echo "\n  Usage: ".$argv[0]." username password bamboo_projectkey bamboo_plankey bamboo_start_from_build_number <bamboo_end_build_number>\n\n";
			exit;
		}else{
			DEFINE("OUTPUT_FORMAT", ".json");
			DEFINE("BAMBOO_USER", $argv[1]);
			DEFINE("BAMBOO_PASSWORD", $argv[2]);
			DEFINE("BAMBOO_PROJECTKEY", $argv[3]);
			DEFINE("BAMBOO_PLANKEY", $argv[4]);												
			DEFINE("START_FROM_BUILD", $argv[5]);
			if (isset($argv[6])){
				DEFINE("END_BUILD", $argv[6]);
			}
			echo "\n [INFO] Starting release notes for ".BAMBOO_PROJECTKEY."-".BAMBOO_PLANKEY." from #".START_FROM_BUILD." with ".BAMBOO_USER." user...";
		}
	}
	else{
		echo "\n [ERROR] Not running from command line \n";
		exit;
	}
	

	//Workout what is the latest build
	$url_args = array("expand"=>"results[0].result");
	$request_latest = fetch("result/".BAMBOO_PROJECTKEY."-".BAMBOO_PLANKEY, $url_args);
	if (!$request_latest){
		echo "\n[ERROR] Error received from Bamboo. Error code: ".$a['http_code'];
		exit;
	}
	
	foreach ($request_latest->results->result as $buildCheck){
		echo "\n [INFO] Checking to see if there are any new builds since build #".START_FROM_BUILD."...";
		
		$build_difference = ($buildCheck->number-START_FROM_BUILD);
		if ($build_difference<=0){
			echo "\n [INFO] There are no new builds since build #".START_FROM_BUILD." (Last run: ".$buildCheck->buildRelativeTime.")";
		}else{
			echo "\n [INFO] There have been ".$build_difference." build(s) since build #".START_FROM_BUILD.". Latest build is #".$buildCheck->number.".";
			echo "\n [INFO] ------ START BUILD RESULTS ------ ";
			
			//Store the data for release notes
			$headlines = array();
			$left_column = array();
			$right_column = array();
			 			
			//Get the difference between the latest build and the build we want to start from
			$url_args = array("expand"=>"results[:".$build_difference."].result.jiraIssues", "max-results"=>$build_difference);	
			$request_diff = fetch("result/".BAMBOO_PROJECTKEY."-".BAMBOO_PLANKEY, $url_args);
			foreach ($request_diff->results->result as $build){
					echo "\n\n        Build #".$build->key;

					if (defined("END_BUILD") && ($build->number > END_BUILD)){
						echo "\n              Skipping build...reached end build limit of ".END_BUILD."...";
					}else{
						
						//If there are any JIRA issues
						if (($build->jiraIssues->size > 0)){
							foreach($build->jiraIssues->issue as $i){
								//Print data
								$issueType = (isset($i->issueType)) ? $i->issueType : "Unknown";
								$issueStatus = (isset($i->status)) ? $i->status : "Unknown";
								$issueSummary = (isset($i->summary)) ? $i->summary : "N/A";
								$issueAssignee = (isset($i->issueAssignee)) ? $i->issueAssignee : "Unknown";
								switch ($issueStatus) {
									case "Open":
  										$issueStatus = "{color:#DF0101}(".$issueStatus."){color}";
  										break;
									case "In Progress":
  										$issueStatus = "{color:#DF0101}(".$issueStatus."){color}";
  										break;
									case "Quality Review":
										$issueStatus = "{color:#FF8000}(".$issueStatus."){color}";
										break;
									case "Technical Review":
										$issueStatus = "{color:#FF8000}(".$issueStatus."){color}";
										break;
									case "Resolved":
										$issueStatus = "{color:#298A08}(".$issueStatus."){color}";
										break;
									default:
  										$issueStatus = "{color:#DF0101}(".$issueStatus."){color}";
								}

								$printIssue = "[".$i->key."|".$i->url->href."] ".$issueStatus." {color:#999999}(".$issueType."){color}: ".$issueSummary;
								echo "\n              ".$printIssue;

								//Populate the data
								switch (strtolower($issueType)){
									case "story":	
									case "new feature":
									case "improvement":
									case "epic":			$headlines[] = $printIssue;
															break;
									case "bug":				$left_column[] = $printIssue;
															break;
									default:				$right_column[] = $printIssue;
															break;
								}
							}
						}else{
							echo "\n              No issues committed against for this build.";
						}
						
					}
					
			}			
			echo "\n\n	 [INFO] ------ END BUILD RESULTS ------ ";
			echo "\n [INFO] Generating release notes wiki markup... ";			
			echo "\n [INFO] ------ START RELEASE NOTES ------ \n";
			$end_build = defined("END_BUILD") ? END_BUILD : (START_FROM_BUILD+$build_difference);
			generateReleaseNotes(BAMBOO_PROJECTKEY."-".BAMBOO_PLANKEY, START_FROM_BUILD, $end_build, $headlines, $left_column, $right_column);
			echo "\n\n [INFO] ------ END RELEASE NOTES ------";
			echo "\n [INFO] Done! \n";
		}
	}

	
	//Generate wiki markup for release notes
	function generateReleaseNotes($project, $start_build, $end_build, $headlines, $left_column, $right_column){
		$h = array_unique($headlines);
		$l = array_unique($left_column);
		$r = array_unique($right_column);
		echo "\n".$project." Release Notes - Build ".+$start_build."-".$end_build." - DRAFT";
		echo "\n";
		echo "\nh3. New Features and Improvements";
		foreach ($h as $feature){
			echo "\n * $feature";
		}
		echo "\n \\\\";	
		echo "\n";
		echo "\n{section}";
		echo "\n{column:width=50%}";
		echo "\nh3.Bug Fixes";
		foreach ($l as $bug){
			echo "\n * $bug";
		}
		echo "\n{column}";
		echo "\n{column:width=50%}";
		echo "\nh3.Other Issues";
		foreach ($r as $task){
			echo "\n * $task";
		}		
		echo "\n{column}";
		echo "\n{section}";
		
	}
	
	// Performs an authenticated Bamboo request, parsing the JSON response.
	function fetch($uri, $url_args=null, $post_args=null) {
		if (!$url_args) $url_args = array();

		$pairs = array();
		foreach ($url_args as $name => $value) {
	    	$pairs[] = $name . "=" . urlencode($value);
		}

		$url = BAMBOO.$uri.OUTPUT_FORMAT."?".join("&", $pairs);
		
		//echo "\n [DEBUG] Calling ".$url."...";
		
		$curl = curl_init(BAMBOO);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_USERPWD, BAMBOO_USER.":".BAMBOO_PASSWORD);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		if ($post_args) {
	    	curl_setopt($curl, CURLOPT_POST, 1);
	    	curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);
		}
		$response = curl_exec($curl);
		$info = curl_getinfo($curl);
		curl_close($curl);

		if ($info["http_code"] != 200) {
			echo "\n [ERROR] Response code was not 200 ...";
			var_dump($response);
			echo "\n ";
	    	return null;
		}
		return json_decode($response);
	}
	
?>