import java.util.Map;
import java.util.HashMap;
import com.google.gson.Gson;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
def issueManager=ComponentAccessor.getIssueManager();
def customFieldManager=ComponentAccessor.getCustomFieldManager();
def epicLinkField=customFieldManager.getCustomFieldObject(10005); //10005 - Epic Link
def dmfzField=customFieldManager.getCustomFieldObject(19200); //19200 - 代码分支
def user=ComponentAccessor.getUserManager().getUserByName("admin");
def issue=issueManager.getIssueObject("EPRODUCT-902");
// Get Configs
CloseableHttpClient httpClient1 = HttpClientBuilder.create().build();
HttpPost httpPost1 = new HttpPost("http://aiops.infinitus.com.cn/testsxapi/cicd/jirainfo");
httpPost1.addHeader("X-Atlassian-Token","nocheck");
httpPost1.setHeader(new BasicHeader("Content-Type", "application/json;charset=utf-8"));
//Compose json request
Map jsonMap1=new HashMap();
jsonMap1.put("authkey","jiracallaiops");
jsonMap1.put("project",issue.getProjectObject().getKey());
jsonMap1.put("component",issue.getComponents()*.getName());
Gson json=new Gson();
def str1=json.toJson(jsonMap1);
log.warn(str1);
StringEntity se1=new StringEntity(str1,"UTF-8");
httpPost1.setEntity(se1);
CloseableHttpResponse httpResponse1=httpClient1.execute(httpPost1);
HttpEntity responseEntity1=httpResponse1.getEntity();
// Create Branches
String BITBUCKET_URL="http://10.86.23.157:7990";
String branchId="";
// Story
if("10001".equals(issue.getIssueTypeId())){
def epic=issue.getCustomFieldValue(epicLinkField);
if(epic!=null){
branchId=epic.getKey();
} else {
return;
}
} else { //Prod-Bug, Enhancement
branchId=issue.getKey();
}
if(responseEntity1!=null){
String configResponse=EntityUtils.toString(responseEntity1,"UTF-8");
def result=json.fromJson(configResponse, Map.class);
if(result.code==0){
String dmfzVal="";
boolean newCreated=false;
result.data.each {repo ->
def reqUrl=BITBUCKET_URL+"/rest/api/1.0/projects/"+repo.gitprj+"/repos/"+repo.gitrepo+"/branches";
log.warn(reqUrl);
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(reqUrl);
httpPost.addHeader("Authorization","Basic MDAxMDk1ODpHYm1maXNoMyQ=");
httpPost.addHeader("X-Atlassian-Token","nocheck");
httpPost.setHeader(new BasicHeader("Content-Type", "application/json;charset=utf-8"));
//Compose json request
Map jsonMap=new HashMap();
jsonMap.put("name",repo.destbranch+"/"+branchId);
jsonMap.put("startPoint",repo.srcbranch);
//Gson json=new Gson();
def str=json.toJson(jsonMap);
log.warn(str);
StringEntity se=new StringEntity(str,"UTF-8");
httpPost.setEntity(se);
CloseableHttpResponse httpResponse=httpClient.execute(httpPost);
HttpEntity responseEntity=httpResponse.getEntity();
if(responseEntity!=null){
String content=EntityUtils.toString(responseEntity,"UTF-8");
if(!content.contains("errors") ||
content.contains("DuplicateRefException")){
newCreated=true;
//Compose access path
dmfzVal+=BITBUCKET_URL+"/projects/"+repo.gitprj+"/repos/"+repo.gitrepo+
"/browse?at="+repo.destbranch+"/"+branchId+"\n";
log.warn(content);
}
}
}
if(newCreated){
// Fill Story and sub-task with brach info
issue.setCustomFieldValue(dmfzField, dmfzVal);
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);
issue.getSubTaskObjects().each {subtask ->
if("5".equals(subtask.getIssueTypeId())){
subtask.setCustomFieldValue(dmfzField, dmfzVal);
issueManager.updateIssue(user, subtask, EventDispatchOption.ISSUE_UPDATED, false);
}
}
}
}
}
Overview
Content Tools