-
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.
Merge pull request #995 from lumapps/chore/improve-deploy-chromatic
chore(ci): improve chromatic deploy
- Loading branch information
Showing
6 changed files
with
215 additions
and
13 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,47 @@ | ||
name: 'Update PR body' | ||
description: 'Update PR body with pattern replace' | ||
|
||
inputs: | ||
pattern: | ||
description: | | ||
RexExp pattern to match. | ||
This can only match on the beginning of a line in the PR body | ||
required: true | ||
replace: | ||
description: | | ||
String to replace the pattern. | ||
Can contain capture groups (`$1` to insert the first capture from the regexp pattern) | ||
required: true | ||
keepLastMatch: | ||
description: | | ||
Set to true to keep only the last match of the pattern and remove the others. | ||
whenNotFound: | ||
description: | | ||
Change behavior when the pattern is not found. | ||
Set to "append" to append the replace string to the end. | ||
Set to "ignore" to skip. | ||
Else an error is thrown. | ||
skipReplaceIf: | ||
description: | | ||
Skip replace if this expression returns true. | ||
This can be used to check a match before replacing it. | ||
Example: `skipReplaceIf: "groups[1] === 'foo'"` | ||
=> Will skip replacing the match if the first capture group is 'foo'. | ||
# Example: | ||
# - name: Update PR Body | ||
# uses: ./.github/actions/update-pr-body-action | ||
# with: | ||
# pattern: 'Storybook: .*' | ||
# replace: 'StoryBook: https://example.com' | ||
|
||
runs: | ||
using: composite | ||
steps: | ||
- name: Update PR body | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
require("${{ github.action_path }}")({ | ||
github, context, inputs: ${{ toJSON(inputs) }} | ||
}) |
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,71 @@ | ||
/** | ||
* Match a pattern in the PR body, replace with a replace pattern. | ||
* If the pattern as multiple matches, all but the last one are removed and the last one is replaced. | ||
* | ||
* Can fall back to appending the replace pattern to the end of the body if the pattern is not found. | ||
* Can skip replace if a condition is met on the match instance. | ||
*/ | ||
async function main({ inputs, github, context }) { | ||
const { pattern: patternString, replace, whenNotFound, skipReplaceIf, keepLastMatch } = inputs; | ||
const pattern = new RegExp(`(?<=\n)${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', { whenNotFound }); | ||
} 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(); | ||
|
||
if (keepLastMatch === 'true' && offset !== lastMatch.index) { | ||
// Not the last occurrence => removing it | ||
return ''; | ||
} | ||
|
||
const [match, ...captureGroups] = groups; | ||
// Skip if condition met | ||
if (skipReplaceIf && eval(skipReplaceIf)) { | ||
console.log(`Condition "${skipReplaceIf}" met.`); | ||
console.log(` 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}"`); | ||
console.log(` With replace pattern: "${replace}"`); | ||
if (replaceText !== replace) | ||
console.log(` Replace pattern resolved as: "${replaceText}"`); | ||
return replaceText; | ||
}, | ||
); | ||
} | ||
|
||
// Update PR body | ||
await github.rest.pulls.update({ | ||
owner, | ||
repo, | ||
pull_number, | ||
body: updatedBody, | ||
}); | ||
} | ||
|
||
module.exports = main; |
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,21 @@ | ||
/** | ||
* Get short SHA for git ref. | ||
*/ | ||
async function getShortSHA(gitRef) { | ||
const util = require('util'); | ||
const exec = util.promisify(require('child_process').exec); | ||
|
||
return exec(`git rev-parse --short ${gitRef}`) | ||
.then(({ stdout }) => stdout.trim()); | ||
} | ||
|
||
/** | ||
* Generate StoryBook URL for the given git ref. | ||
*/ | ||
async function getStoryBookURL(gitRef) { | ||
const { project_id } = require('../../configs/chromatic'); | ||
const shortSHA = await getShortSHA(gitRef); | ||
return `https://${shortSHA}--${project_id}.chromatic.com/`; | ||
} | ||
|
||
module.exports = { getStoryBookURL, getShortSHA }; |
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,3 @@ | ||
module.exports = { | ||
project_id: '5fbfb1d508c0520021560f10', | ||
}; |