LANG SELRCT

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

Tuesday, January 2, 2018

JIRA APIでissueの変更履歴を取得する


JIRA APIでissueの特定の情報を取得する で作った
get_issue_data() の代わりに
get_issue_histories() という機能を作って変更履歴を取得してみます


issueの変更履歴を取得するには

var url = ISSUE_URL + key; を
var url = ISSUE_URL + key + '?expand=changelog';

として changelog を取得できるようにします


get_issue() の中で Logger.log(response) でログを出してみると
変更履歴は changelog の histories に入っていて
何を変更したかは items の中に入っているようなので
欲しい情報をそれぞれ特定して取得します

抽出する情報

  • 何を変更したか
  • 変更前
  • 変更後
  • 変更した日時

response
{
  "expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
  "id": "10000",
  "self": "https://SITENAME.atlassian.net/rest/api/2/issue/10000",
  "key": "KEY-1",
  "changelog": {
    "startAt": 0,
    "maxResults": 3,
    "total": 3,
    "histories": [{
      "id": "10003",
      "author": {
        "self": "https://SITENAME.atlassian.net/rest/api/2/user?username=admin",
        "name": "admin",
        "key": "admin",
        "accountId": "ACCOUNT_ID",
        "emailAddress": "LOGIN_EMAIL",
        "avatarUrls": {
          "48x48": "https://avatar-cdn.atlassian.com/f539e6a019bdcf6b1635481ef4376f6c?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Ff539e6a019bdcf6b1635481ef4376f6c%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue",
          "24x24": "https://avatar-cdn.atlassian.com/f539e6a019bdcf6b1635481ef4376f6c?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Ff539e6a019bdcf6b1635481ef4376f6c%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue",
          "16x16": "https://avatar-cdn.atlassian.com/f539e6a019bdcf6b1635481ef4376f6c?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Ff539e6a019bdcf6b1635481ef4376f6c%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue",
          "32x32": "https://avatar-cdn.atlassian.com/f539e6a019bdcf6b1635481ef4376f6c?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Ff539e6a019bdcf6b1635481ef4376f6c%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue"
        },
        "displayName": "YOUR_NAME",
        "active": true,
        "timeZone": "Asia/Tokyo"
      },
      "created": "2018-01-02T08:17:22.226+0900",
      "items": [{
        "field": "description",
        "fieldtype": "jira",
        "fieldId": "description",
        "from": null,
        "fromString": "最初のタスク",
        "to": null,
        "toString": "最初のタスクです"
      }]
    }, 以下略


コード.gsを実行すると以下のようなログが出ます

この例ではdescriptionを 最初のタスク から 最初のタスクです に変更しただけ



コード.gs
var ISSUE_URL = 'https://SITENAME.atlassian.net/rest/api/2/issue/';

function get_issue_histories(){
  var response = get_issue();
  var jobj = JSON.parse(response);
  var histories = jobj["changelog"]["histories"][0];
  var created = histories["created"];
  var items = histories["items"][0];
  var field = items["field"];
  var from = items["fromString"];
  var to = items["toString"];
  Logger.log([field, from, to, created]);
}

function get_issue() {
  var token = get_token();
  var key = "KEY-1";
  var options = {
    contentType: "application/json",
    headers: {"Authorization": " Basic " + token}
  };
  var url = ISSUE_URL + key + '?expand=changelog';
  var response = UrlFetchApp.fetch(url, options);
  //Logger.log(response);
  return response;
}

function get_token() {
  var id = "LOGIN_EMAIL";
  var pw = "LOGIN_PASSWORD";
  var token = Utilities.base64Encode(id + ":" + pw);
  return token;
}
意訳
issueのapiをたたくURLをISSUE_URLに入れる

この機能がやること
get_issue()から返ってきたデータを
オブジェクトに変換して
historiesの0番目の中の
作成日を取得して
itemsの0番目の
fieldを取得して(何を変更したか)
変更前の文字列を取得して
変更後の文字列を取得して
ログに出す


この機能がやること
get_token()からtokenを取得して
issue keyを指定して
optionのオブジェクトを作る
contentType は "application/json"
headers は  {"Authorization": " Basic " + token}

urlにISSUE_URL + keyを入れて
optionを付けてデータを取得して
ログに出して(見たい場合は//を消すと出ます)
返す


この機能がやること
JIRAのログインに使っているemailと
パスワードを
base64Encodeでエンコードして
返す



補足


var url = ISSUE_URL + key + '?expand=changelog';
ではなく

var url = ISSUE_URL + key + '/changelog';
としても取得できるようですが
その場合は返ってくるJSONの中身が異なるようです


コード.gsの
var histories = jobj["changelog"]["histories"][0]; を
var histories = jobj["values"][0];
に変更すると取得できると思います


var url = ISSUE_URL + key + '/changelog'の場合は
こんな感じで response が返ってきました

response
{
  "self": "https://SITENAME.atlassian.net/rest/api/2/issue/KEY-1/changelog?maxResults=100&startAt=0",
  "maxResults": 100,
  "startAt": 0,
  "total": 3,
  "isLast": true,
  "values": [{
    "id": "10001",
    "author": {
      "self": "https://SITENAME.atlassian.net/rest/api/2/user?username=admin",
      "name": "admin",
      "key": "admin",
      "accountId": "ACCOUNT_ID",
      "emailAddress": "LOGIN_EMAIL",
      "avatarUrls": {
        "48x48": "https://avatar-cdn.atlassian.com/f539e6a019bdcf6b1635481ef4376f6c?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Ff539e6a019bdcf6b1635481ef4376f6c%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue",
        "24x24": "https://avatar-cdn.atlassian.com/f539e6a019bdcf6b1635481ef4376f6c?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Ff539e6a019bdcf6b1635481ef4376f6c%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue",
        "16x16": "https://avatar-cdn.atlassian.com/f539e6a019bdcf6b1635481ef4376f6c?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Ff539e6a019bdcf6b1635481ef4376f6c%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue",
        "32x32": "https://avatar-cdn.atlassian.com/f539e6a019bdcf6b1635481ef4376f6c?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Ff539e6a019bdcf6b1635481ef4376f6c%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue"
      },
      "displayName": "YOUR_NAME",
      "active": true,
      "timeZone": "Asia/Tokyo"
    },
    "created": "2018-01-02T08:17:22.226+0900",
    "items": [{
      "field": "description",
      "fieldtype": "jira",
      "fieldId": "description",
      "from": null,
      "fromString": "最初のタスク",
      "to": null,
      "toString": "最初のタスクです"
    }]
  }, 以下略



参考

Expansion
https://developer.atlassian.com/cloud/jira/platform/rest/#expansion

Get change logs
https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-issueIdOrKey-changelog-get

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