How to programmatically update Issues from a JQL using REST API in Jira
プラットフォームについて: Cloud、Server、および Data Center - この記事はすべてのプラットフォームに等しく適用されます。
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Fisheye および Crucible は除く
要約
This article proposes a solution to update Issues that match a JQL using Jira's REST API.
環境
All versions of Jira Core & Software 7 and 8.
All versions of Jira Service Manager 3 and 4.
Jira Cloud
ソリューション
The solution is to first search for the issues and then, for each of them, call the update. This would result in "1 + N" REST API calls, N being the number of issues returned by the JQL.
- Perform the JQL search through POST or GET to /rest/api/2/search (Server/DC API reference | Cloud API reference)
- Parse the result to you get only the issue keys returned
- For each issue key, call the update endpoint through PUT to /rest/api/2/issue/{issueIdOrKey} (Server/DC API reference | Cloud API reference)
Examples in shell script
Search issues with the JQL
コマンド$ curl -u admin:admin -X POST localhost:50813/rest/api/2/search -H "Content-Type: application/json" -d '{"jql":"updated > -1d","fields":[""]}' -s | jq > jql-output.txt
Sample output{ "expand": "schema,names", "startAt": 0, "maxResults": 50, "total": 10, "issues": [ { "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields", "id": "10108", "self": "http://localhost:50813/rest/api/2/issue/10108", "key": "SC1-10" }, { "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields", "id": "10107", "self": "http://localhost:50813/rest/api/2/issue/10107", "key": "SC1-9" }, { "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields", "id": "10106", "self": "http://localhost:50813/rest/api/2/issue/10106", "key": "SC1-8" } ] }
Parse the json output
コマンド$ cat jql-output.txt | grep '"key"' | egrep -o ': ".*?"' | cut -c3- | sed 's/"//g' > keys.txt
Sample outputSC1-10 SC1-9 SC1-8
For each issue key, update the issue
コマンド$ for issue in $(cat keys.txt); do curl -u admin:admin -X PUT localhost:50813/rest/api/2/issue/$issue -H "Content-Type: application/json" -d '{"fields":{"customfield_10400":"Sample text for a Text Field!"}}'; done;
Alternative one-line example
コマンド$ for issue in \ $(curl -u admin:admin -X POST localhost:50813/rest/api/2/search -H "Content-Type: application/json" -d '{"jql":"updated > -1d","fields":[""]}' -s | jq | grep '"key"' | egrep -o ': ".*?"' | cut -c3- | sed 's/"//g');\ do curl -u admin:admin -X PUT localhost:50813/rest/api/2/issue/$issue -H "Content-Type: application/json" -d '{"fields":{"customfield_10400":"Sample text for a Text Field!"}}';\ done;
For all command examples above:Replace this For this admin:admin
Valid username:password
(with browse and edit permissions in the projects)
localhost:50813 Jira's base URL updated > -1d The desired JQL customfield_10400 The customfield to update
For more examples on how to edit issues through the REST API, like updating multiple fields, check the Server/DC REST API edit examples or the Cloud REST API edit examples!