From d6f547ecf48a0c09874f2efb31b19b471402f37d Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Fri, 22 Dec 2023 22:40:31 +0200 Subject: [PATCH 01/10] Experiment with new trello card command feature --- action.yml | 3 +++ src/githubRequests.ts | 13 ++++++++++++ src/index.ts | 1 + src/main.ts | 49 ++++++++++++++++++++++++++++++++++++++----- src/trelloRequests.ts | 13 ++++++++++++ src/types.ts | 1 + 6 files changed, 75 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index 7ca451b..20d513a 100644 --- a/action.yml +++ b/action.yml @@ -53,6 +53,9 @@ inputs: trello-remove-unrelated-members: description: Enable or disable the removal of unrelated users on Trello cards. default: true + trello-enable-new-card-command: + description: TODO + default: false runs: using: node20 main: 'dist/index.js' diff --git a/src/githubRequests.ts b/src/githubRequests.ts index 35c5742..6ae243c 100644 --- a/src/githubRequests.ts +++ b/src/githubRequests.ts @@ -39,6 +39,8 @@ export async function getBranchName() { } export async function createComment(shortUrl: string) { + console.log('Creating PR comment', shortUrl) + await octokit.rest.issues.createComment({ owner: repoOwner, repo: payload.repository!.name, @@ -46,3 +48,14 @@ export async function createComment(shortUrl: string) { body: shortUrl, }) } + +export async function updatePullRequestBody(newBody: string) { + console.log('Updating PR body', newBody) + + await octokit.rest.issues.update({ + owner: repoOwner, + repo: payload.repository!.name, + issue_number: issueNumber!, + body: newBody, + }) +} diff --git a/src/index.ts b/src/index.ts index 2d08bb5..eccd4c9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,4 +17,5 @@ run((context.payload.pull_request || context.payload.issue) as PR, { trelloBoardId: core.getInput('trello-board-id'), trelloAddLabelsToCards: core.getBooleanInput('trello-add-labels-to-cards'), trelloRemoveUnrelatedMembers: core.getBooleanInput('trello-remove-unrelated-members'), + trelloEnableNewCardCommand: core.getBooleanInput('trello-enable-new-card-command'), }) diff --git a/src/main.ts b/src/main.ts index e24bb7a..39305b1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,16 @@ import { setFailed } from '@actions/core' -import { createComment, getBranchName, getPullRequestAssignees, getPullRequestComments } from './githubRequests' +import { + createComment, + getBranchName, + getPullRequestAssignees, + getPullRequestComments, + updatePullRequestBody, +} from './githubRequests' import { addAttachmentToCard, addLabelToCard, addMemberToCard, + createCard, getBoardLabels, getBoardLists, getCardAttachments, @@ -17,15 +24,28 @@ import { BoardLabel, Conf, PR, PRHead } from './types' export async function run(pr: PR, conf: Conf = {}) { try { + const created = await createNewCard(conf, pr) + if (created) { + console.log('Exiting early as automatic PR body change will retrigger the action') + + return + } + const comments = await getPullRequestComments() const cardIds = await getCardIds(conf, pr.head, pr.body, comments) if (cardIds.length) { console.log('Found card IDs', cardIds) + const linked = await addCardLinkToPR(conf, cardIds, pr.body, comments) + if (linked) { + console.log('Exiting early as automatic comment will retrigger the action') + + return + } + await moveCards(conf, cardIds, pr) await addPRLinkToCards(cardIds, pr.html_url || pr.url) - await addCardLinkToPR(conf, cardIds, pr.body, comments) await updateCardMembers(conf, cardIds) await addLabelToCards(conf, cardIds, pr.head) } @@ -35,6 +55,23 @@ export async function run(pr: PR, conf: Conf = {}) { } } +async function createNewCard(conf: Conf, pr: PR) { + if (!conf.trelloEnableNewCardCommand) { + return false + } + const isDraft = isDraftPr(pr) + const listId = pr.state === 'open' && isDraft ? conf.trelloListIdPrDraft : conf.trelloListIdPrOpen + + if (listId && pr.body?.includes('/new-trello-card')) { + const card = await createCard(listId, pr.title, pr.body) + await updatePullRequestBody(pr.body.replace('/new-trello-card', card.url)) + + return true + } + + return false +} + async function getCardIds(conf: Conf, prHead: PRHead, prBody: string = '', comments: { body?: string }[]) { console.log('Searching for card ids') @@ -187,20 +224,20 @@ async function addPRLinkToCards(cardIds: string[], link: string) { async function addCardLinkToPR(conf: Conf, cardIds: string[], prBody: string = '', comments: { body?: string }[] = []) { if (!conf.githubIncludePrBranchName) { - return + return false } if (matchCardIds(conf, prBody || '')?.length) { console.log('Card is already linked in the PR description') - return + return false } for (const comment of comments) { if (matchCardIds(conf, comment.body)?.length) { console.log('Card is already linked in the comment') - return + return false } } console.log('Commenting Trello card URL to PR', cardIds[0]) @@ -208,6 +245,8 @@ async function addCardLinkToPR(conf: Conf, cardIds: string[], prBody: string = ' const cardInfo = await getCardInfo(cardIds[0]) await createComment(cardInfo.shortUrl) + + return true } async function updateCardMembers(conf: Conf, cardIds: string[]) { diff --git a/src/trelloRequests.ts b/src/trelloRequests.ts index 67a3d3b..52b156d 100644 --- a/src/trelloRequests.ts +++ b/src/trelloRequests.ts @@ -91,6 +91,19 @@ export async function getMemberInfo(username?: string): Promise<{ id: string; or return response?.data } +export async function createCard(listId: string, title: string, body?: string): Promise<{ url: string }> { + console.log('Creating card based on PR info', title, body) + + const response = await makeRequest('post', `https://api.trello.com/1/cards`, { + idList: listId, + name: title, + desc: body, + pos: trelloCardPosition, + }) + + return response?.data +} + async function makeRequest(method: 'get' | 'put' | 'post' | 'delete', url: string, params?: Record) { try { let response diff --git a/src/types.ts b/src/types.ts index c04448a..4850d88 100644 --- a/src/types.ts +++ b/src/types.ts @@ -14,6 +14,7 @@ export interface Conf { trelloConflictingLabels?: string[] trelloRemoveUnrelatedMembers?: boolean trelloAddLabelsToCards?: boolean + trelloEnableNewCardCommand?: boolean } export type PR = Exclude From a06fac26d371bb27027a9bd7bf6a9799e82114b8 Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Fri, 22 Dec 2023 22:47:49 +0200 Subject: [PATCH 02/10] Update build --- dist/index.js | 59 +++++++++++++++++++++++++++++++++++++++++++++------ src/main.ts | 14 ++++++------ 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/dist/index.js b/dist/index.js index 456dc00..aaacb1e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33154,7 +33154,7 @@ function wrappy (fn, cb) { "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.createComment = exports.getBranchName = exports.getPullRequestAssignees = exports.getPullRequestComments = void 0; +exports.updatePullRequestBody = exports.createComment = exports.getBranchName = exports.getPullRequestAssignees = exports.getPullRequestComments = void 0; const core_1 = __nccwpck_require__(2186); const github_1 = __nccwpck_require__(5438); const githubToken = (0, core_1.getInput)('github-token', { required: true }); @@ -33190,6 +33190,7 @@ async function getBranchName() { } exports.getBranchName = getBranchName; async function createComment(shortUrl) { + console.log('Creating PR comment', shortUrl); await octokit.rest.issues.createComment({ owner: repoOwner, repo: payload.repository.name, @@ -33198,6 +33199,16 @@ async function createComment(shortUrl) { }); } exports.createComment = createComment; +async function updatePullRequestBody(newBody) { + console.log('Updating PR body', newBody); + await octokit.rest.issues.update({ + owner: repoOwner, + repo: payload.repository.name, + issue_number: issueNumber, + body: newBody, + }); +} +exports.updatePullRequestBody = updatePullRequestBody; /***/ }), @@ -33248,6 +33259,7 @@ const main_1 = __nccwpck_require__(399); trelloBoardId: core.getInput('trello-board-id'), trelloAddLabelsToCards: core.getBooleanInput('trello-add-labels-to-cards'), trelloRemoveUnrelatedMembers: core.getBooleanInput('trello-remove-unrelated-members'), + trelloEnableNewCardCommand: core.getBooleanInput('trello-enable-new-card-command'), }); @@ -33265,13 +33277,19 @@ const githubRequests_1 = __nccwpck_require__(2963); const trelloRequests_1 = __nccwpck_require__(777); async function run(pr, conf = {}) { try { + if (await createNewCard(conf, pr)) { + console.log('Exiting early as automatic PR body change will retrigger the action'); + return; + } const comments = await (0, githubRequests_1.getPullRequestComments)(); const cardIds = await getCardIds(conf, pr.head, pr.body, comments); if (cardIds.length) { - console.log('Found card IDs', cardIds); + if (await addCardLinkToPR(conf, cardIds, pr.body, comments)) { + console.log('Exiting early as automatic comment will retrigger the action'); + return; + } await moveCards(conf, cardIds, pr); await addPRLinkToCards(cardIds, pr.html_url || pr.url); - await addCardLinkToPR(conf, cardIds, pr.body, comments); await updateCardMembers(conf, cardIds); await addLabelToCards(conf, cardIds, pr.head); } @@ -33282,6 +33300,19 @@ async function run(pr, conf = {}) { } } exports.run = run; +async function createNewCard(conf, pr) { + if (!conf.trelloEnableNewCardCommand) { + return false; + } + const isDraft = isDraftPr(pr); + const listId = pr.state === 'open' && isDraft ? conf.trelloListIdPrDraft : conf.trelloListIdPrOpen; + if (listId && pr.body?.includes('/new-trello-card')) { + const card = await (0, trelloRequests_1.createCard)(listId, pr.title, pr.body); + await (0, githubRequests_1.updatePullRequestBody)(pr.body.replace('/new-trello-card', card.url)); + return true; + } + return false; +} async function getCardIds(conf, prHead, prBody = '', comments) { console.log('Searching for card ids'); let cardIds = matchCardIds(conf, prBody || ''); @@ -33291,11 +33322,13 @@ async function getCardIds(conf, prHead, prBody = '', comments) { } } if (cardIds.length) { + console.log('Found card IDs', cardIds); return [...new Set(cardIds)]; } if (conf.githubIncludePrBranchName) { const cardId = await getCardIdFromBranch(prHead); if (cardId) { + console.log('Found card ID from branch name'); return [cardId]; } } @@ -33395,21 +33428,22 @@ async function addPRLinkToCards(cardIds, link) { } async function addCardLinkToPR(conf, cardIds, prBody = '', comments = []) { if (!conf.githubIncludePrBranchName) { - return; + return false; } if (matchCardIds(conf, prBody || '')?.length) { console.log('Card is already linked in the PR description'); - return; + return false; } for (const comment of comments) { if (matchCardIds(conf, comment.body)?.length) { console.log('Card is already linked in the comment'); - return; + return false; } } console.log('Commenting Trello card URL to PR', cardIds[0]); const cardInfo = await (0, trelloRequests_1.getCardInfo)(cardIds[0]); await (0, githubRequests_1.createComment)(cardInfo.shortUrl); + return true; } async function updateCardMembers(conf, cardIds) { const assignees = await (0, githubRequests_1.getPullRequestAssignees)(); @@ -33567,7 +33601,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.getMemberInfo = exports.moveCardToList = exports.removeMemberFromCard = exports.addLabelToCard = exports.getBoardLists = exports.getBoardLabels = exports.addMemberToCard = exports.addAttachmentToCard = exports.getCardAttachments = exports.getCardInfo = exports.searchTrelloCards = void 0; +exports.createCard = exports.getMemberInfo = exports.moveCardToList = exports.removeMemberFromCard = exports.addLabelToCard = exports.getBoardLists = exports.getBoardLabels = exports.addMemberToCard = exports.addAttachmentToCard = exports.getCardAttachments = exports.getCardInfo = exports.searchTrelloCards = void 0; const axios_1 = __importDefault(__nccwpck_require__(8757)); const core = __importStar(__nccwpck_require__(2186)); const trelloApiKey = core.getInput('trello-api-key', { required: true }); @@ -33643,6 +33677,17 @@ async function getMemberInfo(username) { return response?.data; } exports.getMemberInfo = getMemberInfo; +async function createCard(listId, title, body) { + console.log('Creating card based on PR info', title, body); + const response = await makeRequest('post', `https://api.trello.com/1/cards`, { + idList: listId, + name: title, + desc: body, + pos: trelloCardPosition, + }); + return response?.data; +} +exports.createCard = createCard; async function makeRequest(method, url, params) { try { let response; diff --git a/src/main.ts b/src/main.ts index 39305b1..06a57a2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,26 +24,20 @@ import { BoardLabel, Conf, PR, PRHead } from './types' export async function run(pr: PR, conf: Conf = {}) { try { - const created = await createNewCard(conf, pr) - if (created) { + if (await createNewCard(conf, pr)) { console.log('Exiting early as automatic PR body change will retrigger the action') return } - const comments = await getPullRequestComments() const cardIds = await getCardIds(conf, pr.head, pr.body, comments) if (cardIds.length) { - console.log('Found card IDs', cardIds) - - const linked = await addCardLinkToPR(conf, cardIds, pr.body, comments) - if (linked) { + if (await addCardLinkToPR(conf, cardIds, pr.body, comments)) { console.log('Exiting early as automatic comment will retrigger the action') return } - await moveCards(conf, cardIds, pr) await addPRLinkToCards(cardIds, pr.html_url || pr.url) await updateCardMembers(conf, cardIds) @@ -84,6 +78,8 @@ async function getCardIds(conf: Conf, prHead: PRHead, prBody: string = '', comme } if (cardIds.length) { + console.log('Found card IDs', cardIds) + return [...new Set(cardIds)] } @@ -91,6 +87,8 @@ async function getCardIds(conf: Conf, prHead: PRHead, prBody: string = '', comme const cardId = await getCardIdFromBranch(prHead) if (cardId) { + console.log('Found card ID from branch name') + return [cardId] } } From 0c8290e96ac918a1fcbdfa65e820f994129f4bd3 Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Sat, 23 Dec 2023 10:17:37 +0200 Subject: [PATCH 03/10] Update logic --- dist/index.js | 54 ++++++++++++++++------------------- src/main.ts | 66 ++++++++++++++++++++----------------------- src/trelloRequests.ts | 2 +- 3 files changed, 56 insertions(+), 66 deletions(-) diff --git a/dist/index.js b/dist/index.js index aaacb1e..5f5e6be 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33277,19 +33277,12 @@ const githubRequests_1 = __nccwpck_require__(2963); const trelloRequests_1 = __nccwpck_require__(777); async function run(pr, conf = {}) { try { - if (await createNewCard(conf, pr)) { - console.log('Exiting early as automatic PR body change will retrigger the action'); - return; - } const comments = await (0, githubRequests_1.getPullRequestComments)(); - const cardIds = await getCardIds(conf, pr.head, pr.body, comments); + const cardIds = await getCardIds(conf, pr, comments); if (cardIds.length) { - if (await addCardLinkToPR(conf, cardIds, pr.body, comments)) { - console.log('Exiting early as automatic comment will retrigger the action'); - return; - } await moveCards(conf, cardIds, pr); await addPRLinkToCards(cardIds, pr.html_url || pr.url); + await addCardLinkToPR(conf, cardIds, pr.body, comments); await updateCardMembers(conf, cardIds); await addLabelToCards(conf, cardIds, pr.head); } @@ -33300,33 +33293,24 @@ async function run(pr, conf = {}) { } } exports.run = run; -async function createNewCard(conf, pr) { - if (!conf.trelloEnableNewCardCommand) { - return false; - } - const isDraft = isDraftPr(pr); - const listId = pr.state === 'open' && isDraft ? conf.trelloListIdPrDraft : conf.trelloListIdPrOpen; - if (listId && pr.body?.includes('/new-trello-card')) { - const card = await (0, trelloRequests_1.createCard)(listId, pr.title, pr.body); - await (0, githubRequests_1.updatePullRequestBody)(pr.body.replace('/new-trello-card', card.url)); - return true; - } - return false; -} -async function getCardIds(conf, prHead, prBody = '', comments) { +async function getCardIds(conf, pr, comments) { console.log('Searching for card ids'); - let cardIds = matchCardIds(conf, prBody || ''); + let cardIds = matchCardIds(conf, pr.body || ''); if (conf.githubIncludePrComments) { for (const comment of comments) { cardIds = [...cardIds, ...matchCardIds(conf, comment.body)]; } } + const createdCardId = await createNewCard(conf, pr); + if (createdCardId) { + cardIds = [...cardIds, createdCardId]; + } if (cardIds.length) { console.log('Found card IDs', cardIds); return [...new Set(cardIds)]; } if (conf.githubIncludePrBranchName) { - const cardId = await getCardIdFromBranch(prHead); + const cardId = await getCardIdFromBranch(pr.head); if (cardId) { console.log('Found card ID from branch name'); return [cardId]; @@ -33355,6 +33339,19 @@ function matchCardIds(conf, text) { return cardIds; }))); } +async function createNewCard(conf, pr) { + if (!conf.trelloEnableNewCardCommand) { + return; + } + const isDraft = isDraftPr(pr); + const listId = pr.state === 'open' && isDraft ? conf.trelloListIdPrDraft : conf.trelloListIdPrOpen; + if (listId && pr.body?.includes('/new-trello-card')) { + const card = await (0, trelloRequests_1.createCard)(listId, pr.title, pr.body.replace('/new-trello-card', '')); + await (0, githubRequests_1.updatePullRequestBody)(pr.body.replace('/new-trello-card', card.url)); + return card.id; + } + return; +} async function getCardIdFromBranch(prHead) { console.log('Searching card from branch name'); const branchName = prHead?.ref || (await (0, githubRequests_1.getBranchName)()); @@ -33428,22 +33425,21 @@ async function addPRLinkToCards(cardIds, link) { } async function addCardLinkToPR(conf, cardIds, prBody = '', comments = []) { if (!conf.githubIncludePrBranchName) { - return false; + return; } if (matchCardIds(conf, prBody || '')?.length) { console.log('Card is already linked in the PR description'); - return false; + return; } for (const comment of comments) { if (matchCardIds(conf, comment.body)?.length) { console.log('Card is already linked in the comment'); - return false; + return; } } console.log('Commenting Trello card URL to PR', cardIds[0]); const cardInfo = await (0, trelloRequests_1.getCardInfo)(cardIds[0]); await (0, githubRequests_1.createComment)(cardInfo.shortUrl); - return true; } async function updateCardMembers(conf, cardIds) { const assignees = await (0, githubRequests_1.getPullRequestAssignees)(); diff --git a/src/main.ts b/src/main.ts index 06a57a2..ea784cc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,22 +24,13 @@ import { BoardLabel, Conf, PR, PRHead } from './types' export async function run(pr: PR, conf: Conf = {}) { try { - if (await createNewCard(conf, pr)) { - console.log('Exiting early as automatic PR body change will retrigger the action') - - return - } const comments = await getPullRequestComments() - const cardIds = await getCardIds(conf, pr.head, pr.body, comments) + const cardIds = await getCardIds(conf, pr, comments) if (cardIds.length) { - if (await addCardLinkToPR(conf, cardIds, pr.body, comments)) { - console.log('Exiting early as automatic comment will retrigger the action') - - return - } await moveCards(conf, cardIds, pr) await addPRLinkToCards(cardIds, pr.html_url || pr.url) + await addCardLinkToPR(conf, cardIds, pr.body, comments) await updateCardMembers(conf, cardIds) await addLabelToCards(conf, cardIds, pr.head) } @@ -49,27 +40,10 @@ export async function run(pr: PR, conf: Conf = {}) { } } -async function createNewCard(conf: Conf, pr: PR) { - if (!conf.trelloEnableNewCardCommand) { - return false - } - const isDraft = isDraftPr(pr) - const listId = pr.state === 'open' && isDraft ? conf.trelloListIdPrDraft : conf.trelloListIdPrOpen - - if (listId && pr.body?.includes('/new-trello-card')) { - const card = await createCard(listId, pr.title, pr.body) - await updatePullRequestBody(pr.body.replace('/new-trello-card', card.url)) - - return true - } - - return false -} - -async function getCardIds(conf: Conf, prHead: PRHead, prBody: string = '', comments: { body?: string }[]) { +async function getCardIds(conf: Conf, pr: PR, comments: { body?: string }[]) { console.log('Searching for card ids') - let cardIds = matchCardIds(conf, prBody || '') + let cardIds = matchCardIds(conf, pr.body || '') if (conf.githubIncludePrComments) { for (const comment of comments) { @@ -77,6 +51,11 @@ async function getCardIds(conf: Conf, prHead: PRHead, prBody: string = '', comme } } + const createdCardId = await createNewCard(conf, pr) + if (createdCardId) { + cardIds = [...cardIds, createdCardId] + } + if (cardIds.length) { console.log('Found card IDs', cardIds) @@ -84,7 +63,7 @@ async function getCardIds(conf: Conf, prHead: PRHead, prBody: string = '', comme } if (conf.githubIncludePrBranchName) { - const cardId = await getCardIdFromBranch(prHead) + const cardId = await getCardIdFromBranch(pr.head) if (cardId) { console.log('Found card ID from branch name') @@ -127,6 +106,23 @@ function matchCardIds(conf: Conf, text?: string) { ) } +async function createNewCard(conf: Conf, pr: PR) { + if (!conf.trelloEnableNewCardCommand) { + return + } + const isDraft = isDraftPr(pr) + const listId = pr.state === 'open' && isDraft ? conf.trelloListIdPrDraft : conf.trelloListIdPrOpen + + if (listId && pr.body?.includes('/new-trello-card')) { + const card = await createCard(listId, pr.title, pr.body.replace('/new-trello-card', '')) + await updatePullRequestBody(pr.body.replace('/new-trello-card', card.url)) + + return card.id + } + + return +} + async function getCardIdFromBranch(prHead?: PRHead) { console.log('Searching card from branch name') @@ -222,20 +218,20 @@ async function addPRLinkToCards(cardIds: string[], link: string) { async function addCardLinkToPR(conf: Conf, cardIds: string[], prBody: string = '', comments: { body?: string }[] = []) { if (!conf.githubIncludePrBranchName) { - return false + return } if (matchCardIds(conf, prBody || '')?.length) { console.log('Card is already linked in the PR description') - return false + return } for (const comment of comments) { if (matchCardIds(conf, comment.body)?.length) { console.log('Card is already linked in the comment') - return false + return } } console.log('Commenting Trello card URL to PR', cardIds[0]) @@ -243,8 +239,6 @@ async function addCardLinkToPR(conf: Conf, cardIds: string[], prBody: string = ' const cardInfo = await getCardInfo(cardIds[0]) await createComment(cardInfo.shortUrl) - - return true } async function updateCardMembers(conf: Conf, cardIds: string[]) { diff --git a/src/trelloRequests.ts b/src/trelloRequests.ts index 52b156d..2343a35 100644 --- a/src/trelloRequests.ts +++ b/src/trelloRequests.ts @@ -91,7 +91,7 @@ export async function getMemberInfo(username?: string): Promise<{ id: string; or return response?.data } -export async function createCard(listId: string, title: string, body?: string): Promise<{ url: string }> { +export async function createCard(listId: string, title: string, body?: string): Promise<{ id: string; url: string }> { console.log('Creating card based on PR info', title, body) const response = await makeRequest('post', `https://api.trello.com/1/cards`, { From 6924a1e55eb86506225b61eb1dab12e24d0dc765 Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Sat, 23 Dec 2023 10:25:42 +0200 Subject: [PATCH 04/10] Change member logic --- dist/index.js | 4 ++-- src/main.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index 5f5e6be..d405246 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33283,8 +33283,8 @@ async function run(pr, conf = {}) { await moveCards(conf, cardIds, pr); await addPRLinkToCards(cardIds, pr.html_url || pr.url); await addCardLinkToPR(conf, cardIds, pr.body, comments); - await updateCardMembers(conf, cardIds); await addLabelToCards(conf, cardIds, pr.head); + await updateCardMembers(conf, cardIds); } } catch (error) { @@ -33456,10 +33456,10 @@ async function updateCardMembers(conf, cardIds) { } return Promise.all(cardIds.map(async (cardId) => { const cardInfo = await (0, trelloRequests_1.getCardInfo)(cardId); + await addNewMembers(cardInfo, memberIds); if (conf.trelloRemoveUnrelatedMembers) { await removeUnrelatedMembers(cardInfo, memberIds); } - return addNewMembers(cardInfo, memberIds); })); } async function getTrelloMemberId(conf, githubUserName) { diff --git a/src/main.ts b/src/main.ts index ea784cc..56804aa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,8 +31,8 @@ export async function run(pr: PR, conf: Conf = {}) { await moveCards(conf, cardIds, pr) await addPRLinkToCards(cardIds, pr.html_url || pr.url) await addCardLinkToPR(conf, cardIds, pr.body, comments) - await updateCardMembers(conf, cardIds) await addLabelToCards(conf, cardIds, pr.head) + await updateCardMembers(conf, cardIds) } } catch (error: any) { setFailed(error) @@ -264,11 +264,11 @@ async function updateCardMembers(conf: Conf, cardIds: string[]) { cardIds.map(async (cardId) => { const cardInfo = await getCardInfo(cardId) + await addNewMembers(cardInfo, memberIds) + if (conf.trelloRemoveUnrelatedMembers) { await removeUnrelatedMembers(cardInfo, memberIds) } - - return addNewMembers(cardInfo, memberIds) }), ) } From 678d1cf322dbc292dbff951314eab02b92f898fc Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Sat, 23 Dec 2023 10:51:18 +0200 Subject: [PATCH 05/10] Improve new card command logic --- README.md | 7 +++++++ action.yml | 8 ++++---- dist/index.js | 11 ++++++----- src/index.ts | 2 +- src/main.ts | 9 +++++---- src/types.ts | 2 +- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index bb99ead..6f52cfe 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ This action scans PR description and comments for Trello card URL(s) or branch n - Assigns a PR author and fellow assignees to a Trello card. - And more... +You can also optionally create new Trello cards by adding `/new-trello-card` in the PR description. + ## Basic configuration ```yaml @@ -51,6 +53,11 @@ github-include-pr-comments: true # DEFAULT: false github-include-pr-branch-name: false +# Creates a new Trello card from PR details if "/new-trello-card" is written in the PR description. +# Replaces "/new-trello-card" with the card link. +# DEFAULT: false +github-include-new-card-command: false + # Only matches Trello URLs prefixed with "Closes" etc. # Just like https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword # DEFAULT: false diff --git a/action.yml b/action.yml index 20d513a..fa1fe6c 100644 --- a/action.yml +++ b/action.yml @@ -20,6 +20,9 @@ inputs: github-include-pr-branch-name: description: Include PR branch name when searching for Trello cards (e.g. "1234-card-title"). If card ID is found, it automatically comments card URL to the PR. default: false + github-include-new-card-command: + description: Creates a new Trello card from PR details if "/new-trello-card" is written in the PR description. Replaces "/new-trello-card" with the card link. + default: false github-users-to-trello-users: description: |- Newline-separated list of mapping between Github username and Trello username. Example: @@ -51,11 +54,8 @@ inputs: description: Position of the card after being moved to a list. Can be "top" or "bottom". default: 'top' trello-remove-unrelated-members: - description: Enable or disable the removal of unrelated users on Trello cards. + description: Removes card members who are not authors or assignees of the PR. default: true - trello-enable-new-card-command: - description: TODO - default: false runs: using: node20 main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index d405246..8e48623 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33250,6 +33250,7 @@ const main_1 = __nccwpck_require__(399); githubRequireTrelloCard: core.getBooleanInput('github-require-trello-card'), githubIncludePrComments: core.getBooleanInput('github-include-pr-comments'), githubIncludePrBranchName: core.getBooleanInput('github-include-pr-branch-name'), + githubIncludeNewCardCommand: core.getBooleanInput('github-include-new-card-command'), githubUsersToTrelloUsers: core.getInput('github-users-to-trello-users'), trelloOrganizationName: core.getInput('trello-organization-name'), trelloListIdPrDraft: core.getInput('trello-list-id-pr-draft'), @@ -33259,7 +33260,6 @@ const main_1 = __nccwpck_require__(399); trelloBoardId: core.getInput('trello-board-id'), trelloAddLabelsToCards: core.getBooleanInput('trello-add-labels-to-cards'), trelloRemoveUnrelatedMembers: core.getBooleanInput('trello-remove-unrelated-members'), - trelloEnableNewCardCommand: core.getBooleanInput('trello-enable-new-card-command'), }); @@ -33340,14 +33340,15 @@ function matchCardIds(conf, text) { }))); } async function createNewCard(conf, pr) { - if (!conf.trelloEnableNewCardCommand) { + if (!conf.githubIncludeNewCardCommand) { return; } const isDraft = isDraftPr(pr); const listId = pr.state === 'open' && isDraft ? conf.trelloListIdPrDraft : conf.trelloListIdPrOpen; - if (listId && pr.body?.includes('/new-trello-card')) { - const card = await (0, trelloRequests_1.createCard)(listId, pr.title, pr.body.replace('/new-trello-card', '')); - await (0, githubRequests_1.updatePullRequestBody)(pr.body.replace('/new-trello-card', card.url)); + const commandRegex = /(^|\s)\/new-trello-card(\s|$)/; // Avoids matching URLs + if (listId && pr.body && commandRegex.test(pr.body)) { + const card = await (0, trelloRequests_1.createCard)(listId, pr.title, pr.body.replace(commandRegex, '')); + await (0, githubRequests_1.updatePullRequestBody)(pr.body.replace(commandRegex, card.url)); return card.id; } return; diff --git a/src/index.ts b/src/index.ts index eccd4c9..b534a9e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ run((context.payload.pull_request || context.payload.issue) as PR, { githubRequireTrelloCard: core.getBooleanInput('github-require-trello-card'), githubIncludePrComments: core.getBooleanInput('github-include-pr-comments'), githubIncludePrBranchName: core.getBooleanInput('github-include-pr-branch-name'), + githubIncludeNewCardCommand: core.getBooleanInput('github-include-new-card-command'), githubUsersToTrelloUsers: core.getInput('github-users-to-trello-users'), trelloOrganizationName: core.getInput('trello-organization-name'), trelloListIdPrDraft: core.getInput('trello-list-id-pr-draft'), @@ -17,5 +18,4 @@ run((context.payload.pull_request || context.payload.issue) as PR, { trelloBoardId: core.getInput('trello-board-id'), trelloAddLabelsToCards: core.getBooleanInput('trello-add-labels-to-cards'), trelloRemoveUnrelatedMembers: core.getBooleanInput('trello-remove-unrelated-members'), - trelloEnableNewCardCommand: core.getBooleanInput('trello-enable-new-card-command'), }) diff --git a/src/main.ts b/src/main.ts index 56804aa..8c50eff 100644 --- a/src/main.ts +++ b/src/main.ts @@ -107,15 +107,16 @@ function matchCardIds(conf: Conf, text?: string) { } async function createNewCard(conf: Conf, pr: PR) { - if (!conf.trelloEnableNewCardCommand) { + if (!conf.githubIncludeNewCardCommand) { return } const isDraft = isDraftPr(pr) const listId = pr.state === 'open' && isDraft ? conf.trelloListIdPrDraft : conf.trelloListIdPrOpen + const commandRegex = /(^|\s)\/new-trello-card(\s|$)/ // Avoids matching URLs - if (listId && pr.body?.includes('/new-trello-card')) { - const card = await createCard(listId, pr.title, pr.body.replace('/new-trello-card', '')) - await updatePullRequestBody(pr.body.replace('/new-trello-card', card.url)) + if (listId && pr.body && commandRegex.test(pr.body)) { + const card = await createCard(listId, pr.title, pr.body.replace(commandRegex, '')) + await updatePullRequestBody(pr.body.replace(commandRegex, card.url)) return card.id } diff --git a/src/types.ts b/src/types.ts index 4850d88..74a66a4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,6 +4,7 @@ export interface Conf { githubRequireTrelloCard?: boolean githubIncludePrComments?: boolean githubIncludePrBranchName?: boolean + githubIncludeNewCardCommand?: boolean githubRequireKeywordPrefix?: boolean githubUsersToTrelloUsers?: string trelloListIdPrDraft?: string @@ -14,7 +15,6 @@ export interface Conf { trelloConflictingLabels?: string[] trelloRemoveUnrelatedMembers?: boolean trelloAddLabelsToCards?: boolean - trelloEnableNewCardCommand?: boolean } export type PR = Exclude From f97b2783574e51c709f133138c25ab42a731184a Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Sat, 23 Dec 2023 11:01:56 +0200 Subject: [PATCH 06/10] Fix card link replacement --- dist/index.js | 4 ++-- src/main.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8e48623..7c82dcb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33347,8 +33347,8 @@ async function createNewCard(conf, pr) { const listId = pr.state === 'open' && isDraft ? conf.trelloListIdPrDraft : conf.trelloListIdPrOpen; const commandRegex = /(^|\s)\/new-trello-card(\s|$)/; // Avoids matching URLs if (listId && pr.body && commandRegex.test(pr.body)) { - const card = await (0, trelloRequests_1.createCard)(listId, pr.title, pr.body.replace(commandRegex, '')); - await (0, githubRequests_1.updatePullRequestBody)(pr.body.replace(commandRegex, card.url)); + const card = await (0, trelloRequests_1.createCard)(listId, pr.title, pr.body.replace('/new-trello-card', '')); + await (0, githubRequests_1.updatePullRequestBody)(pr.body.replace('/new-trello-card', card.url)); return card.id; } return; diff --git a/src/main.ts b/src/main.ts index 8c50eff..b1c8dda 100644 --- a/src/main.ts +++ b/src/main.ts @@ -115,8 +115,8 @@ async function createNewCard(conf: Conf, pr: PR) { const commandRegex = /(^|\s)\/new-trello-card(\s|$)/ // Avoids matching URLs if (listId && pr.body && commandRegex.test(pr.body)) { - const card = await createCard(listId, pr.title, pr.body.replace(commandRegex, '')) - await updatePullRequestBody(pr.body.replace(commandRegex, card.url)) + const card = await createCard(listId, pr.title, pr.body.replace('/new-trello-card', '')) + await updatePullRequestBody(pr.body.replace('/new-trello-card', card.url)) return card.id } From 1d6598214b661f0ecaa8a8221aed6b46a0c48de1 Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Sat, 23 Dec 2023 17:47:09 +0200 Subject: [PATCH 07/10] Try to avoid commenting card when creating a new card --- dist/index.js | 21 +++++++++++++-------- src/githubRequests.ts | 4 ++-- src/main.test.ts | 12 ++++++------ src/main.ts | 15 +++++++++++---- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/dist/index.js b/dist/index.js index 7c82dcb..cc754a0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33154,7 +33154,7 @@ function wrappy (fn, cb) { "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.updatePullRequestBody = exports.createComment = exports.getBranchName = exports.getPullRequestAssignees = exports.getPullRequestComments = void 0; +exports.updatePullRequestBody = exports.createComment = exports.getBranchName = exports.getPullRequest = exports.getPullRequestComments = void 0; const core_1 = __nccwpck_require__(2186); const github_1 = __nccwpck_require__(5438); const githubToken = (0, core_1.getInput)('github-token', { required: true }); @@ -33171,15 +33171,15 @@ async function getPullRequestComments() { return response.data; } exports.getPullRequestComments = getPullRequestComments; -async function getPullRequestAssignees() { +async function getPullRequest() { const response = await octokit.rest.issues.get({ owner: repoOwner, repo: payload.repository.name, issue_number: issueNumber, }); - return [...(response.data.assignees || []), response.data.user]; + return response.data; } -exports.getPullRequestAssignees = getPullRequestAssignees; +exports.getPullRequest = getPullRequest; async function getBranchName() { const response = await octokit.rest.pulls.get({ owner: repoOwner, @@ -33282,7 +33282,7 @@ async function run(pr, conf = {}) { if (cardIds.length) { await moveCards(conf, cardIds, pr); await addPRLinkToCards(cardIds, pr.html_url || pr.url); - await addCardLinkToPR(conf, cardIds, pr.body, comments); + await addCardLinkToPR(conf, cardIds, comments); await addLabelToCards(conf, cardIds, pr.head); await updateCardMembers(conf, cardIds); } @@ -33424,11 +33424,12 @@ async function addPRLinkToCards(cardIds, link) { return (0, trelloRequests_1.addAttachmentToCard)(cardId, link); })); } -async function addCardLinkToPR(conf, cardIds, prBody = '', comments = []) { +async function addCardLinkToPR(conf, cardIds, comments = []) { if (!conf.githubIncludePrBranchName) { return; } - if (matchCardIds(conf, prBody || '')?.length) { + const pr = await (0, githubRequests_1.getPullRequest)(); + if (matchCardIds(conf, pr.body || '')?.length) { console.log('Card is already linked in the PR description'); return; } @@ -33443,7 +33444,7 @@ async function addCardLinkToPR(conf, cardIds, prBody = '', comments = []) { await (0, githubRequests_1.createComment)(cardInfo.shortUrl); } async function updateCardMembers(conf, cardIds) { - const assignees = await (0, githubRequests_1.getPullRequestAssignees)(); + const assignees = await getPullRequestAssignees(); console.log('Starting to update card members'); if (!assignees?.length) { console.log('No PR assignees found'); @@ -33463,6 +33464,10 @@ async function updateCardMembers(conf, cardIds) { } })); } +async function getPullRequestAssignees() { + const pr = await (0, githubRequests_1.getPullRequest)(); + return [...(pr.assignees || []), pr.user]; +} async function getTrelloMemberId(conf, githubUserName) { let username = githubUserName?.replace('-', '_'); if (conf.githubUsersToTrelloUsers?.trim()) { diff --git a/src/githubRequests.ts b/src/githubRequests.ts index 6ae243c..a1490fc 100644 --- a/src/githubRequests.ts +++ b/src/githubRequests.ts @@ -18,14 +18,14 @@ export async function getPullRequestComments() { return response.data } -export async function getPullRequestAssignees() { +export async function getPullRequest() { const response = await octokit.rest.issues.get({ owner: repoOwner, repo: payload.repository!.name, issue_number: issueNumber!, }) - return [...(response.data.assignees || []), response.data.user] + return response.data } export async function getBranchName() { diff --git a/src/main.test.ts b/src/main.test.ts index a21ab78..a1ef100 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -13,14 +13,14 @@ import { addLabelToCard, getBoardLists, } from './trelloRequests' -import { getPullRequestComments, getBranchName, createComment, getPullRequestAssignees } from './githubRequests' +import { getPullRequestComments, getBranchName, createComment, getPullRequest } from './githubRequests' jest.mock('@actions/core') jest.mock('@actions/github') jest.mock('./githubRequests') jest.mock('./trelloRequests') -const getPullRequestAssigneesMock = getPullRequestAssignees as jest.Mock +const getPullRequestMock = getPullRequest as jest.Mock const getMemberInfoMock = getMemberInfo as jest.Mock const getCardInfoMock = getCardInfo as jest.Mock const getPullRequestCommentsMock = getPullRequestComments as jest.Mock @@ -222,7 +222,7 @@ describe('Updating card members', () => { const conf = { githubUsersToTrelloUsers: 'jack: jones\namy: amy1993', trelloRemoveUnrelatedMembers: true } it('adds PR author and assignees to the card and removes unrelated members', async () => { - getPullRequestAssigneesMock.mockResolvedValueOnce([{ login: 'phil' }, { login: 'amy' }]) + getPullRequestMock.mockResolvedValueOnce({ author: { login: 'phil' }, assignees: [{ login: 'amy' }] }) getMemberInfoMock.mockImplementation((username) => username === 'amy1993' ? { id: 'amy-id' } : { id: 'phil-id' }, ) @@ -236,7 +236,7 @@ describe('Updating card members', () => { }) it('skips removing unrelated members when turned off', async () => { - getPullRequestAssigneesMock.mockResolvedValueOnce([{ login: 'phil' }]) + getPullRequestMock.mockResolvedValueOnce({ author: { login: 'phil' } }) getMemberInfoMock.mockResolvedValueOnce({ id: 'phil-id' }) getCardInfoMock.mockResolvedValueOnce({ id: 'card', idMembers: ['jones-id'] }) @@ -246,7 +246,7 @@ describe('Updating card members', () => { }) it('skips adding when all members are already assigned to the card', async () => { - getPullRequestAssigneesMock.mockResolvedValueOnce([{ login: 'phil' }]) + getPullRequestMock.mockResolvedValueOnce({ author: { login: 'phil' } }) getMemberInfoMock.mockResolvedValueOnce({ id: 'phil-id' }) getCardInfoMock.mockResolvedValueOnce({ id: 'card', idMembers: ['phil-id'] }) @@ -256,7 +256,7 @@ describe('Updating card members', () => { }) it('skips adding when member not found with GitHub username', async () => { - getPullRequestAssigneesMock.mockResolvedValueOnce([{ login: 'phil' }]) + getPullRequestMock.mockResolvedValueOnce({ author: { login: 'phil' } }) getMemberInfoMock.mockResolvedValue(undefined) await run(pr) diff --git a/src/main.ts b/src/main.ts index b1c8dda..6af03ef 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import { setFailed } from '@actions/core' import { createComment, getBranchName, - getPullRequestAssignees, + getPullRequest, getPullRequestComments, updatePullRequestBody, } from './githubRequests' @@ -30,7 +30,7 @@ export async function run(pr: PR, conf: Conf = {}) { if (cardIds.length) { await moveCards(conf, cardIds, pr) await addPRLinkToCards(cardIds, pr.html_url || pr.url) - await addCardLinkToPR(conf, cardIds, pr.body, comments) + await addCardLinkToPR(conf, cardIds, comments) await addLabelToCards(conf, cardIds, pr.head) await updateCardMembers(conf, cardIds) } @@ -217,12 +217,13 @@ async function addPRLinkToCards(cardIds: string[], link: string) { ) } -async function addCardLinkToPR(conf: Conf, cardIds: string[], prBody: string = '', comments: { body?: string }[] = []) { +async function addCardLinkToPR(conf: Conf, cardIds: string[], comments: { body?: string }[] = []) { if (!conf.githubIncludePrBranchName) { return } + const pr = await getPullRequest() - if (matchCardIds(conf, prBody || '')?.length) { + if (matchCardIds(conf, pr.body || '')?.length) { console.log('Card is already linked in the PR description') return @@ -274,6 +275,12 @@ async function updateCardMembers(conf: Conf, cardIds: string[]) { ) } +async function getPullRequestAssignees() { + const pr = await getPullRequest() + + return [...(pr.assignees || []), pr.user] +} + async function getTrelloMemberId(conf: Conf, githubUserName?: string) { let username = githubUserName?.replace('-', '_') if (conf.githubUsersToTrelloUsers?.trim()) { From 5f501f4eec192436dc4b928a3865e60e376b4f08 Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Sat, 23 Dec 2023 18:09:07 +0200 Subject: [PATCH 08/10] Refactor and fix tests --- src/main.test.ts | 42 ++++++++++++++++++++++++++++++++++++++++-- src/main.ts | 11 ++++++----- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/main.test.ts b/src/main.test.ts index a1ef100..45cc9c4 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -12,8 +12,15 @@ import { getBoardLabels, addLabelToCard, getBoardLists, + createCard, } from './trelloRequests' -import { getPullRequestComments, getBranchName, createComment, getPullRequest } from './githubRequests' +import { + getPullRequestComments, + getBranchName, + createComment, + getPullRequest, + updatePullRequestBody, +} from './githubRequests' jest.mock('@actions/core') jest.mock('@actions/github') @@ -29,6 +36,7 @@ const searchTrelloCardsMock = searchTrelloCards as jest.Mock const getCardAttachmentsMock = getCardAttachments as jest.Mock const getBoardLabelsMock = getBoardLabels as jest.Mock const getBoardListsMock = getBoardLists as jest.Mock +const createCardMock = createCard as jest.Mock const basePR = { number: 0, state: 'open', title: 'Title' } @@ -87,6 +95,36 @@ describe('Finding cards', () => { }) }) +describe('Creating new card', () => { + const pr = { ...basePR, body: '/new-trello-card Description' } + const conf = { trelloListIdPrOpen: 'open-list-id', githubIncludeNewCardCommand: true } + + it('adds new card, updates PR body and adds to card ids list', async () => { + createCardMock.mockResolvedValueOnce({ id: 'card-id', url: 'card-url' }) + + await run(pr, conf) + + expect(createCard).toHaveBeenCalledWith('open-list-id', 'Title', ' Description') + expect(updatePullRequestBody).toHaveBeenCalledWith('card-url Description') + expect(moveCardToList).toHaveBeenCalledWith('card-id', 'open-list-id', undefined) + }) + + it('skips when no command found', async () => { + await run({ ...pr, body: '' }, conf) + expect(createCard).not.toHaveBeenCalled() + }) + + it('skips when list is missing', async () => { + await run(pr, { ...conf, trelloListIdPrOpen: '' }) + expect(createCard).not.toHaveBeenCalled() + }) + + it('skips when turned off', async () => { + await run(pr, { ...conf, githubIncludeNewCardCommand: false }) + expect(createCard).not.toHaveBeenCalled() + }) +}) + describe('Moving cards', () => { describe('PR is added to draft', () => { const pr = { ...basePR, body: 'https://trello.com/c/card/title' } @@ -222,7 +260,7 @@ describe('Updating card members', () => { const conf = { githubUsersToTrelloUsers: 'jack: jones\namy: amy1993', trelloRemoveUnrelatedMembers: true } it('adds PR author and assignees to the card and removes unrelated members', async () => { - getPullRequestMock.mockResolvedValueOnce({ author: { login: 'phil' }, assignees: [{ login: 'amy' }] }) + getPullRequestMock.mockResolvedValueOnce({ user: { login: 'phil' }, assignees: [{ login: 'amy' }] }) getMemberInfoMock.mockImplementation((username) => username === 'amy1993' ? { id: 'amy-id' } : { id: 'phil-id' }, ) diff --git a/src/main.ts b/src/main.ts index 6af03ef..b0d728f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -30,7 +30,7 @@ export async function run(pr: PR, conf: Conf = {}) { if (cardIds.length) { await moveCards(conf, cardIds, pr) await addPRLinkToCards(cardIds, pr.html_url || pr.url) - await addCardLinkToPR(conf, cardIds, comments) + await addCardLinkToPR(conf, cardIds, pr, comments) await addLabelToCards(conf, cardIds, pr.head) await updateCardMembers(conf, cardIds) } @@ -217,13 +217,13 @@ async function addPRLinkToCards(cardIds: string[], link: string) { ) } -async function addCardLinkToPR(conf: Conf, cardIds: string[], comments: { body?: string }[] = []) { +async function addCardLinkToPR(conf: Conf, cardIds: string[], pr: PR, comments: { body?: string }[] = []) { if (!conf.githubIncludePrBranchName) { return } - const pr = await getPullRequest() + const pullRequest = conf.githubIncludeNewCardCommand ? await getPullRequest() : pr - if (matchCardIds(conf, pr.body || '')?.length) { + if (matchCardIds(conf, pullRequest.body || '')?.length) { console.log('Card is already linked in the PR description') return @@ -278,11 +278,12 @@ async function updateCardMembers(conf: Conf, cardIds: string[]) { async function getPullRequestAssignees() { const pr = await getPullRequest() - return [...(pr.assignees || []), pr.user] + return pr ? [...(pr.assignees || []), pr.user] : [] } async function getTrelloMemberId(conf: Conf, githubUserName?: string) { let username = githubUserName?.replace('-', '_') + if (conf.githubUsersToTrelloUsers?.trim()) { username = getTrelloUsernameFromInputMap(conf, githubUserName) || username } From d4c4b9fd144739b5ee08ea1dc8c14bcbef183e3a Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Sat, 23 Dec 2023 18:10:14 +0200 Subject: [PATCH 09/10] Update README and build --- README.md | 2 +- dist/index.js | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6f52cfe..537f531 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This action scans PR description and comments for Trello card URL(s) or branch n - Assigns a PR author and fellow assignees to a Trello card. - And more... -You can also optionally create new Trello cards by adding `/new-trello-card` in the PR description. +You can also optionally use the action to create new Trello card by adding `/new-trello-card` in the PR description. ## Basic configuration diff --git a/dist/index.js b/dist/index.js index cc754a0..de28a08 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33282,7 +33282,7 @@ async function run(pr, conf = {}) { if (cardIds.length) { await moveCards(conf, cardIds, pr); await addPRLinkToCards(cardIds, pr.html_url || pr.url); - await addCardLinkToPR(conf, cardIds, comments); + await addCardLinkToPR(conf, cardIds, pr, comments); await addLabelToCards(conf, cardIds, pr.head); await updateCardMembers(conf, cardIds); } @@ -33424,12 +33424,12 @@ async function addPRLinkToCards(cardIds, link) { return (0, trelloRequests_1.addAttachmentToCard)(cardId, link); })); } -async function addCardLinkToPR(conf, cardIds, comments = []) { +async function addCardLinkToPR(conf, cardIds, pr, comments = []) { if (!conf.githubIncludePrBranchName) { return; } - const pr = await (0, githubRequests_1.getPullRequest)(); - if (matchCardIds(conf, pr.body || '')?.length) { + const pullRequest = conf.githubIncludeNewCardCommand ? await (0, githubRequests_1.getPullRequest)() : pr; + if (matchCardIds(conf, pullRequest.body || '')?.length) { console.log('Card is already linked in the PR description'); return; } @@ -33466,7 +33466,7 @@ async function updateCardMembers(conf, cardIds) { } async function getPullRequestAssignees() { const pr = await (0, githubRequests_1.getPullRequest)(); - return [...(pr.assignees || []), pr.user]; + return pr ? [...(pr.assignees || []), pr.user] : []; } async function getTrelloMemberId(conf, githubUserName) { let username = githubUserName?.replace('-', '_'); From c89dab62a343b48ec6fae3a7e8a16ee2faa8c415 Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Sat, 23 Dec 2023 18:11:18 +0200 Subject: [PATCH 10/10] Fix test --- src/main.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.test.ts b/src/main.test.ts index 45cc9c4..d226b27 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -274,7 +274,7 @@ describe('Updating card members', () => { }) it('skips removing unrelated members when turned off', async () => { - getPullRequestMock.mockResolvedValueOnce({ author: { login: 'phil' } }) + getPullRequestMock.mockResolvedValueOnce({ user: { login: 'phil' } }) getMemberInfoMock.mockResolvedValueOnce({ id: 'phil-id' }) getCardInfoMock.mockResolvedValueOnce({ id: 'card', idMembers: ['jones-id'] }) @@ -284,7 +284,7 @@ describe('Updating card members', () => { }) it('skips adding when all members are already assigned to the card', async () => { - getPullRequestMock.mockResolvedValueOnce({ author: { login: 'phil' } }) + getPullRequestMock.mockResolvedValueOnce({ user: { login: 'phil' } }) getMemberInfoMock.mockResolvedValueOnce({ id: 'phil-id' }) getCardInfoMock.mockResolvedValueOnce({ id: 'card', idMembers: ['phil-id'] }) @@ -294,7 +294,7 @@ describe('Updating card members', () => { }) it('skips adding when member not found with GitHub username', async () => { - getPullRequestMock.mockResolvedValueOnce({ author: { login: 'phil' } }) + getPullRequestMock.mockResolvedValueOnce({ user: { login: 'phil' } }) getMemberInfoMock.mockResolvedValue(undefined) await run(pr)