Use Google Apps Script to Call Jira API
We had an interesting requirement to automate the process of adding new values to a custom field in jira.
This custom field in jira stores information used by let say Team ABC in our company who have limited permission in jira which in turn stops them from directly adding new values to the jira custom field.
To get new values added they would reach out to our team , we would then manual add the value to jira custom field.
We first checked the feasibility to automate this , luckily jira supports changes to custom fields via rest api.
The Rest API to be used was
payload='{
"options": [
{
"cascadingOptions": [],
"value": "TEST_VALUE",
},
]
}'
curl --request POST --url '<YOUR_JIRA_INSTANCE>/rest/api/3/customField/<your_customField_value>/option' --user 'username:password' --header 'Accept: application/json' --header 'Content-Type: application/json' --data "${payload}"
Reference : Jira Create Custom Field Documentation
We wanted to automate this , however we had many options (jenkins job, slack slash commands, monitoring script to check changes to common file shared between our team and concerned team , internal portal etc ) along with GoogleAppsScript.
To decide on which approach to follow we had below criteria :
- This Automation should not require us to provide access to user on tools/bump up permission
- Least Amount of our resources should be consumed
- Should be as quick as possible , with a confirmation message to the user ( email/slack message etc)
Learning is a continuous process and the decision we take are somehow dependent on our knowledge of what we learnt in the past
We had an internal meetup group. One of our Technical Architect Sunand ( linkedin.com/in/sunandp ) has given a session on Distributed Systems and the importance of using/developing products which are event based rather than one applications polling every minutes another application for new changes.
With those suggestions in mind we found GoogleAppsScript Apt ( as it meets all our required criteria) for our use case and went ahead with it.
Since we already had an Jira Api to perform what is required , we need a common platform for Team ABC to add new values. so we went ahead with below approach
- Create Google Form which takes required input and stores responses in an excel sheet
- From the Google Form when creating it we have used script editor
Sample Screenshot :
The script editor will open https://script.google.com/ where you can add your script
As we wanted to perform the addition of new value to jira immediately after a new response is submitted , we had to use Triggers in GoogleAppsScript
Trigger used for this use case :
If we comeback to our google app script project , we need to now use google's OnFormSubmit Function and embed our code in it which should collect user response and use that value and send it to jira using its api
You can define key-value pairs in Project Properties ( File-->Project Properties) and read them using getScriptProperties() ( final_token and jira_url will be read using this method) , this allow script to hide passwords and other sensitive information
Now when TEAM ABC enter the input in google form and submit's their response , the GoogleAppsScript Trigger we configured earlier will kick off and runs our script which in turn adds the new value to jira custom field and sends an email to the requestor for confirmation.
Once the Trigger execution completes he would get a confirmation response email
We can do much more using GoogleAppsScript. Do let me know if you find this use case helpful can share the code if interested.
#happylearning
An elegant solution to a simple but mundane usecase.