0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RedmineのREST APIで100件以上のデータを取得する

Last updated at Posted at 2020-10-02

「RedmineのREST APIで100件以上のデータを取得する」というのを、View Customize Pluginを使ってやってみました。

/*
パスのパターン:/issues
挿入位置:全ページのヘッダ
種別:JavaScript
*/

let resultPromisesid = [];
let issueslist = [];
$(function() {
    const apiKey = ViewCustomize.context.user.apiKey;
    if (ViewCustomize.context.project) { //特定のプロジェクト
        project_id = ViewCustomize.context.project.identifier;
        url_pageno = '' + '/issues.json' + '?project_id=' + project_id + '&status_id=*';
    } else { //すべてのプロジェクト
        project_id = "";
        url_pageno = '' + '/issues.json' + '?status_id=*';
    }

    getissueitemList().done(function() {
        console.log(issueslist);
    })
})

//すべてのチケットリストを取得
function getissueitemList() {
    issueslist = [];
    resultPromisesid = [];
    const deferred = new $.Deferred();
    $.when(
        getpageno(url_pageno)
    ).done(function(data) {
        data = JSON.parse(data);
        const page_no = Math.ceil(data.total_count / 100); //ページ数
        for (let i = 1; i <= page_no; i++) {
            if (project_id !== "") {
                url_getissue = '' + '/issues.json' + '?project_id=' + project_id + '&status_id=*&page=' + i + '&limit=100';
            } else {
                url_getissue = '' + '/issues.json' + '?status_id=*&page=' + i + '&limit=100';
            }
            resultPromisesid.push(getallissue(url_getissue));
        }
        //取得したチケットデータから情報を取得
        Promise.all(resultPromisesid).then(function(results) {
            results.forEach(function(result) {
                issues = result.issues;
                for (let j = 0; j < issues.length; j++) {
                    issueid = issues[j].id;
                    subject = issues[j].subject;
                    status_name = issues[j].status.name;
                    issueslist.push({
                        issueid: issueid,
                        subject: subject,
                        status_name: status_name
                    });
                }
            })
            deferred.resolve();
        })
    });
    return deferred;
}

//総チケット数を取得してページ数を計算
function getpageno(url_pageno) {
    const deferred = new $.Deferred();
    return $.ajax({
        type: "GET",
        url: url_pageno,
        headers: {
            'X-Redmine-API-Key': apiKey
        },
        dataType: "text",
        contentType: 'application/json',
    }).done(function() {
        deferred.resolve();
    })
    return deferred;
}

//すべてのチケットをページ(100件)ごとに取得
const getallissue = function(url_getissue) {
    return new Promise(function(resolve) {
        $.ajax({
            type: "GET",
            url: url_getissue,
            headers: {
                'X-Redmine-API-Key': apiKey
            },
            dataType: "text",
            contentType: 'application/json',
        }).done(function(data) {
            resolve(JSON.parse(data));
        })
    })
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?