Help us understand the problem. What is going on with this article?

GitHubからアサインされたissue一覧を取得する

More than 1 year has passed since last update.

概要

GoogleAppsScriptからGitHubAPIを呼び出し、アサインされたissueだけをスプレッドシートに出力する。
APIで取得できるissueは20件までしか取得できないため、取得件数が0件になるまで処理を繰り返し出力している。

事前準備

GitHubでアクセストークンを作成しておいてください。

実装

github.gs
function fetchIssues() {
  // スプレッドシート:シート名
  var SHEET_NAME = 'issue';
  // スプレッドシート:開始位置
  var RANGE = 2;

  // GitHub:アクセストークン
  var GITHUB_ACCESS_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  // GitHub:組織名
  var GITHUB_OWNER = 'xxx';
  // GitHub:アカウント名
  var GITHUB_ACCOUNT = 'xxx';
  // GitHub:API
  var GITHUB_API = 'https://api.github.com/orgs/' + GITHUB_OWNER + '/issues';

  // シート情報を初期化して取得
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME).clear();
  // ページ番号
  var page = 1;

  while (true) {
    // GitHubAPIにアクセス
    var url = GITHUB_API + '?access_token=' + GITHUB_ACCESS_TOKEN + '&page=' + page;
    var response = UrlFetchApp.fetch(url);
    var issues = JSON.parse(UrlFetchApp.fetch(url));

    if (issues.length == 0) {
      break;
    }
    // 取得した件数分を出力する
    var skip_index = 0;
    for(var index = 0; index < issues.length; index++) {

      var range = RANGE + index + 20 * (page - 1) - skip_index;
      var repository = issues[index]["repository"];
      var milestone = issues[index]["milestone"];

      // issueのみ出力する
      if (issues[index]["pull_request"] == null) {
        if (repository != null) { 
          // リポジトリ名
          sheet.getRange(range, COLUMN_REPOSITORY_NAME).setValue(repository["name"]);
        }
        if (milestone != null) { 
        // ミッションノート
          sheet.getRange(range, 3).setValue(milestone["title"]);
        }
        // issue番号
        sheet.getRange(range, 4).setValue(issues[index]["number"]);
        // issue名
        sheet.getRange(range, 5).setValue(issues[index]["title"]);
        // URL
        sheet.getRange(range, 6).setValue(issues[index]["html_url"]);
      } else {
        // プルリクエストは除外するためカウントして出力行を調整する
        skip_index++;
      }
    }
    page++;
  }
}
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away