1+ <task xmlns =" http://midpoint.evolveum.com/xml/ns/public/common/common-3"
2+ xmlns : c =" http://midpoint.evolveum.com/xml/ns/public/common/common-3"
3+ xmlns : s =" http://midpoint.evolveum.com/xml/ns/public/model/scripting-3"
4+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
5+ oid =" e7aace37-0a2b-44ef-8e23-ebb3c7c5439d" >
6+ <name >Jira ticket creation task</name >
7+ <ownerRef oid =" 00000000-0000-0000-0000-000000000002" type =" UserType" />
8+ <executionState >suspended</executionState >
9+ <schedule >
10+ <recurrence >recurring</recurrence >
11+ <interval >3600</interval >
12+ <misfireAction >executeImmediately</misfireAction >
13+ </schedule >
14+ <activity >
15+ <work >
16+ <iterativeScripting >
17+ <objects >
18+ <type >UserType</type >
19+ </objects >
20+ <scriptExecutionRequest >
21+ <s : action >
22+ <s : type >execute-script</s : type >
23+ <s : parameter >
24+ <s : name >script</s : name >
25+ <c : value xsi : type =" c:ScriptExpressionEvaluatorType" >
26+ <c : code >
27+ import com.github.openjson.JSONObject
28+ import org.apache.commons.codec.binary.Base64
29+ import org.apache.http.HttpHeaders
30+ import org.apache.http.Consts
31+ import org.apache.http.HttpStatus
32+ import org.apache.http.client.config.RequestConfig
33+ import org.apache.http.client.methods.HttpPost
34+ import org.apache.http.conn.ConnectTimeoutException
35+ import org.apache.http.entity.StringEntity
36+ import org.apache.http.impl.client.CloseableHttpClient
37+ import org.apache.http.impl.client.HttpClientBuilder
38+ import org.apache.http.util.EntityUtils
39+
40+ import java.nio.charset.StandardCharsets
41+
42+ log.info("[ONBOARDING TASK] Enter task")
43+
44+ def hasOnboardingRole = false
45+ def hasEmptyTicketKey = false
46+
47+ def onboardingRole = '9822a7fe-7362-4d65-b383-b8018c335d37'
48+ for (role in input.getRoleMembershipRef()) {
49+ roleOid = role.getOid()
50+ if (onboardingRole == onboardingRole) {
51+ hasOnboardingRole = true
52+ break
53+ }
54+ }
55+
56+ def onboardingJiraTicket = basic.getPropertyValue(input, "extension/onboardingJiraTicket")
57+ hasEmptyTicketKey = onboardingJiraTicket == null || basic.isEmpty(onboardingJiraTicket)
58+
59+ if (hasOnboardingRole && hasEmptyTicketKey) {
60+ // ------------------------------------------------------------------------------
61+ // EMPLOYEE INFO //
62+ // ------------------------------------------------------------------------------
63+ def givenName = basic.getPropertyValue(input, "givenName")
64+ def familyName = basic.getPropertyValue(input, "familyName")
65+ def emailAddress = input.getEmailAddress()
66+
67+ // ------------------------------------------------------------------------------
68+ // CREDENTIALS VARS //
69+ // ------------------------------------------------------------------------------
70+
71+ def final BEARER_TOKEN = "Bearer 301bf498dd45d800842af0b84230f1bb58606c1" // fake
72+ // ------------------------------------------------------------------------------
73+ // HTTP CONNECTION VARS //
74+ // ------------------------------------------------------------------------------
75+
76+ def final URL = "https://test-sandbox-001.atlassian.net/rest/api/2/issue" // fake
77+ def final CONNECT_TIMEOUT = 5000
78+ def final REQUEST_TIMEOUT = 5000
79+ def final SOCKET_TIMEOUT = 5000
80+
81+ def config = RequestConfig.custom()
82+ .setConnectTimeout(CONNECT_TIMEOUT)
83+ .setConnectionRequestTimeout(REQUEST_TIMEOUT)
84+ .setSocketTimeout(SOCKET_TIMEOUT)
85+ .build()
86+
87+ // ------------------------------------------------------------------------------
88+ // REQUEST PREPARATION //
89+ // ------------------------------------------------------------------------------
90+
91+ def final ISSUE_TYPE = 10002
92+ def final PROJECT_ID = 10000
93+ def final SUMMARY = "Newcomer ${givenName} ${familyName}, ${emailAddress}"
94+
95+ def httpPost = new HttpPost(URL)
96+ httpPost.setHeader(HttpHeaders.ACCEPT, "application/json")
97+ httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
98+
99+ httpPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader)
100+ def ticketPayload = new JSONObject()
101+ ticketPayload.put(
102+ "fields",
103+ new JSONObject()
104+ .put("issuetype", new JSONObject().put("id", ISSUE_TYPE))
105+ .put("project", new JSONObject().put("id", PROJECT_ID))
106+ .put("summary", SUMMARY.toString())
107+ )
108+ httpPost.setEntity(new StringEntity(ticketPayload.toString(), Consts.UTF_8))
109+
110+ // ------------------------------------------------------------------------------
111+ // HTTP CLIENT PREPARATION //
112+ // ------------------------------------------------------------------------------
113+
114+ CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build()
115+
116+ // ------------------------------------------------------------------------------
117+ // HTTP REQUEST PROCESS //
118+ // ------------------------------------------------------------------------------
119+ try {
120+ log.info("[ONBOARDING TASK] Creating ticket for employee")
121+ def response = httpClient.execute(httpPost)
122+ try {
123+ def code = response.getStatusLine().getStatusCode()
124+ if (code == HttpStatus.SC_CREATED) {
125+ def entity = response.getEntity()
126+ def json = EntityUtils.toString(entity, StandardCharsets.UTF_8)
127+ def responsePayload = new JSONObject(json)
128+ def ticketKey = responsePayload.getString("key")
129+ log.info("[ONBOARDING TASK] Ticket created, key: {}")
130+ def deltas = midpoint.deltaFor(UserType.class)
131+ .item(ItemPath.create('extension', 'onboardingJiraTicket'))
132+ .replace(ticketKey)
133+ .asObjectDeltas(input.oid)
134+ midpoint.executeChanges(deltas, null)
135+ } else {
136+ def errors = responsePayload.getJSONObject("errors")
137+ log.info("[ONBOARDING TASK] Failed to create ticket, reason: {} code: {}", errors.toString(), code)
138+ }
139+ } catch (ConnectTimeoutException ex) {
140+ log.info("Connection timed out after " + CONNECT_TIMEOUT + " ms: " + URL)
141+ } finally {
142+ response.close()
143+ }
144+ } finally {
145+ httpClient.close()
146+ }
147+ }
148+ log.info("[ONBOARDING TASK] Quit task")
149+ </c : code >
150+ </c : value >
151+ </s : parameter >
152+ </s : action >
153+ </scriptExecutionRequest >
154+ </iterativeScripting >
155+ </work >
156+ <controlFlow >
157+ <errorHandling >
158+ <entry >
159+ <situation >
160+ <status >partial_error</status >
161+ </situation >
162+ <reaction >
163+ <ignore />
164+ </reaction >
165+ </entry >
166+ <entry >
167+ <situation >
168+ <status >fatal_error</status >
169+ </situation >
170+ <reaction >
171+ <retryLater >
172+ <initialInterval >PT30M</initialInterval >
173+ <nextInterval >PT1H</nextInterval >
174+ <retryLimit >3</retryLimit >
175+ </retryLater >
176+ </reaction >
177+ </entry >
178+ </errorHandling >
179+ </controlFlow >
180+ </activity >
181+ </task >
0 commit comments