Trello: Move Card

Trello: Move Card

Trello: カードを移動

This item moves cards to a specified list 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_CardIds
C3: Card IDs (write one per line) *
conf_ListId
C4: List ID to move the cards to *

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

Capture

See Also

Script (click to open)
  • An XML file that contains the code below is available to download
    • trello-card-move.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 apiKey = authKey.getToken();
    const apiToken = authToken.getToken();

    const cardIdsArray = retrieveCardIds();
    const listId = retrieveListId();

    //// == Calculating / 演算 ==
    const numOfLines = cardIdsArray.length;
    for (let i = 0; i < numOfLines; i++) {
        checkCard(apiKey, apiToken, cardIdsArray[i]);
        moveCard(apiKey, apiToken, cardIdsArray[i], listId);
    }
}

/**
  * config からカード ID を読み出す
  * @return {String} cardIds
  * @return {Array<String>} linesArray  カード ID の配列
  */
function retrieveCardIds() {
    const cardIdsDef = configs.getObject('conf_CardIds');
    const cardIds = engine.findData(cardIdsDef);

    if (cardIds === null || cardIds === "") {
        throw new Error("Card IDs aren't set.");
    }

    let linesArray = cardIds.split("\n");
    linesArray = linesArray.filter(lines => lines !== ""); // 空文字列を削除
    if (linesArray.length === 0) {
        throw new Error("Card IDs aren't set.");
    }

    const numOfLines = linesArray.length;
    if (numOfLines * 2  > httpClient.getRequestingLimit()) {
        throw new Error("Number of Card IDs is over the limit.");
    }
    return linesArray;
}

/**
  * config からリスト ID を読み出す
  * @return {String} listId
  */
function retrieveListId() {
    const listIdDef = configs.getObject('conf_ListId');
    if (listIdDef === null) { // 固定値で指定
        return configs.get('conf_ListId');
    }
    // 文字型データ項目の場合
    if (listIdDef.matchDataType('STRING_TEXTFIELD')) {
        const listId = engine.findData(listIdDef);
        if (listId === null) {
            throw new Error('List ID is blank.');
        }
        return listId;
    }
    // 選択型データ項目の場合
    const selects = engine.findData(listIdDef);
    if (selects === null || selects.size() === 0) {
        throw new Error('List ID is not selected.');
    }
    return selects.get(0).getValue();
}

/**
  * カードの状態を確認する
  * アーカイブ済のカードであればエラー
  * @param {String} auth.apiKey
  * @param {String} auth.apiToken
  * @param {String} cardId
  */
function checkCard(apiKey, apiToken, cardId) {

    const url = `https://api.trello.com/1/cards/${encodeURIComponent(cardId)}`;
    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 new Error(`Failed to get card information. Card ID: ${cardId}, status: ${status}`);
    }
    const jsonRes = JSON.parse(responseStr);
    if (jsonRes.closed === true) {
        throw new Error(`Card ID: ${jsonRes.id} is archived.`);
    }
}

/**
  * カードを移動する
  * @param {String} auth.apiKey
  * @param {String} auth.apiToken
  * @param {String} cardId
  * @param {String} listId
  */
function moveCard(apiKey, apiToken, cardId, listId) {

    const url = `https://api.trello.com/1/cards/${encodeURIComponent(cardId)}`;
    const response = httpClient.begin()
        .queryParam("key", `${apiKey}`)
        .queryParam("token", `${apiToken}`)
        .queryParam("idList", `${listId}`)
        .put(url);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw new Error(`Failed to move. Card ID: ${cardId}, status: ${status}`);
    } else {
        engine.log(`Succeeded to move. Card ID: ${cardId}`);
    }
}

    
Scroll to Top

Discover more from Questetra Support

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

Continue reading