|
| 1 | +package util; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.UnsupportedEncodingException; |
| 6 | +import java.net.URISyntaxException; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Paths; |
| 10 | + |
| 11 | +import javax.xml.bind.DatatypeConverter; |
| 12 | + |
| 13 | +import org.apache.http.client.ClientProtocolException; |
| 14 | +import org.json.JSONObject; |
| 15 | +import org.springframework.http.HttpStatus; |
| 16 | +import org.springframework.http.MediaType; |
| 17 | +import org.springframework.http.ResponseEntity; |
| 18 | + |
| 19 | +import com.ericsson.ei.utils.HttpRequest; |
| 20 | +import com.ericsson.ei.utils.HttpRequest.HttpMethod; |
| 21 | + |
| 22 | +public class JenkinsManager { |
| 23 | + |
| 24 | + private static final String JENKINS_JOB_TEMPLATE_FILE_PATH = String.join(File.separator, "src", "integrationtests", |
| 25 | + "resources", "jenkinsJobTemplate.xml"); |
| 26 | + private static final String DEFAULT_JOB_TOKEN = "test"; |
| 27 | + private static final String DEFAULT_SCRIPT = "echo "Test""; |
| 28 | + private static final String DEFAULT_JOB_NAME = "test_job"; |
| 29 | + |
| 30 | + private String host; |
| 31 | + private int port; |
| 32 | + private String encoding; |
| 33 | + private String crumb; |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructor, takes jenkins host, port, username and password. |
| 37 | + * @param host |
| 38 | + * @param port |
| 39 | + * @param username |
| 40 | + * @param password ** Jenkins password or API-token |
| 41 | + * @throws UnsupportedEncodingException |
| 42 | + * @throws URISyntaxException |
| 43 | + */ |
| 44 | + public JenkinsManager(String host, int port, String username, String password) |
| 45 | + throws UnsupportedEncodingException, URISyntaxException { |
| 46 | + String authString = String.join(":", username, password); |
| 47 | + |
| 48 | + this.host = host; |
| 49 | + this.port = port; |
| 50 | + this.encoding = DatatypeConverter.printBase64Binary(authString.getBytes("utf-8")); |
| 51 | + this.crumb = getCrumb(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Takes a token and script and returns XML data for jenkins with job_token and |
| 56 | + * script added. |
| 57 | + * |
| 58 | + * @param job_token |
| 59 | + * @param script |
| 60 | + * @return |
| 61 | + * @throws IOException |
| 62 | + */ |
| 63 | + public String getXmlJobData(String job_token, String script) throws IOException { |
| 64 | + String jobData = getStringFromFile(JENKINS_JOB_TEMPLATE_FILE_PATH); |
| 65 | + |
| 66 | + if (job_token == null) { |
| 67 | + job_token = DEFAULT_JOB_TOKEN; |
| 68 | + } |
| 69 | + if (script == null) { |
| 70 | + script = DEFAULT_SCRIPT; |
| 71 | + } |
| 72 | + |
| 73 | + jobData = injectScriptAndTokenIntoXmlString(jobData, job_token, script); |
| 74 | + return jobData; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates a jenkins job with a given name using the XML data as input for |
| 79 | + * job configuration |
| 80 | + * |
| 81 | + * @param jobName |
| 82 | + * @param jobXmlData |
| 83 | + * @return |
| 84 | + * @throws URISyntaxException |
| 85 | + * @throws ClientProtocolException |
| 86 | + * @throws IOException |
| 87 | + */ |
| 88 | + public boolean createJob(String jobName, String jobXmlData) throws URISyntaxException { |
| 89 | + HttpRequest httpRequest = new HttpRequest(HttpMethod.POST); |
| 90 | + boolean success = false; |
| 91 | + |
| 92 | + if (jobName == null) { |
| 93 | + jobName = DEFAULT_JOB_NAME; |
| 94 | + } |
| 95 | + |
| 96 | + httpRequest.setHost(host).setPort(port).addHeader("Authorization", "Basic " + encoding) |
| 97 | + .addHeader("Content-type", MediaType.APPLICATION_XML_VALUE).addHeader("Jenkins-Crumb", crumb) |
| 98 | + .addParam("name", jobName).setBody(jobXmlData).setEndpoint("/createItem"); |
| 99 | + |
| 100 | + ResponseEntity<String> response = httpRequest.performRequest(); |
| 101 | + success = response.getStatusCode() == HttpStatus.OK; |
| 102 | + |
| 103 | + return success; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * This function recieves a jenkins job name and are used to trigger the job |
| 108 | + * |
| 109 | + * @throws URISyntaxException |
| 110 | + */ |
| 111 | + public boolean triggerJob(String jobName, String job_token) throws URISyntaxException { |
| 112 | + HttpRequest httpRequest = new HttpRequest(HttpMethod.GET); |
| 113 | + boolean success = false; |
| 114 | + |
| 115 | + if (jobName == null) { |
| 116 | + jobName = DEFAULT_JOB_NAME; |
| 117 | + } |
| 118 | + if (job_token == null) { |
| 119 | + job_token = DEFAULT_JOB_TOKEN; |
| 120 | + } |
| 121 | + |
| 122 | + String endpoint = "/job/" + jobName + "/build"; |
| 123 | + |
| 124 | + httpRequest.setHost(host).setPort(port).addHeader("Authorization", "Basic " + encoding) |
| 125 | + .addHeader("Content-type", MediaType.APPLICATION_JSON_VALUE).addParam("token", job_token) |
| 126 | + .setEndpoint(endpoint); |
| 127 | + |
| 128 | + ResponseEntity<String> response = httpRequest.performRequest(); |
| 129 | + success = response.getStatusCode() == HttpStatus.CREATED; |
| 130 | + return success; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * This function recieves a jenkins job name and returns whether the job has |
| 135 | + * been triggered at least once |
| 136 | + * |
| 137 | + * @param jobName |
| 138 | + * @return |
| 139 | + * @throws URISyntaxException |
| 140 | + */ |
| 141 | + public boolean jobHasBeenTriggered(String jobName) throws URISyntaxException { |
| 142 | + boolean isTriggered = false; |
| 143 | + HttpRequest httpRequest = new HttpRequest(HttpMethod.GET); |
| 144 | + if (jobName == null) { |
| 145 | + jobName = DEFAULT_JOB_NAME; |
| 146 | + } |
| 147 | + |
| 148 | + String endpoint = "/job/" + jobName + "/1/api/json"; |
| 149 | + |
| 150 | + httpRequest.setHost(host).setPort(port).addHeader("Authorization", "Basic " + encoding) |
| 151 | + .addHeader("Content-type", MediaType.APPLICATION_JSON_VALUE) |
| 152 | + .setEndpoint(endpoint); |
| 153 | + |
| 154 | + ResponseEntity<String> response = httpRequest.performRequest(); |
| 155 | + isTriggered = response.getStatusCode() == HttpStatus.OK; |
| 156 | + return isTriggered; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * |
| 161 | + * This function recieves a jenkins job name and deletes that job from the |
| 162 | + * jenkins system |
| 163 | + * |
| 164 | + * @param jobName |
| 165 | + * @return |
| 166 | + * @throws URISyntaxException |
| 167 | + */ |
| 168 | + public boolean deleteJob(String jobName) throws URISyntaxException { |
| 169 | + boolean isDeleted = false; |
| 170 | + HttpRequest httpRequest = new HttpRequest(HttpMethod.POST); |
| 171 | + if (jobName == null) { |
| 172 | + jobName = DEFAULT_JOB_NAME; |
| 173 | + } |
| 174 | + |
| 175 | + String endpoint = "/job/" + jobName + "/doDelete"; |
| 176 | + |
| 177 | + httpRequest.setHost(host).setPort(port).addHeader("Authorization", "Basic " + encoding) |
| 178 | + .addHeader("Content-type", MediaType.APPLICATION_JSON_VALUE).addHeader("Jenkins-Crumb", crumb) |
| 179 | + .setEndpoint(endpoint); |
| 180 | + |
| 181 | + ResponseEntity<String> response = httpRequest.performRequest(); |
| 182 | + isDeleted = response.getStatusCode() == HttpStatus.FOUND; |
| 183 | + return isDeleted; |
| 184 | + } |
| 185 | + |
| 186 | + private String getCrumb() throws URISyntaxException { |
| 187 | + String crumb = ""; |
| 188 | + HttpRequest httpRequest = new HttpRequest(HttpMethod.GET); |
| 189 | + |
| 190 | + httpRequest.setHost(host).setPort(port).addHeader("Authorization", "Basic " + encoding) |
| 191 | + .addHeader("Content-type", MediaType.APPLICATION_JSON_VALUE).setEndpoint("/crumbIssuer/api/json"); |
| 192 | + |
| 193 | + ResponseEntity<String> response = httpRequest.performRequest(); |
| 194 | + boolean success = response.getStatusCode() == HttpStatus.OK; |
| 195 | + |
| 196 | + if (success) { |
| 197 | + JSONObject jsonObj = new JSONObject(response.getBody()); |
| 198 | + crumb = jsonObj.getString("crumb"); |
| 199 | + } |
| 200 | + return crumb; |
| 201 | + } |
| 202 | + |
| 203 | + private static String getStringFromFile(String filepath) throws IOException { |
| 204 | + return new String(Files.readAllBytes(Paths.get(filepath)), StandardCharsets.UTF_8); |
| 205 | + } |
| 206 | + |
| 207 | + private String injectScriptAndTokenIntoXmlString(String xmlString, String token, String script) { |
| 208 | + xmlString = xmlString.replaceAll("\\$\\{shell\\.script\\}", script); |
| 209 | + xmlString = xmlString.replaceAll("\\$\\{auth\\.token\\}", token); |
| 210 | + return xmlString; |
| 211 | + } |
| 212 | + |
| 213 | +} |
0 commit comments