Last active
November 23, 2019 10:33
-
-
Save jeremylenz/d8da486cf746fdcc3a0023002d5de6dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Zapier task: "Add RM # to Asana task from PR title" | |
// This is the exact code used in the Zapier step. | |
const { taskTitle, taskNotes} = inputData; | |
// match 5 or more digit characters | |
const extractRMNum = (taskTitle) => { | |
const result = taskTitle.match(/\d{5,}/); | |
if (result) return result[0]; | |
return null; | |
} | |
// match https://github.com/{orgName}/{repoNameWithZeroOrOneHyphen}/pull/{1 or more digit chars} | |
const extractGithubPrUrl = (taskNotes) => { | |
const result = taskNotes.match(/https:\/\/github\.com\/\w+\/\w+-*\w*\/pull\/\d+/); | |
if (result) return result[0]; | |
return null; | |
} | |
// match 1 or more digit characters after pull/ | |
const extractPRNum = (hrefStr) => { | |
const result = hrefStr.match(/https:\/\/.{0,4}github\.com\/(\w+\/\w+[-_]*\w*)\/pull\/(\d+)/); | |
if (result && result.length > 1) return result[result.length - 1]; | |
return null; | |
} | |
// get full repo name from Github URL | |
const extractFullRepoName = (hrefStr) => { | |
const result = hrefStr.match(/https:\/\/.{0,4}github\.com\/(\w+\/\w+-*\w*)/); | |
if (result && result.length > 1) return result[1]; | |
return null; | |
} | |
const pullRequestInfo = (hrefStr) => { | |
const prLink = extractGithubPrUrl(hrefStr); | |
let prNum, repoName; | |
if (prLink) { | |
prNum = extractPRNum(prLink); | |
repoName = extractFullRepoName(prLink); | |
} | |
return { | |
found: (!!prLink && !!prNum && !!repoName), | |
url: prLink, | |
pullRequestNumber: prNum, | |
fullRepoName: repoName, | |
} | |
} | |
const redmineNum = extractRMNum(taskTitle); | |
if (redmineNum) return { redmineNumAlreadyExists: true }; // Only continue if RM # is NOT in the title | |
const prInfo = pullRequestInfo(taskNotes); | |
return prInfo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment