Skip to content

Commit

Permalink
Merge pull request #3 from brianhenryhf/allow-trello-links-in-body
Browse files Browse the repository at this point in the history
Allow trello links in body
  • Loading branch information
Brian Henry authored Feb 17, 2020
2 parents 85014f0 + 751269a commit 5e46760
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- name: Confirm Link
id: confirm
uses: delivered/trello-github-action/@MM-first-pr
uses: delivered/trello-github-action/@master
with:
link-regex: '(https:\/\/trello.com[^\)]*)'
repo-token: ${{ secrets.GITHUB_TOKEN }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
54 changes: 36 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,58 @@ const github = require('@actions/github');

async function run() {
try {
// `who-to-greet` input defined in action metadata file
const linkRegExInput = core.getInput('link-regex');
const linkRegExp = new RegExp(linkRegExInput)

//matching in pr comments OR body of pr
let matchedStrings = [];

console.log(`Checking for links: ${linkRegExInput}!`);

const time = (new Date()).toTimeString();
const token = core.getInput('repo-token');
const octokit = new github.GitHub(token);
const payload = github.context.payload;

const prComments = await octokit.issues.listComments({
owner: payload.organization.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number
});

const commentsWithLinks = prComments.data.filter(d => linkRegExp.exec(d.body) && linkRegExp.exec(d.body).length > 0).map(i => linkRegExp.exec(i.body)[0])

console.log('all prComments:')
console.log(prComments.data.map(i => i.body));
// personal repos have no org - allow for those as well as team ones
const owner = (payload.organization || payload.repository.owner).login;
const repo = payload.repository.name;

// issue events (like create comment) supply #issue instead of #pull_request - handle both
const issue_number = (payload.pull_request || payload.issue).number;

const issuesArgs = { owner, repo, issue_number };

const pull = await octokit.issues.get(issuesArgs);
console.log(`pr body: ${pull.data.body}`);

const bodyMatches = linkRegExp.exec(pull.data.body);
if(bodyMatches) matchedStrings.push(bodyMatches[0]);

if(matchedStrings.length < 1) {
const prComments = await octokit.issues.listComments(issuesArgs);

const commentsWithLinks = prComments.data.reduce((acc, curr) => {
const matches = linkRegExp.exec(curr.body);
if(matches) acc.push(matches[0]);
return acc;
}, []);

matchedStrings = matchedStrings.concat(commentsWithLinks);

console.log('all prComments:')
console.log(prComments.data.map(i => i.body));
}

console.log('matches:')
console.log(commentsWithLinks);

core.setOutput('msg', `Found ${commentsWithLinks.length} with matching links`)
console.log(matchedStrings);

if(commentsWithLinks.length === 0)
core.setFailed(`unable to find any comments matching link ${linkRegExInput}`)
core.setOutput('msg', `Found ${matchedStrings.length} with matching links`)

if(matchedStrings.length === 0) core.setFailed(`unable to find any comments matching link ${linkRegExInput}`)
} catch (error) {
console.log(error);
core.setFailed(error.message);
}
}
}

run();

0 comments on commit 5e46760

Please sign in to comment.