Skip to content

Instantly share code, notes, and snippets.

@nafeger
Created October 22, 2020 15:35
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 nafeger/dc754f59645120df3f5e030dd0166067 to your computer and use it in GitHub Desktop.
Save nafeger/dc754f59645120df3f5e030dd0166067 to your computer and use it in GitHub Desktop.
Jira Adaptavist ScriptRunner Listener to post to Airtable
import groovy.json.JsonBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovyx.net.http.*
/**
Change these based on api.airtable.com
*/
final API_KEY = "YOUR API KEY HERE"
final AIRTABLE_BASE_ID = "appXXXXXX"
final AIRTABLE_TABLE_NAME = "Table Name"
def key = event.issue.getKey()
def type = event.issue.getIssueType().getName()
def action = event.getEventTypeId()
log.error("key identified: "+key)
def http = new HTTPBuilder("https://api.airtable.com")
http.request(Method.POST, ContentType.TEXT) {
uri.path = "/v0/${AIRTABLE_BASE_ID}/${AIRTABLE_TABLE_NAME}"
requestContentType = ContentType.JSON
headers.Authorization = "Bearer ${API_KEY}"
/*
This is not super pretty, but it is effective for writing a single record.
*/
body =
"""{
"records": [
{
"fields": {
"Info" : "${key}",
"Action": "${action}",
"Type": "${type}"
}
}
]
}"""
// log.error("Tracking body: >"+body+"<")
response.success = { resp, json -> log.error("key written: "+key)}
response.failure = { resp, json ->
log.error("key not deleted: "+key+" response: "+resp.statusLine)
throw new RuntimeException("Failure to record key: "+key)
}
}
@nafeger
Copy link
Author

nafeger commented May 10, 2021

This is for a ScriptRunner running on JIRA server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment