Skip to content

Commit

Permalink
Cleaned up/removed print statements. Added the functionality to only …
Browse files Browse the repository at this point in the history
…create one jira version if multiple tickets are referenced in the release. Updated method names to represent what they actually do.
  • Loading branch information
Jeff Wysong committed Apr 14, 2016
1 parent c17b613 commit 0c484ab
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 113 deletions.
8 changes: 8 additions & 0 deletions agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<properties>
<jackson.version>2.6.5</jackson.version>
<jira.restapi.version>1.0</jira.restapi.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<teamcity.version>9.0.3</teamcity.version>
</properties>

Expand All @@ -58,6 +60,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jetbrains.teamcity</groupId>
<artifactId>common-api</artifactId>
<version>${teamcity.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
package com.amirov.jirareporter;

import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.*;
import jetbrains.buildServer.agent.AgentRunningBuild;
import jetbrains.buildServer.agent.BuildFinishedStatus;
import jetbrains.buildServer.agent.BuildProcess;
import jetbrains.buildServer.agent.BuildProgressLogger;
import jetbrains.buildServer.agent.BuildRunnerContext;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
Expand Down Expand Up @@ -42,15 +46,13 @@ public void start() throws RunBuildException {
RunnerParamsProvider.setProperty("buildName", myContext.getBuild().getBuildTypeName());
if(issueId == null || issueId.isEmpty()){
logger.message("Issue is not related");
}
else {
} else {
if(issueId.contains(",")){
for(String issue : issueId.split(",")){
reporter.report(issue);
reporter.progressIssue();
}
}
else {
} else {
reporter.report(issueId);
reporter.progressIssue();
}
Expand Down
102 changes: 19 additions & 83 deletions agent/src/main/java/com/amirov/jirareporter/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@
import com.amirov.jirareporter.teamcity.TeamCityXMLParser;
import com.atlassian.jira.rest.client.NullProgressMonitor;
import com.atlassian.jira.rest.client.domain.Comment;
import com.atlassian.jira.rest.client.domain.Field;
import com.atlassian.jira.rest.client.domain.Issue;
import com.atlassian.jira.rest.client.domain.Resolution;
import com.atlassian.jira.rest.client.domain.Transition;
import com.atlassian.jira.rest.client.domain.Version;
import com.atlassian.jira.rest.client.domain.input.ComplexIssueInputFieldValue;
import com.atlassian.jira.rest.client.domain.input.FieldInput;
import com.atlassian.jira.rest.client.domain.input.TransitionInput;
import com.atlassian.jira.rest.client.domain.input.VersionInput;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jetbrains.buildServer.agent.BuildProgressLogger;
import org.joda.time.DateTime;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -30,7 +25,7 @@
import static com.amirov.jirareporter.jira.JIRAClient.*;

public class Reporter {
private static String issueId;
private static String issueKey;
private BuildProgressLogger myLogger;
private TeamCityXMLParser parser = new TeamCityXMLParser();
private static final ObjectMapper mapper = new ObjectMapper();
Expand All @@ -40,117 +35,58 @@ public Reporter(BuildProgressLogger logger){
myLogger = logger;
}

public void report(String issue){
issueId = issue;
myLogger.message("\nISSUE: " + issue
public void report(String issueKeyString){
issueKey = issueKeyString;
myLogger.message("\nISSUE: " + issueKeyString
+ "\nTitle: " + getIssue().getSummary()
+ "\nDescription: " + getIssue().getDescription());
NullProgressMonitor pm = new NullProgressMonitor();
getRestClient().getIssueClient().addComment(pm, getIssue().getCommentsUri(), Comment.valueOf(parser.getTestResultText()));
}

public void progressIssue(){
String releasedVersion = parser.getReleasedPomVersionString();
public void progressIssue() {
NullProgressMonitor pm = new NullProgressMonitor();
if(RunnerParamsProvider.progressIssueIsEnable() == null){}
else if(RunnerParamsProvider.progressIssueIsEnable().equals("true")){
String transitionName = JIRAConfig.prepareJiraWorkflow(parser.getStatusBuild()).get(getIssueStatus());
if (transitionName != null) {
System.out.println("transitionName = " + transitionName);
//create new version
Issue issue = getIssue();
DateTime dateTime = new DateTime();
VersionInput versionInput = new VersionInput(issue.getProject().getKey(), releasedVersion, null, dateTime, false, true);
Version version = getRestClient().getVersionRestClient().createVersion(versionInput, pm);

System.out.println("********* ISSUE FIELDS ************");
for (Field field : issue.getFields()) {
System.out.println(field.toString());
}
System.out.println("********* END ISSUE FIELDS ************");

//Get Transition
Transition transition = getTransitionByName(transitionName);
System.out.println("Got the transition!!! " + transition.toString());
System.out.println("Transition id = " + transition.getId());

System.out.println("********* TRANSITION FIELDS ************");
for (Transition.Field field : transition.getFields()) {
System.out.println(field.toString());
}
System.out.println("********* END TRANSITION FIELDS ************");


System.out.println("********* RESOLUTIONS ************");
for (Resolution resolution : getRestClient().getMetadataClient().getResolutions(pm)) {
System.out.println(resolution.toString());
}
System.out.println("********* END RESOLUTIONS ************");

/*
Field trigger = issue.getFieldByName("Trigger");
if (trigger != null) {
newIssue.setFieldValue(trigger.getId(), trigger);
}
IssueField trigger = issue.getFieldByName("Trigger");
if (trigger != null) {
JSONObject triggerJO = (JSONObject) trigger.getValue();
newIssue.setFieldValue(trigger.getId(), ComplexIssueInputFieldValue.with("value", triggerJO.get("value")));
}
*/
RunnerParamsProvider.printProperties();



//Create New Field Input Updates
Resolution fixedResolution = getResolutionByName("Fixed");
String resolutionString = null;
Map resMap = Collections.EMPTY_MAP;
Map versionMap = new HashMap();
versionMap.put("id", String.valueOf(version.getId()));
versionMap.put("name", version.getName());
try {
resolutionString = mapper.writeValueAsString(fixedResolution);
System.out.println("********* RESOLUTION STRING ************");
System.out.println(resolutionString);
System.out.println("********* END RESOLUTION STRING ************");
String resolutionString = mapper.writeValueAsString(fixedResolution);
resMap = mapper.readValue(resolutionString, HashMap.class);
// versionMap = mapper.readValue(mapper.writeValueAsString(version), HashMap.class);

} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

List<Map> fixVersions = new ArrayList<Map>();
fixVersions.add(resMap);
// JSONObject resolutionJO = new JSONObject(fixedResolution, new String[]{"resolution"});
// ComplexIssueInputFieldValue complexIssueInputFieldValue = ComplexIssueInputFieldValue.with("resolution", "{\"self\"=\"http://localhost:2990/jira/rest/api/2/resolution/10100\", \"name\"=\"Fixed\", \"description\"=\"A fix for this issue is checked into the tree and tested.\"}");
// ComplexIssueInputFieldValue complexIssueInputFieldValue = ComplexIssueInputFieldValue.with("resolution", resolutionString);
// Map map = new HashMap();
// map.put("name", "Fixed");
ComplexIssueInputFieldValue complexIssueInputFieldValue = new ComplexIssueInputFieldValue(resMap);

//Get Jira Version
Map<String, Object> versionMap = new HashMap<>();
Version version = getVersion(parser.getReleasedPomVersionString());
versionMap.put("id", String.valueOf(version.getId()));
versionMap.put("name", version.getName());
ComplexIssueInputFieldValue versionComplexIssueInputFieldValue = new ComplexIssueInputFieldValue(versionMap);

List<ComplexIssueInputFieldValue> fixVersionsComplex = new ArrayList<ComplexIssueInputFieldValue>();
List<ComplexIssueInputFieldValue> fixVersionsComplex = new ArrayList<>();
fixVersionsComplex.add(versionComplexIssueInputFieldValue);

// Collection<FieldInput> fieldInputs = Arrays.asList(new FieldInput("resolution", resolutionString), new FieldInput("fixVersions", fixVersions));
Collection<FieldInput> fieldInputs = Arrays.asList(new FieldInput("resolution", complexIssueInputFieldValue), new FieldInput("fixVersions", fixVersionsComplex));
// Collection<FieldInput> fieldInputs = Arrays.asList(new FieldInput("fixVersions", fixVersions));
//Create final transition to ship across the wire.
//Create final transition input to ship across the wire.
final TransitionInput resolvedTransitionInput = new TransitionInput(transition.getId(), fieldInputs, Comment.valueOf("This issue was released and closed via TeamCity Plugin."));
//SHIP IT!!!
getRestClient().getIssueClient().transition(issue.getTransitionsUri(), resolvedTransitionInput, pm);
getRestClient().getIssueClient().transition(getIssue().getTransitionsUri(), resolvedTransitionInput, pm);

myLogger.message(issueKey + " has been transitioned to Closed with resolution Fixed and Fix Version of " + version.getName());
}
}
}

public static String getIssueId(){
return issueId;
public static String getIssueKey(){
return issueKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static String getIssueId(){
String issueIdPlace = getIssueIdPlace();
if (issueIdPlace.equals("teamcity")) {
TeamCityXMLParser parser = new TeamCityXMLParser();
String issueTC = parser.getIssue();
String issueTC = parser.getIssueKey();
setProperty("issueId", issueTC);
issueId = issueTC;

Expand Down
34 changes: 30 additions & 4 deletions agent/src/main/java/com/amirov/jirareporter/jira/JIRAClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
import com.atlassian.jira.rest.client.domain.Issue;
import com.atlassian.jira.rest.client.domain.Resolution;
import com.atlassian.jira.rest.client.domain.Transition;
import com.atlassian.jira.rest.client.domain.Version;
import com.atlassian.jira.rest.client.domain.input.TransitionInput;
import com.atlassian.jira.rest.client.domain.input.VersionInput;
import com.atlassian.jira.rest.client.domain.input.VersionInputBuilder;
import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;
import org.joda.time.DateTime;

import java.net.URI;
import java.net.URISyntaxException;

public class JIRAClient {
private static final NullProgressMonitor pm = new NullProgressMonitor();

public static JiraRestClient getRestClient() {
System.setProperty("jsse.enableSNIExtension", RunnerParamsProvider.sslConnectionIsEnabled());
Expand All @@ -30,10 +35,9 @@ public static JiraRestClient getRestClient() {
}

public static Issue getIssue() {
NullProgressMonitor pm = new NullProgressMonitor();
Issue issue = null;
try {
issue = getRestClient().getIssueClient().getIssue(Reporter.getIssueId(), pm);
issue = getRestClient().getIssueClient().getIssue(Reporter.getIssueKey(), pm);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -45,7 +49,7 @@ public static String getIssueStatus(){
}

private static Iterable<Transition> getTransitions (){
return getRestClient().getIssueClient().getTransitions(getIssue().getTransitionsUri(), new NullProgressMonitor());
return getRestClient().getIssueClient().getTransitions(getIssue().getTransitionsUri(), pm);
}

private static Transition getTransition(String transitionName){
Expand All @@ -72,7 +76,7 @@ public static Transition getTransitionByName(String transitionName) {
}

private static Iterable<Resolution> getResolutions() {
return getRestClient().getMetadataClient().getResolutions(new NullProgressMonitor());
return getRestClient().getMetadataClient().getResolutions(pm);
}

public static Resolution getResolutionByName(String resolutionName) {
Expand All @@ -84,4 +88,26 @@ public static Resolution getResolutionByName(String resolutionName) {
}
return null;
}

private static Version createVersion(String versionName, String projectKey) {
//create new version
VersionInput versionInput = new VersionInputBuilder(projectKey)
.setName(versionName)
.setReleaseDate(new DateTime())
.setReleased(true)
.build();
return getRestClient().getVersionRestClient().createVersion(versionInput, pm);
}

public static Version getVersion(String versionName) {
String projectKey = getIssue().getProject().getKey();
Iterable<Version> versions = getRestClient().getProjectClient().getProject(projectKey, pm).getVersions();
for (Version version : versions) {
if (version.getName().equals(versionName)) {
return version;
}
}
return createVersion(versionName, projectKey);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.google.common.collect.ImmutableMap;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import sun.misc.BASE64Encoder;
Expand Down Expand Up @@ -42,7 +41,6 @@ private NodeList getNodeList(String xmlUrl, String tag) {
Document doc = db.parse(new InputSource(uc.getInputStream()));
doc.getDocumentElement().normalize();
return doc.getElementsByTagName(tag);
// return nodeList.item(num);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -55,43 +53,27 @@ private NamedNodeMap parseXML(String xmlUrl, String tag){
}

private String getBuildAttribute(String attribute){
// System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^");
// printNodeMap(buildData);
// System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^");
return buildData.getNamedItem(attribute).getNodeValue();

}

private void printNodeMap(NamedNodeMap nodeMap) {
int length = nodeMap.getLength();
for(int i=0; i<length;i++) {
Node item = nodeMap.item(i);
System.out.println(item.getNodeValue());
System.out.println("-------------");
}
}

public String getReleasedPomVersionString() {
String urlString = SERVER_URL + "/httpAuth/app/rest/builds/id:" + getBuildId() + "/artifacts/content/pomVersion.txt";

System.out.println("Build Param Endpoint = " + urlString);

try {
URL url = new URL(urlString);
String encoding = new BASE64Encoder().encode(userPassword.getBytes());
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization","Basic " + encoding);
uc.connect();
String releasedPomVersion = mapper.readValue(uc.getInputStream(), String.class);
System.out.println("releasedPomVersion = " + releasedPomVersion);
return releasedPomVersion;
return mapper.readValue(uc.getInputStream(), String.class);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public String getIssue(){
public String getIssueKey(){
StringBuilder sb = new StringBuilder();
try{
NodeList issueList = getNodeList(SERVER_URL +"/httpAuth/app/rest/builds/id:"+getBuildId()+"/relatedIssues", "issue");
Expand Down

0 comments on commit 0c484ab

Please sign in to comment.