LANG SELRCT

コードを書く場所についてはこちら

2018年1月1日月曜日

JIRA APIでissueの内容を取得する

JIRAのAPIをたたいてissueの内容をJSONで取得してみる

まずはレスポンスをログに出すところまで



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

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;
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(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_token()からtokenを取得して
issue keyを指定して
optionのオブジェクトを作る
contentType は "application/json"
headers は  {"Authorization": " Basic " + token}

urlにISSUE_URL + keyを入れて
optionを付けてデータを取得して
ログに出す


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




ログに出たresponseを整形するとこのようになっている

response
{
  "expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
  "id": "10000",
  "self": "https://SITENAME.atlassian.net/rest/api/2/issue/10000",
  "key": "KEY-1",
  "fields": {
    "issuetype": {
      "self": "https://SITENAME.atlassian.net/rest/api/2/issuetype/10002",
      "id": "10002",
      "description": "A task that needs to be done.",
      "iconUrl": "https://SITENAME.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype",
      "name": "Task",
      "subtask": false,
      "avatarId": 10318
    },
    "timespent": null,
    "project": {
      "self": "https://SITENAME.atlassian.net/rest/api/2/project/10000",
      "id": "10000",
      "key": "KEY",
      "name": "PROJECT_NAME",
      "avatarUrls": {
        "48x48": "https://SITENAME.atlassian.net/secure/projectavatar?pid=10000&avatarId=10400",
        "24x24": "https://SITENAME.atlassian.net/secure/projectavatar?size=small&pid=10000&avatarId=10400",
        "16x16": "https://SITENAME.atlassian.net/secure/projectavatar?size=xsmall&pid=10000&avatarId=10400",
        "32x32": "https://SITENAME.atlassian.net/secure/projectavatar?size=medium&pid=10000&avatarId=10400"
      }
    },
    "fixVersions": [],
    "aggregatetimespent": null,
    "resolution": null,
    "resolutiondate": null,
    "workratio": -1,
    "lastViewed": "2018-01-02T06:16:27.735+0900",
    "watches": {
      "self": "https://SITENAME.atlassian.net/rest/api/2/issue/KEY-1/watchers",
      "watchCount": 1,
      "isWatching": true
    },
    "created": "2018-01-01T20:39:15.578+0900",
    "customfield_10020": null,
    "customfield_10021": null,
    "customfield_10022": null,
    "priority": {
      "self": "https://SITENAME.atlassian.net/rest/api/2/priority/3",
      "iconUrl": "https://SITENAME.atlassian.net/images/icons/priorities/medium.svg",
      "name": "Medium",
      "id": "3"
    },
    "customfield_10023": null,
    "customfield_10024": null,
    "labels": [],
    "customfield_10017": null,
    "customfield_10018": null,
    "customfield_10019": null,
    "timeestimate": null,
    "aggregatetimeoriginalestimate": null,
    "versions": [],
    "issuelinks": [],
    "assignee": null,
    "updated": "2018-01-02T06:16:26.867+0900",
    "status": {
      "self": "https://SITENAME.atlassian.net/rest/api/2/status/10000",
      "description": "",
      "iconUrl": "https://SITENAME.atlassian.net/",
      "name": "To Do",
      "id": "10000",
      "statusCategory": {
        "self": "https://SITENAME.atlassian.net/rest/api/2/statuscategory/2",
        "id": 2,
        "key": "new",
        "colorName": "blue-gray",
        "name": "To Do"
      }
    },
    "components": [],
    "timeoriginalestimate": null,
    "description": "最初のタスク",
    "customfield_10010": [],
    "customfield_10011": "0|hzzzzz:",
    "customfield_10012": null,
    "customfield_10013": null,
    "timetracking": {},
    "security": null,
    "customfield_10008": null,
    "attachment": [],
    "customfield_10009": null,
    "aggregatetimeestimate": null,
    "summary": "テスト1",
    "creator": {
      "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"
    },
    "subtasks": [],
    "reporter": {
      "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"
    },
    "customfield_10000": "{}",
    "aggregateprogress": {
      "progress": 0,
      "total": 0
    },
    "customfield_10001": null,
    "customfield_10002": null,
    "customfield_10003": null,
    "customfield_10004": null,
    "environment": null,
    "duedate": null,
    "progress": {
      "progress": 0,
      "total": 0
    },
    "votes": {
      "self": "https://SITENAME.atlassian.net/rest/api/2/issue/KEY-1/votes",
      "votes": 0,
      "hasVoted": false
    },
    "comment": {
      "comments": [{
        "self": "https://SITENAME.atlassian.net/rest/api/2/issue/10000/comment/10000",
        "id": "10000",
        "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"
        },
        "body": "最初のタスクのコメント",
        "updateAuthor": {
          "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-02T06:13:25.441+0900",
        "updated": "2018-01-02T06:13:25.441+0900"
      }],
      "maxResults": 1,
      "total": 1,
      "startAt": 0
    },
    "worklog": {
      "startAt": 0,
      "maxResults": 20,
      "total": 0,
      "worklogs": []
    }
  }
}



参考

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