Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbrenman/5ccebe3f770048d921898870bc3dedd2 to your computer and use it in GitHub Desktop.
Save lbrenman/5ccebe3f770048d921898870bc3dedd2 to your computer and use it in GitHub Desktop.
AMPLIFY Central Connected Gateway Custom API Subscription Approval Flow using JIRA Assets
{
"id": 43386,
"name": "AMPLIFY Central Create JIRA Subscription Flow Part 2",
"description": "Update an API Subscription when a Jira Incident is closed",
"debugLoggingExpires": "2021-01-28T04:59:32Z",
"userId": 21107,
"accountId": 18281,
"createdDate": "2021-01-21T22:30:14Z",
"steps": [{
"id": 468073,
"onSuccess": [],
"onFailure": [],
"name": "addCommentToIncident",
"type": "elementRequest",
"properties": {
"body": "${steps.prepareAddCommentToIncident.body}",
"api": "/${steps.prepareAddCommentToIncident.url}",
"method": "POST",
"elementInstanceId": "${config.jira}"
}
}, {
"id": 467619,
"onSuccess": ["shouldUpdateSubscription"],
"onFailure": [],
"name": "checkSubscriptionStillExists",
"type": "httpRequest",
"properties": {
"url": "${steps.prepareCheckSubscriptionStillExists.url}",
"headers": "${steps. defineGlobals.globalVars.apiServerHeaders}",
"method": "GET"
}
}, {
"id": 467596,
"onSuccess": ["prepareCheckSubscriptionStillExists"],
"onFailure": [],
"name": "defineGlobals",
"type": "script",
"properties": {
"body": "let apicentralUrl = config.apiCentralUrl ? config.apiCentralUrl : 'https://apicentral.axway.com';\n\nconfig.apiCentralUrl = apicentralUrl;\n\nlet apiServerHeaders = {\n 'Authorization': `Bearer ${steps.getAxwayAccessToken.response.body.access_token}`,\n 'Content-Type': 'application/json'\n}\n\nlet globalVars = {\n apiServerHeaders: apiServerHeaders\n}\n\ndone({globalVars});"
}
}, {
"id": 467597,
"onSuccess": ["getFieldNames"],
"onFailure": [],
"name": "defineUtils",
"type": "script",
"properties": {
"body": "function findObjectByKey(array, key, value){\n for (var i = 0; i < array.length; i++) {\n if (array[i][key] === value) {\n return array[i];\n }\n }\n return null;\n}\n\nlet utils = {\n findObjectByKey: findObjectByKey\n}\n\ndone({utils});"
}
}, {
"id": 467598,
"onSuccess": ["defineGlobals"],
"onFailure": [],
"name": "getAxwayAccessToken",
"type": "httpRequest",
"properties": {
"body": "${steps.prepAxwayAccessTokenRequest.form}",
"headers": "${steps.prepAxwayAccessTokenRequest.headers}",
"url": "https://login.axway.com/auth/realms/Broker/protocol/openid-connect/token",
"method": "POST"
}
}, {
"id": 467617,
"onSuccess": ["shouldProcess"],
"onFailure": [],
"name": "getFieldNames",
"type": "elementRequest",
"properties": {
"api": "/fields",
"method": "GET",
"elementInstanceId": "${config.jira}"
}
}, {
"id": 468071,
"onSuccess": ["addCommentToIncident"],
"onFailure": [],
"name": "prepareAddCommentToIncident",
"type": "script",
"properties": {
"body": "let url = 'incidents/'+trigger.body.message.raw.issue.key+'/comments';\n\nlet body = {\n \"body\": \"API subscription \"+steps.prepareUpdateSubscriptionRequest.approvalValue.toLowerCase()+\" by the subscription flow.\"\n}\n\ndone({url:url, body:body});"
}
}, {
"id": 467618,
"onSuccess": ["checkSubscriptionStillExists"],
"onFailure": [],
"name": "prepareCheckSubscriptionStillExists",
"type": "script",
"properties": {
"body": "let catalogItemId = trigger.body.message.raw.issue.fields[steps.defineUtils.utils.findObjectByKey(steps.getFieldNames.response.body, 'name', config.catalogItemIdJiraCustomFieldName).key];\n\nlet subscriptionId = trigger.body.message.raw.issue.fields[steps.defineUtils.utils.findObjectByKey(steps.getFieldNames.response.body, 'name', config.subscriptionIdJiraCustomFieldName).key];\n\nconsole.log(catalogItemId);\nconsole.log(subscriptionId);\n\nlet url = config.apiCentralUrl+'/api/unifiedCatalog/v1/catalogItems/'+catalogItemId+'/subscriptions/'+subscriptionId;\n\ndone({url:url});"
}
}, {
"id": 467620,
"onSuccess": ["updateSubscriptionRequest"],
"onFailure": [],
"name": "prepareUpdateSubscriptionRequest",
"type": "script",
"properties": {
"body": "let url =steps.prepareCheckSubscriptionStillExists.url+'/states';\n\nlet approveRejectField = steps.defineUtils.utils.findObjectByKey(steps.getFieldNames.response.body, 'name', config.approveRejectJiraCustomFieldName).key;\n\nlet approvalState = trigger.body.message.raw.issue.fields[approveRejectField].value;\n\nlet approvalValue = '';\n\nif(approvalState === config.approveRejectJiraCustomFieldApproveValue) {\n approvalValue = \"Approved\";\n} else {\n approvalValue = \"Rejected\";\n}\n\nlet approveRejectCommentField = steps.defineUtils.utils.findObjectByKey(steps.getFieldNames.response.body, 'name', config.approveRejectCommentJiraCustomFieldName).key;\n\nlet description = approvalValue+\" via Custom Subsription Flow\";\n\nif(trigger.body.message.raw.issue.fields.hasOwnProperty(approveRejectCommentField)) {\n description = trigger.body.message.raw.issue.fields[approveRejectCommentField];\n}\n\nconst body = {\n \"description\": description,\n \"state\": approvalValue.toUpperCase()\n}\n\ndone({url:url, body:body, approvalValue: approvalValue});"
}
}, {
"id": 467606,
"onSuccess": ["getAxwayAccessToken"],
"onFailure": [],
"name": "prepAxwayAccessTokenRequest",
"type": "script",
"properties": {
"body": "\ndone({\n 'headers': { \n Authorization: 'Basic ' + CE.b64(config.axwayClientId + ':' + config.axwayClientSecret),\n 'Content-Type': 'application/x-www-form-urlencoded'\n },\n form: 'grant_type=client_credentials'\n});\n"
}
}, {
"id": 467608,
"onSuccess": ["prepAxwayAccessTokenRequest"],
"onFailure": [],
"name": "shouldProcess",
"type": "filter",
"properties": {
"body": "let approveRejectField = steps.defineUtils.utils.findObjectByKey(steps.getFieldNames.response.body, 'name', config.approveRejectJiraCustomFieldName).key;\n\nlet projectKey = trigger.body.message.raw.issue.fields.project.key;\n\ndone((projectKey === config.jiraProjectKey) && (trigger.body.message.raw.issue.fields.status.name === 'Done') && trigger.body.message.raw.issue.fields[approveRejectField].hasOwnProperty('value'));"
}
}, {
"id": 468063,
"onSuccess": ["prepareUpdateSubscriptionRequest"],
"onFailure": [],
"name": "shouldUpdateSubscription",
"type": "filter",
"properties": {
"body": "done(steps.checkSubscriptionStillExists.response.body.state === \"REQUESTED\");"
}
}, {
"id": 467621,
"onSuccess": ["prepareAddCommentToIncident"],
"onFailure": [],
"name": "updateSubscriptionRequest",
"type": "httpRequest",
"properties": {
"body": "${steps.prepareUpdateSubscriptionRequest.body}",
"headers": "${steps. defineGlobals.globalVars.apiServerHeaders}",
"url": "${steps.prepareUpdateSubscriptionRequest.url}",
"method": "POST"
}
}],
"triggers": [{
"id": 39486,
"onSuccess": ["defineUtils"],
"onFailure": [],
"type": "event",
"async": true,
"active": true,
"name": "trigger",
"properties": {
"elementInstanceId": "${config.jira}"
}
}],
"engine": "v3",
"active": true,
"debugLoggingEnabled": false,
"singleThreaded": false,
"configuration": [{
"id": 84252,
"key": "apiCentralUrl",
"name": "apiCentralUrl",
"type": "value",
"required": false
}, {
"id": 84434,
"key": "approveRejectCommentJiraCustomFieldName",
"name": "approveRejectCommentJiraCustomFieldName",
"type": "value",
"required": true
}, {
"id": 84332,
"key": "approveRejectJiraCustomFieldApproveValue",
"name": "approveRejectJiraCustomFieldApproveValue",
"type": "value",
"required": true
}, {
"id": 84331,
"key": "approveRejectJiraCustomFieldName",
"name": "approveRejectJiraCustomFieldName",
"type": "value",
"required": true
}, {
"id": 84333,
"key": "approveRejectJiraCustomFieldRejectValue",
"name": "approveRejectJiraCustomFieldRejectValue",
"type": "value",
"required": true
}, {
"id": 84253,
"key": "axwayClientId",
"name": "axwayClientId",
"type": "value",
"required": true
}, {
"id": 84254,
"key": "axwayClientSecret",
"name": "axwayClientSecret",
"type": "value",
"required": true
}, {
"id": 84329,
"key": "catalogItemIdJiraCustomFieldName",
"name": "catalogItemIdJiraCustomFieldName",
"type": "value",
"required": true
}, {
"id": 84255,
"key": "jira",
"name": "jira",
"type": "elementInstance",
"required": true
}, {
"id": 84257,
"key": "jiraProjectKey",
"name": "jiraProjectKey",
"type": "value",
"required": true
}, {
"id": 84330,
"key": "subscriptionIdJiraCustomFieldName",
"name": "subscriptionIdJiraCustomFieldName",
"type": "value",
"required": true
}]
}
{
"id": 43357,
"name": "AMPLIFY Central Create JIRA Subscription Flow",
"description": "Create a JIRA incident when an amplify central unified catalog API is subscribed to",
"debugLoggingExpires": "2021-02-27T05:45:18Z",
"userId": 21107,
"accountId": 18281,
"createdDate": "2021-01-20T20:54:02Z",
"steps": [{
"id": 476823,
"onSuccess": ["defineUtils"],
"onFailure": [],
"name": "consoleLog",
"type": "script",
"properties": {
"body": "console.log(trigger);\ndone({});"
}
}, {
"id": 467185,
"onSuccess": ["prepareSendToTeams"],
"onFailure": [],
"name": "createIncident",
"type": "elementRequest",
"properties": {
"body": "${steps.prepareCreateIncident.body}",
"api": "/incidents",
"elementInstanceId": "${config.jira}",
"method": "POST"
}
}, {
"id": 467177,
"onSuccess": ["prepareGetCatalogItem"],
"onFailure": [],
"name": "defineGlobals",
"type": "script",
"properties": {
"body": "let tagMap = [\n {\n tagName: \"Research\",\n assigneeId: \"5ac65324cf8dc659b8f1cd98\" // Jim Beam\n },\n {\n tagName: \"Clinical Trial\",\n assigneeId: \"5bb2679d672747437cf74547\" // Leor Brenman\n }\n];\n\nlet apicentralUrl = config.apiCentralUrl ? config.apiCentralUrl : 'https://apicentral.axway.com';\n\nconfig.apiCentralUrl = apicentralUrl;\n\nlet apiServerHeaders = {\n 'Authorization': `Bearer ${steps.getAxwayAccessToken.response.body.access_token}`,\n 'Content-Type': 'application/json'\n}\n\nlet globalVars = {\n apiServerHeaders: apiServerHeaders,\n tagMap: tagMap\n}\n\ndone({globalVars});"
}
}, {
"id": 467182,
"onSuccess": ["shouldProcess"],
"onFailure": [],
"name": "defineUtils",
"type": "script",
"properties": {
"body": "function findObjectByKey(array, key, value){\n for (var i = 0; i < array.length; i++) {\n if (array[i][key] === value) {\n return array[i];\n }\n }\n return null;\n}\n\nlet utils = {\n findObjectByKey: findObjectByKey\n}\n\ndone({utils});"
}
}, {
"id": 467168,
"onSuccess": ["defineGlobals"],
"onFailure": [],
"name": "getAxwayAccessToken",
"type": "httpRequest",
"properties": {
"body": "${steps.prepAxwayAccessTokenRequest.form}",
"headers": "${steps.prepAxwayAccessTokenRequest.headers}",
"method": "POST",
"url": "https://login.axway.com/auth/realms/Broker/protocol/openid-connect/token"
}
}, {
"id": 467179,
"onSuccess": ["prepareGetUser"],
"onFailure": [],
"name": "getCatalogItem",
"type": "httpRequest",
"properties": {
"headers": "${steps. defineGlobals.globalVars.apiServerHeaders}",
"url": "${steps.prepareGetCatalogItem.url}",
"method": "GET"
}
}, {
"id": 467183,
"onSuccess": ["prepareCreateIncident"],
"onFailure": [],
"name": "getFieldNames",
"type": "elementRequest",
"properties": {
"api": "/fields",
"elementInstanceId": "${config.jira}",
"method": "GET"
}
}, {
"id": 467181,
"onSuccess": ["getFieldNames"],
"onFailure": [],
"name": "getUser",
"type": "httpRequest",
"properties": {
"headers": "${steps. defineGlobals.globalVars.apiServerHeaders}",
"method": "GET",
"url": "${steps.prepareGetUser.url}"
}
}, {
"id": 467184,
"onSuccess": ["createIncident"],
"onFailure": [],
"name": "prepareCreateIncident",
"type": "script",
"properties": {
"body": "let subscriptionIdField = steps.defineUtils.utils.findObjectByKey(steps.getFieldNames.response.body, 'name', config.subscriptionIdJiraCustomFieldName);\n\nconsole.log(subscriptionIdField);\n\nlet catalogItemIdField = steps.defineUtils.utils.findObjectByKey(steps.getFieldNames.response.body, 'name', config.catalogItemIdJiraCustomFieldName);\n\nconsole.log(catalogItemIdField);\n\nlet subscriptionRequestURL = config.apiCentralUrl+'/catalog/'+steps.getCatalogItem.response.body.id+'/subscriptions/'+trigger.args.payload.subscription.id;\n\nlet summary = 'Subscription approval request for API: \"'+steps.getCatalogItem.response.body.name+'\"';\n\nlet description = summary+\"\\n\\n\";\n\ndescription += \"Requestor: \\n\";\n\ndescription += steps.getUser.response.body.result.firstname+\" \"+steps.getUser.response.body.result.lastname+\"\\n\";\n\ndescription += steps.getUser.response.body.result.email+\"\\n\\n\";\n\ndescription += \"In order to approve or reject the subscription request, set the Approve/Reject field and close the incident.\\n\\n\";\n\ndescription += \"You can review the susbscription request here:\\n\"+subscriptionRequestURL+\"\\n\\n\";\n\nlet body = {\n \"fields\": {\n \"issuetype\": {\n \"name\": \"Bug\"\n },\n \"priority\": {\n \"name\": \"Highest\"\n },\n \"project\": {\n \"key\": config.jiraProjectKey\n },\n \"summary\": summary,\n \"description\": description\n }\n};\n\n// Check if API has tags\n// If so, check tags against the tagMap t determine assignee\n\nfunction findObjectByKey(array, key, value) {\n for (var i = 0; i < array.length; i++) {\n if (array[i][key] === value) {\n return array[i];\n }\n }\n return null;\n}\n\nvar resultObj;\nvar obj;\nvar i = 0;\n\nif(steps.getCatalogItem.response.body.hasOwnProperty('tags')) {\n if(steps.getCatalogItem.response.body.tags.length > 0) {\n \n console.log(steps.defineGlobals.globalVars.tagMap)\n \n do {\n obj = findObjectByKey(steps.defineGlobals.globalVars.tagMap, \"tagName\", steps.getCatalogItem.response.body.tags[i]);\n if(obj !== null) {\n resultObj = obj;\n }\n i++;\n } while (obj !== null)\n \n // console.log(resultObj);\n // console.log(resultObj.assigneeId);\n \n // body.fields.assignee = {\n // id: resultObj.assigneeId\n // } \n }\n}\n\n\nbody.fields[subscriptionIdField.key] = trigger.args.payload.subscription.id;\nbody.fields[catalogItemIdField.key] = steps.getCatalogItem.response.body.id;\n\ndone({body:body});"
}
}, {
"id": 467178,
"onSuccess": ["getCatalogItem"],
"onFailure": [],
"name": "prepareGetCatalogItem",
"type": "script",
"properties": {
"body": "let url = config.apiCentralUrl+'/api/unifiedCatalog/v1/catalogItems/'+trigger.args.payload.catalogItem.id;\n\ndone({url:url});"
}
}, {
"id": 467180,
"onSuccess": ["getUser"],
"onFailure": [],
"name": "prepareGetUser",
"type": "script",
"properties": {
"body": "done({url:'https://platform.axway.com/api/v1/user/'+trigger.args.payload.subscription.metadata.createUserId});"
}
}, {
"id": 467586,
"onSuccess": ["sendToTeams"],
"onFailure": [],
"name": "prepareSendToTeams",
"type": "script",
"properties": {
"body": "let url = config.msTeamsWebhookURL;\n\nlet subscriptionUrl = config.apiCentralUrl+'/catalog/'+steps.getCatalogItem.response.body.id+'/subscriptions/'+trigger.args.payload.subscription.id;\n\nlet incidentUrl = config.jiraBaseAddress + '/jira/software/c/projects/' + config.jiraProjectKey + '/issues/' + steps.createIncident.response.body.key;\n\nlet body = {\n \"@type\": \"MessageCard\",\n \"@context\": \"http://schema.org/extensions\",\n \"themeColor\": \"0076D7\",\n \"summary\": \"API Subscription Request\",\n \"sections\": [{\n \"activityTitle\": \"[\"+trigger.args.payload.subscription.name+\"](\"+subscriptionUrl+\")\",\n \"activitySubtitle\": \"API: \"+steps.getCatalogItem.response.body.name,\n \"facts\": [{\n \"name\": \"Name:\",\n \"value\": steps.getUser.response.body.result.firstname + ' ' + steps.getUser.response.body.result.lastname\n },{\n \"name\": \"Email:\",\n \"value\": steps.getUser.response.body.result.email\n }],\n \"text\": \"A Jira incident was automatically created. Click on incident link below to approve/reject the API subscription request and set the incident status to Done.\",\n \"markdown\": true\n }],\n \"potentialAction\": [\n {\n \t\t\t\"@type\": \"OpenUri\",\n \t\t\t\"name\": \"View Incident\",\n \t\t\t\"targets\": [\n \t\t\t\t{\n \t\t\t\t\t\"os\": \"default\",\n \t\t\t\t\t\"uri\": incidentUrl\n \t\t\t\t}\n \t\t\t]\n \t\t}\n ]\n};\n\ndone({body: body, url: url});"
}
}, {
"id": 467170,
"onSuccess": ["getAxwayAccessToken"],
"onFailure": [],
"name": "prepAxwayAccessTokenRequest",
"type": "script",
"properties": {
"body": "\ndone({\n 'headers': { \n Authorization: 'Basic ' + CE.b64(config.axwayClientId + ':' + config.axwayClientSecret),\n 'Content-Type': 'application/x-www-form-urlencoded'\n },\n form: 'grant_type=client_credentials'\n});\n"
}
}, {
"id": 467587,
"onSuccess": [],
"onFailure": [],
"name": "sendToTeams",
"type": "httpRequest",
"properties": {
"body": "${steps.prepareSendToTeams.body}",
"url": "${steps.prepareSendToTeams.url}",
"method": "POST"
}
}, {
"id": 467171,
"onSuccess": ["prepAxwayAccessTokenRequest"],
"onFailure": [],
"name": "shouldProcess",
"type": "filter",
"properties": {
"body": "done((trigger.args.type === 'SubscriptionUpdatedEvent') && (trigger.args.payload.subscription.currentState === 'REQUESTED'));"
}
}],
"triggers": [{
"id": 39451,
"onSuccess": ["consoleLog"],
"onFailure": [],
"type": "manual",
"async": true,
"active": true,
"name": "trigger",
"properties": {}
}],
"engine": "v3",
"active": true,
"debugLoggingEnabled": false,
"singleThreaded": false,
"configuration": [{
"id": 84177,
"key": "apiCentralUrl",
"name": "apiCentralUrl",
"type": "value",
"required": false
}, {
"id": 84178,
"key": "axwayClientId",
"name": "axwayClientId",
"type": "value",
"required": true
}, {
"id": 84179,
"key": "axwayClientSecret",
"name": "axwayClientSecret",
"type": "value",
"required": true
}, {
"id": 84334,
"key": "catalogItemIdJiraCustomFieldName",
"name": "catalogItemIdJiraCustomFieldName",
"type": "value",
"required": true
}, {
"id": 84181,
"key": "jira",
"name": "jira",
"type": "elementInstance",
"required": true
}, {
"id": 84182,
"key": "jiraBaseAddress",
"name": "jiraBaseAddress",
"type": "value",
"required": true
}, {
"id": 84183,
"key": "jiraProjectKey",
"name": "jiraProjectKey",
"type": "value",
"required": true
}, {
"id": 84184,
"key": "msTeamsWebhookURL",
"name": "msTeamsWebhookURL",
"type": "value",
"required": true
}, {
"id": 84335,
"key": "subscriptionIdJiraCustomFieldName",
"name": "subscriptionIdJiraCustomFieldName",
"type": "value",
"required": true
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment