|
20 | 20 |
|
21 | 21 | package com.microfocus.application.automation.tools.octane.events; |
22 | 22 |
|
| 23 | +import com.cloudbees.hudson.plugins.folder.Folder; |
23 | 24 | import com.hp.octane.integrations.OctaneSDK; |
24 | 25 | import com.hp.octane.integrations.dto.DTOFactory; |
25 | 26 | import com.hp.octane.integrations.dto.events.CIEvent; |
26 | 27 | import com.hp.octane.integrations.dto.events.CIEventType; |
| 28 | +import com.hp.octane.integrations.dto.events.ItemType; |
| 29 | +import com.microfocus.application.automation.tools.octane.CIJenkinsServicesImpl; |
27 | 30 | import com.microfocus.application.automation.tools.octane.configuration.ConfigurationService; |
28 | 31 | import com.microfocus.application.automation.tools.octane.configuration.SDKBasedLoggerProvider; |
29 | 32 | import com.microfocus.application.automation.tools.octane.executor.UftTestDiscoveryDispatcher; |
30 | 33 | import com.microfocus.application.automation.tools.octane.model.processors.projects.JobProcessorFactory; |
| 34 | +import com.microfocus.application.automation.tools.octane.tests.build.BuildHandlerUtils; |
31 | 35 | import com.microfocus.application.automation.tools.settings.OctaneServerSettingsBuilder; |
32 | 36 | import hudson.Extension; |
33 | 37 | import hudson.model.Item; |
| 38 | +import hudson.model.Job; |
34 | 39 | import hudson.model.listeners.ItemListener; |
35 | 40 | import jenkins.model.Jenkins; |
36 | 41 | import org.apache.logging.log4j.Logger; |
@@ -84,4 +89,59 @@ public void onBeforeShutdown() { |
84 | 89 | UftTestDiscoveryDispatcher dispatcher = Jenkins.get().getExtensionList(UftTestDiscoveryDispatcher.class).get(0); |
85 | 90 | dispatcher.close(); |
86 | 91 | } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onRenamed(Item item, String oldName, String newName) { |
| 95 | + if(!OctaneSDK.hasClients()){ |
| 96 | + return; |
| 97 | + } |
| 98 | + logger.info("Renaming Job " + oldName + " to " + item.getFullName()); |
| 99 | + |
| 100 | + try { |
| 101 | + CIEvent event = dtoFactory.newDTO(CIEvent.class) |
| 102 | + .setEventType(CIEventType.RENAMED); |
| 103 | + String project; |
| 104 | + if(isJob(item)) { |
| 105 | + project = JobProcessorFactory.getFlowProcessor((Job) item).getTranslatedJobName(); |
| 106 | + event.setItemType(ItemType.JOB); |
| 107 | + } else if (isMultibranch(item)) { |
| 108 | + project = BuildHandlerUtils.translateFolderJobName(item.getFullName()); |
| 109 | + event.setItemType(ItemType.MULTI_BRANCH); |
| 110 | + } else if(isFolder(item)) { |
| 111 | + project = BuildHandlerUtils.translateFolderJobName(item.getFullName()); |
| 112 | + event.setItemType(ItemType.FOLDER); |
| 113 | + } else { |
| 114 | + logger.info("Cannot handle rename for " + item.getClass().getName()); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + String previousProject = getPreviousProject(oldName, newName, project); |
| 119 | + |
| 120 | + event.setProject(project) |
| 121 | + .setProjectDisplayName(newName) |
| 122 | + .setPreviousProject(previousProject) |
| 123 | + .setPreviousProjectDisplayName(oldName); |
| 124 | + |
| 125 | + CIJenkinsServicesImpl.publishEventToRelevantClients(event); |
| 126 | + } catch (Throwable throwable) { |
| 127 | + logger.error("failed to build and/or dispatch RENAMED event for " + item, throwable); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private String getPreviousProject(String oldName, String newName, String project) { |
| 132 | + String jobPath = project.substring(0, project.lastIndexOf(newName)); |
| 133 | + return jobPath + oldName; |
| 134 | + } |
| 135 | + |
| 136 | + private boolean isFolder(Item item){ |
| 137 | + return item.getClass().getName().equals(JobProcessorFactory.FOLDER_JOB_NAME) && item instanceof Folder; |
| 138 | + } |
| 139 | + |
| 140 | + private boolean isMultibranch(Item item){ |
| 141 | + return item.getClass().getName().equalsIgnoreCase(JobProcessorFactory.WORKFLOW_MULTI_BRANCH_JOB_NAME); |
| 142 | + } |
| 143 | + |
| 144 | + private boolean isJob(Item item){ |
| 145 | + return item instanceof Job; |
| 146 | + } |
87 | 147 | } |
0 commit comments