- Необходимо импортировать сертификат в Java KeyStore перед тем как отправлять запросы
- Basic authentication (не забудьте указать свой логин и пароль JIRA в коде примера)
- Для поработы с groovy в maven-проекте использован gmavenplus-plugin
Created
April 26, 2018 19:41
-
-
Save nickname55/89f58d3de0d0cdddda9b89b85b3f8cf1 to your computer and use it in GitHub Desktop.
Groovy HTTP client for JIRA REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pampushko.jira.rest.groovy | |
/** | |
* You might need to import JIRA ssl certificate into your jdk/jre cacerts file: | |
* 1) save JIRA certificate. E.g. in Chrome right click on https icon, click "Certificate information" link. | |
* In "Details" tab, click "Copy to File..." button. | |
* 2) in jdk "bin" folder run: "keytool -importcert -keystore ./cacerts -file /file/from/the/step/above/cacert.crt -trustcacerts -alias jira_ca | |
*/ | |
class JiraRestClientExample { | |
static void main(String[] args) { | |
// see https://docs.atlassian.com/jira/REST/server/ | |
// версия нашей JIRA -6.4.14, её документация - https://docs.atlassian.com/software/jira/docs/api/REST/6.4.13/ | |
def yourProjectIssues = "https://jira.np.ua/rest/api/2/search?jql=project=SDP&maxResults=1000" | |
def createIssue = "https://jira.np.ua/rest/api/2/issue" | |
println(get(yourProjectIssues)) | |
println(post(createIssue, createIssueContent("Have some cake", 777777))) | |
} | |
private | |
static createIssueContent(String summary, long customField_NaumenId = 0, String description = "I love JIRA", List<String> labels = []) { | |
""" | |
{ | |
"fields": { | |
"project": { "id": "${projectId}" }, | |
"summary": "${summary}", | |
"issuetype": { "id": "${taskIssueType}" }, | |
"description": "${description}", | |
"customfield_14307" : ${customField_NaumenId} | |
} | |
} | |
""" | |
} | |
private static get(String url) { | |
def connection = url.toURL().openConnection() | |
connection.addRequestProperty("Authorization", "Basic ${authString}") | |
connection.setRequestMethod("GET") | |
connection.doOutput = false | |
connection.connect() | |
connection.content.text | |
} | |
private static delete(String url) { | |
def connection = url.toURL().openConnection() | |
connection.addRequestProperty("Authorization", "Basic ${authString}") | |
connection.setRequestMethod("DELETE") | |
connection.doOutput = true | |
connection.connect() | |
connection.content.text | |
} | |
private static post(String url, String postContent) { | |
def connection = url.toURL().openConnection() | |
connection.addRequestProperty("Authorization", "Basic ${authString}") | |
connection.addRequestProperty("Content-Type", "application/json") | |
connection.setRequestMethod("POST") | |
connection.doOutput = true | |
connection.outputStream.withWriter { | |
it.write(postContent) | |
it.flush() | |
} | |
connection.connect() | |
try { | |
connection.content.text | |
} catch (IOException e) { | |
try { | |
((HttpURLConnection) connection).errorStream.text | |
} catch (Exception ignored) { | |
throw e | |
} | |
} | |
} | |
static { | |
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)) | |
} | |
//Идентификатор типа создаваемого нами запроса | |
//Тип запроса (issueTypeId) "Зміна функціоналу" имеет id 10400 | |
//Тип запроса (issueTypeId) "Оптимізація процесу" имеет id 10401 | |
private static final taskIssueType = 10400 | |
//Идентификатор проекта | |
//Проект SDP | |
private static final projectId = 14700 | |
//замените username и password на свои учетные данные разделенные двоеточием | |
private static final authString = "username:password".bytes.encodeBase64().toString() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- | |
Generated from archetype; please customize. | |
--> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>pampushko.jira.rest.groovy</groupId> | |
<artifactId>jira-groovy-rest-client</artifactId> | |
<name>Example Project</name> | |
<version>0.1-SNAPSHOT</version> | |
<properties> | |
<jdk.version>1.8</jdk.version> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<maven.compiler.target>1.8</maven.compiler.target> | |
<maven.compiler.source>1.8</maven.compiler.source> | |
<maven.compiler.testTarget>1.8</maven.compiler.testTarget> | |
<maven.compiler.testSource>1.8</maven.compiler.testSource> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.codehaus.groovy</groupId> | |
<artifactId>groovy-all</artifactId> | |
<!-- any version of Groovy \>= 1.5.0 should work here --> | |
<version>2.4.12</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.codehaus.gmavenplus</groupId> | |
<artifactId>gmavenplus-plugin</artifactId> | |
<version>1.6</version> | |
<executions> | |
<execution> | |
<goals> | |
<goal>addSources</goal> | |
<goal>addTestSources</goal> | |
<goal>compile</goal> | |
<goal>compileTests</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.7.0</version> | |
<configuration> | |
<source>${maven.compiler.source}</source> | |
<target>${maven.compiler.target}</target> | |
<encoding>UTF-8</encoding> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment