Trello: Get Label ID

Trello: Get Label ID

Trello: ラベル ID 取得

This item gets a label ID on Trello.

Basic Configs
Step Name
Note
Auto Step icon
Configs for this Auto Step
conf_ApiKey
C1: Authorization Setting in which API Key is set *
conf_ApiToken
C2: Authorization Setting in which API Token is set *
conf_BoardId
C3: Board ID *
conf_LabelNames
C4: Label Name (Write one per line) *
conf_LabelId
C5: Data item to save Label ID *

Notes

  • To get your API Key and API Token,
    1. Open the Power-Ups administration page and create a new Power-Up
    2. Proceed to the “API Key” page from the left menu, and your API Key is shown on the page
    3. Click the link in “you can manually generate a Token” to the right of the API Key to get your API Token
  • To get your board ID,
    1. Open your Trello Board in your browser
    2. Add “.json” at the end of the URL in the address bar, and JSON of the board information will be shown
    3. The value of “id” is your board ID
  • This item gets the ID of the first label whose name matches
  • You cannot get the ID of an unnamed label within a board

Capture

See Also

Script (click to open)
  • An XML file that contains the code below is available to download
    • trello-labelid-get.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step


function main() {
    //// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
    const authKey = configs.getObject("conf_ApiKey");
    const authToken = configs.getObject("conf_ApiToken");
    const boardId = configs.get('conf_BoardId');//固定値指定のみ 必須
    const labelNamesArray = decideLabelNames();
    const labelIdDef = configs.getObject('conf_LabelId');

    const apiKey = authKey.getToken();
    const apiToken = authToken.getToken();

    //// == Calculating / 演算 ==
    const labelsArray = getLabels(apiKey, apiToken, boardId);

    // 与えられた関数を配列のすべての要素に対して呼び出し、その結果からなる新しい配列を生成
    const labelIdsArray = labelNamesArray.map(
        labelName => getLabelId(labelsArray, labelName)
    );

    engine.setData(labelIdDef, labelIdsArray.join("\n"));
}


/**
  * config から設定値を読み出す
  * 設定値に空行が含まれている場合はエラー
  * 末尾が改行 1 行で終わる場合だけ、それを除去して処理する
  * @return {Array} labelNamesArray ラベル名の配列
  */
function decideLabelNames() {
    let labelNames = configs.get('conf_LabelNames');//固定値指定のみ 必須

    // まず、末尾に改行がある場合、1 行削除する	
    if (labelNames.endsWith('\n')) {
        labelNames = labelNames.slice(0, -1);
    }
 
    let labelNamesArray = labelNames.split("\n");

    // 残っている配列に空文字が含まれる場合はエラーにする
    if (labelNamesArray.some(line => line === '')) {
        throw "Empty lines are not allowed.";
    }   
    return labelNamesArray;
}


/**
  * ボード内のラベル情報を取得する
  * @param {String} auth.apiKey
  * @param {String} auth.apiToken
  * @param {String} boardId
  * @return {Array} labelsArray
  */
function getLabels(apiKey, apiToken, boardId) {

    const url = `https://api.trello.com/1/boards/${encodeURIComponent(boardId)}/labels`;
    const response = httpClient.begin()
        .queryParam("key", `${apiKey}`)
        .queryParam("token", `${apiToken}`)
        .get(url);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw `Failed to get label information. status: ${status}`;
    }
    return JSON.parse(responseStr);
}


/**
  * ラベル情報からラベル名に対応するラベル ID を取得する
  * ラベル名が複数ある場合、最初に名前が一致したラベルの ID を取得する
  * @param {Object} labelsArray  ラベル情報の JSON 配列
  * @param {String} labelName  ラベル名
  * @return {String} labelObj.id  ラベル ID
  */
function getLabelId(labelsArray, labelName) {

    let labelObj = labelsArray.find(labelElement => labelElement.name === labelName);
    if (labelObj !== undefined) {
        return labelObj.id;
    } else {
        throw `Label name: "${labelName}" not found in the Board`;
    }
}
    
Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading