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.

  1. Perform the JQL search through POST or GET to /rest/api/2/search (Server/DC API reference | Cloud API reference)
  2. Parse the result to you get only the issue keys returned
  3. 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

  1. 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"
        }
      ]
    }

     
     

  2. Parse the json output

    コマンド
    $ cat jql-output.txt | grep '"key"' | egrep -o ': ".*?"' | cut -c3- | sed 's/"//g' > keys.txt
    Sample output
    SC1-10
    SC1-9
    SC1-8

     
     

  3. 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;

     
     

  4. 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 thisFor this
    admin:admin

    Valid username:password

    (with browse and edit permissions in the projects)

    localhost:50813Jira's base URL
    updated > -1dThe desired JQL
    customfield_10400The 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!

最終更新日: 2020 年 12 月 12 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.