LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Friday, March 31, 2023

JIRA APIで選択リスト(複数選択)を課題作成時に選択してPOSTしたい


JIRA APIを利用して選択リスト(複数選択)フィールドに値を入れたくて書いたコードです。

コード.gsのこの部分で複数選択の値を選択できました。
customfield_10043 は手元のJIRAでの選択リスト(複数選択)のフィールドIDなので、各自の環境によって異なります。
valueの値は選択肢の文字列です。

    customfield_10043: [// 複数選択チェックボックス
      {
        "value": "チェック2"
      },
      {
        "value": "チェック4"
      }
    ],

他の部分はPOSTする際に共通で使っているコードです。



コード.gs
var BASE_URL = 'https://SITENAME.atlassian.net/rest/api/2/';
function doPost() {
  var payload = get_payload();
  var options = get_options(payload);
  var response = UrlFetchApp.fetch(BASE_URL + "issue/", options);
  var key = get_key(response);
  Logger.log(key);
}

function get_payload() {
  var data = {
    project: {key: "KEY"},
    issuetype: {name: "Bug"},
    summary: "たいとる",
    description: "タスクの説明です\n説明の2行め",
    customfield_10043: [// 複数選択チェックボックス
      {
        "value": "チェック2"
      },
      {
        "value": "チェック4"
      }
    ],
    reporter: {id: getAccountId()},
  };
  var fields = {fields: data};
  var payload = JSON.stringify(fields);
  return payload;
}

function get_options(payload) {
  var options = {
    method: "post",
    payload: payload,
    contentType: "application/json",
    headers: {"Authorization": " Basic " + getToken()}
  }
  return options;
}

function getAccountId() {
  var email = Session.getActiveUser().getEmail();
  var url = BASE_URL + 'user/search?query=' + email;
    var options = {
    method: "get",
    contentType: "application/json",
    headers: {"Authorization": " Basic " + getToken()},
    muteHttpExceptions: true
  }
  var response = UrlFetchApp.fetch(url, options);
  var jobj = JSON.parse(response);
  var accountInfo = getAccountInfo(jobj, email);
  var accountId = accountInfo['accountId'];
  return accountId;
}

function getAccountInfo(jobj, email) {
  var accountInfo;
  for(var i = 0; i < jobj.length; i++) {
    var emailAddress = jobj[i]['emailAddress'];
    if(emailAddress === email) {
      accountInfo = jobj[i];
    }
  }
  return accountInfo;
} 

function get_key(response){
  var jobj = JSON.parse(response);
  var key = jobj["key"];
  return key;
}

function getToken() {
  return PropertiesService.getScriptProperties().getProperty('token');
}




Latest post

Extracting data from Google Sheets with regular expressions

Introduction Regular expressions are a powerful tool that can be used to extract data from text.  In Google Sheets, regular expressions ca...