jira-trigger-jenkins

JIra 状态发生变化时通知Jenkins进行动作执行


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.event.issue.IssueEvent
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.JobWithDetails;

import org.ofbiz.core.entity.GenericValue;

import java.util.Map;
import java.util.Set;
import java.net.URI;
import java.util.HashMap;


def triggerTask(String jobName, Map<String, String> jobParams){
  // URI uri = new URI('http://galaxy.localhost.com/jenkins');
  // String username = 'galaxy';
  // String token = '114a89b4aabb621aeed20fc33a6366bc5f';
  
  URI uri = new URI('http://127.0.0.1:8090/jenkins');
  String username = 'galaxy';
  String token = '11ae2c9ff872d83e8c61d8abc64c5fc655';
  
  JenkinsServer jenkinsServer = new JenkinsServer(uri, username, token);
  log.error("jobParams {}", jobParams);
  try{
    JobWithDetails jobWithDetails = jenkinsServer.getJob(jobName);
    jobWithDetails.build(jobParams);
    log.trace('build success')
  }catch (IOException ioe){
    log.error(ioe.message);
  }
}

IssueEvent issueEvent = event;
Map<String, String> jobParams = new HashMap<>();
jobParams.put('Type', 'auto trigger by jira');

List<HashMap<String, Object>> fieldsModified = (List<HashMap<String, Object>>)event.getChangeLog()?.getRelated('ChildChangeItem');
CommentManager commentManager = ComponentAccessor.commentManager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

Issue issue = issueEvent.getIssue();

String issueId = issue.getId();
jobParams.put('IssueId', issueId);

// GitModule
CustomField GitModuleField = customFieldManager.getCustomFieldObject('customfield_10305');
String GitModule = GitModuleField.getValue(issue);
jobParams.put('GitModule', GitModule);

// GitTag
CustomField GitTagField = customFieldManager.getCustomFieldObject('customfield_10306');
String gitTag = GitTagField.getValue(issue);
jobParams.put('GitTag', gitTag);

// GitCommitId
CustomField GitCommitIdField = customFieldManager.getCustomFieldObject('customfield_10307');
String gitCommitId = GitCommitIdField.getValue(issue)
jobParams.put('GitCommitId', gitCommitId);

// moduleType
CustomField moduleTypeField = customFieldManager.getCustomFieldObject('customfield_10700');
String moduleType = moduleTypeField.getValue(issue)
jobParams.put('ModuleType', moduleType);


for (HashMap<String, Object> field : fieldsModified){
  if(field['newvalue'] == '10100' && field['fieldtype'] == 'jira'){ // 触发编译任务
    commentManager.create(event.issue, event.getUser(), "触发编译任务: ${field}", true)
    triggerTask('development/build-task', jobParams)
  }
  if(field['newvalue'] == '10101' && field['fieldtype'] == 'jira'){ // 触发测试环境部署任务
    commentManager.create(event.issue, event.getUser(), "触发测试环境部署任务: ${field}", true)
    triggerTask('development/test-deploy', jobParams)
  }
  if(field['newvalue'] == '10102' && field['fieldtype'] == 'jira'){ // 触发预览环境部署任务
    commentManager.create(event.issue, event.getUser(), "触发预览环境部署任务: ${field}", true)
    triggerTask('development/pre-deploy', jobParams)
  }
  if(field['newvalue'] == '10404' && field['fieldtype'] == 'jira'){ // 触发生产环境部署任务
    commentManager.create(event.issue, event.getUser(), "触发生产环境部署任务: ${field}", true)
    triggerTask('development/prod-deploy', jobParams)
  } 
}

 

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐