Trello: Get Member ID

Trello: Get Member ID

Trello: メンバ ID 取得

This item gets a member 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_Names
C4: Member Name (Full Name or User Name; write one per line) *
conf_MemberId
C5: Data item to save Member 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

Capture

See Also

Script (click to open)
  • An XML file that contains the code below is available to download
    • trello-memberid-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 memberNamesArray = decideMemberNames();
    const memberIdDef = configs.getObject('conf_MemberId');

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

    //// == Calculating / 演算 ==
    const membersArray = getMembers(apiKey, apiToken, boardId);

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

    engine.setData(memberIdDef, memberIdsArray.join("\n"));
}


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

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

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


/**
  * ボード内のメンバ情報を取得する
  * @param {String} auth.apiKey
  * @param {String} auth.apiToken
  * @param {String} boardId
  * @return {Array} membersArray
  */
function getMembers(apiKey, apiToken, boardId) {

    const url = `https://api.trello.com/1/boards/${encodeURIComponent(boardId)}/members`;
    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 member information. status: ${status}`;
    }
    return JSON.parse(responseStr);
}


/**
  * メンバ情報からメンバ名に対応するメンバ ID を取得する
  * 最初にユーザ名またはフルネームが、設定したメンバ名に一致した、メンバの ID を取得する
  * @param {Object} membersArray  メンバ情報の JSON 配列
  * @param {String} memberName  メンバ名
  * @return {String} memberObj.id  メンバ ID
  */
function getMemberId(membersArray, memberName) {

    const memberObj = membersArray.find(memberElement => memberElement.fullName === memberName || memberElement.username === memberName);
    if (memberObj !== undefined) {
        return memberObj.id;
    } else {
        throw `Member name: "${memberName}" 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