-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
57 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module.exports = async ({ inputs, github, context }) => { | ||
const { pattern: patternString, replace, whenNotFound, skipReplaceIf } = inputs; | ||
const pattern = new RegExp(`(?<= | ||
)${patternString}`, 'g'); | ||
|
||
const { owner, repo } = context.repo; | ||
const pull_number = context.issue.number; | ||
|
||
const body = context.payload.pull_request.body; | ||
const matches = Array.from(body.matchAll(pattern)); | ||
const lastMatch = matches[matches.length - 1]; | ||
|
||
let updatedBody = body; | ||
if (!lastMatch) { | ||
console.log(`Pattern ${pattern} does not match`); | ||
if (whenNotFound === 'append') { | ||
console.log('Appending replace string to the end of the body'); | ||
updatedBody += `\n\n${replace}`; | ||
} else if (whenNotFound !== 'ignore') { | ||
throw new Error(`No match found for pattern '${pattern}'`); | ||
} | ||
console.log('Nothing to do'); | ||
} else { | ||
console.log(`Pattern ${pattern} matches`); | ||
updatedBody = body.replaceAll( | ||
pattern, | ||
(...groups) => { | ||
// Remove last arg (the body) | ||
groups.pop(); | ||
// Get match offset (second to last arg) | ||
const offset = groups.pop(); | ||
// Replace the last match with the replace string | ||
if (offset === lastMatch.index) { | ||
//console.log('matches:', { groups }); | ||
const [match, ...captureGroups] = groups; | ||
// Skip if condition met | ||
if (skipReplaceIf && eval(skipReplaceIf)) { | ||
console.log(`Condition "${skipReplaceIf}" met. Not replacing the match "${match}"`); | ||
return match; | ||
} | ||
// Replace $1, $2, etc. with captured elements | ||
const replaceText = captureGroups.reduce((a, b, i) => a.replace(`$${i + 1}`, b), replace); | ||
console.log(`Replacing match: "${match}"\nWith replace pattern: "${replace}"\nResolved to "${replaceText}"`); | ||
return replaceText; | ||
} | ||
// Else remove the match | ||
return ''; | ||
}, | ||
); | ||
} | ||
|
||
// Update PR body | ||
await github.rest.pulls.update({ | ||
owner, | ||
repo, | ||
pull_number, | ||
body: updatedBody, | ||
}); | ||
}; |
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