Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from delivered/skip-irrelevant-event
Browse files Browse the repository at this point in the history
Skip irrelevant event
  • Loading branch information
Brian Henry authored Feb 24, 2020
2 parents 32f370e + c02fb50 commit 68a19e5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
# Attach to Trello Card action

This action looks for a Trello card URL in the start of a Pull Request description. If found, will push an attachment to Trello (for use by Trello Github Power-Up). Optionally, this can be configured to also attach a (redundant) PR comment with link/name, similar to what the Trello Power-up will do, for actions that expect that to be present.
The purpose of this action is to enable attaching a pull request to a Trello card, from within the PR body. This is best used with the Trello [Github Power-Up](https://trello.com/power-ups/55a5d916446f517774210004) added to your Trello Boards. With that enabled, this effectively enables the "Attach Pull Request" action of the Power-Up, but from the Github side (and via a quick URL copy-paste instead of clicking through menus).

The action looks for a Trello card URL at the start of a Pull Request description. If found, it will add the PR URL as an attachment to Trello.

Optionally, this can be configured to also attach a (redundant) PR comment with link/name, similar to what the Trello Power-up will do, for use cases requiring that.


## Events

Workflows using this action should include some/all of the following supported event types:
- `pull_request.opened`
- `pull_request.reopened`
- `pull_request.edited`

Unsupported actions are ignored.


## Inputs/Outputs

This requires Trello key+token to be supplied from workflow where used (The token, at least, should come from repo Secrets).
This requires Trello key and token to be supplied from the workflow where used. The token, at least, should come from repo Secrets.

There are no outputs.


## Example workflow config:
```yml
Expand All @@ -21,6 +39,7 @@ jobs:
with:
trello-key: ${{ secrets.TRELLO_KEY }}
trello-token: ${{ secrets.TRELLO_TOKEN }}

## optional
# add-pr-comment: true
## required if add-pr-comment is true. secrets.GITHUB_TOKEN is supplied by GH action implicitly.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ inputs:
runs:
using: 'node12'
main: 'index.js'
branding:
icon: 'paperclip'
color: 'blue'
42 changes: 26 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const util = require('util');
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios');

const supportedEvent = 'pull_request';
const supportedActions = ['opened', 'reopened', 'edited'];

//configured in workflow file, which in turn should use repo secrets settings
const trelloKey = core.getInput('trello-key');
const trelloToken = core.getInput('trello-token');
const trelloKey = core.getInput('trello-key', { required: true });
const trelloToken = core.getInput('trello-token', { required: true });
//adds extra (redundant) PR comment, to mimic normal behavior of trello GH powerup
const shouldAddPrComment = core.getInput('add-pr-comment') === 'true';
//token is NOT magically present in context as some docs seem to indicate - have to supply in workflow yaml to input var
Expand All @@ -30,14 +34,14 @@ const requestTrello = async (verb, url, body = null, extraParams = null) => {
data: body || {},
params: params
});
console.log(`${verb} to ${url} completed with status: ${res.status}. data follows:`);
console.dir(res.data);
core.debug(`${verb} to ${url} completed with status: ${res.status}. data follows:`);
//BRH NOTE core.xxx logging methods explode with typeerror when given non-string object. TODO wrap.
core.debug(util.inspect(res.data));
return res.data;
} catch(err) {
console.log(`${verb} to ${url} errored: ${err}`);
core.error(`${verb} to ${url} errored: ${err}`);
if(err.response) {
//console.log(`status: ${err.status}. error data follows:`);
console.dir(err.response.data);
core.error(util.inspect(err.response.data));
}
throw err;
}
Expand Down Expand Up @@ -77,12 +81,12 @@ const addPrComment = async (body) => {


const extractTrelloCardId = (prBody) => {
console.log(`pr body: ${prBody}`);
core.debug(`pr body: ${prBody}`);

//find 1st instance of trello card url - must be 1st thing in PR
const matches = /^\s*https\:\/\/trello\.com\/c\/(\w+)/.exec(prBody);
const cardId = matches && matches[1];
console.log(`card id = ${cardId}`);
core.debug(`card id = ${cardId}`);

return cardId;
}
Expand All @@ -102,38 +106,44 @@ const buildTrelloLinkComment = async (cardId) => {

(async () => {
try {
if(!(github.context.eventName === supportedEvent && supportedActions.some(el => el === evthookPayload.action))) {
core.info(`event/type not supported: ${github.context.eventName.eventName}.${evthookPayload.action}. skipping action.`);
return;
}

const cardId = extractTrelloCardId(evthookPayload.pull_request.body);
const prUrl = evthookPayload.pull_request.html_url;

if(cardId) {
let extantAttachments;

console.log(`card url for ${cardId} specified in pr comment.`);
core.debug(`card url for ${cardId} specified in pr comment.`);
extantAttachments = await getCardAttachments(cardId);

//make sure not already attached
if(extantAttachments == null || !extantAttachments.some(it => it.url === prUrl)) {
const createdAttachment = await createCardAttachment(cardId, prUrl);
console.log(`created trello attachment: ${JSON.stringify(createdAttachment)}`);
core.info(`created trello attachment.`);
core.debug(util.inspect(createdAttachment));

// BRH NOTE actually, the power-up doesn't check if it previously added comment, so check is maybe superfluous
if(shouldAddPrComment && !await commentsContainsTrelloLink(cardId)) {
console.log('adding pr comment');
core.debug('adding pr comment');
const newComment = await buildTrelloLinkComment(cardId)

//comments as 'github actions' bot, at least when using token automatically generated for GH workflows
await addPrComment(newComment);
} else {
console.log('pr comment present or unwanted - skipping add');
core.info('pr comment already present or unwanted - skipped comment add.');
}
} else {
console.log('trello attachement already exists. skipped create.');
core.info('trello attachement already exists - skipped create.');
}
} else {
console.log(`no card url in pr comment. nothing to do`);
core.info(`no card url in pr comment. nothing to do.`);
}
} catch (error) {
console.error(error);
core.error(util.inspect(error));
//failure will stop PR from being mergeable if that setting enabled on the repo. there is not currently a neutral exit in actions v2.
core.setFailed(error.message);
}
Expand Down

0 comments on commit 68a19e5

Please sign in to comment.