Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gcornut committed Jun 16, 2023
1 parent 024ef23 commit 5ecb6dd
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 57 deletions.
75 changes: 20 additions & 55 deletions .github/actions/update-pr-body/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ description: 'Update PR body with pattern replace'

inputs:
pattern:
description: 'Pattern to match'
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'
description: |
String to replace the pattern.
Can contain capture groups (`$1` to insert the first capture from the regexp pattern)
required: true
whenNotFound:
description: |
Expand All @@ -15,7 +19,11 @@ inputs:
Set to "ignore" to skip.
Else an error is thrown.
skipReplaceIf:
description: 'Skip replace if this expression returns true'
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
Expand All @@ -27,55 +35,12 @@ inputs:
runs:
using: composite
steps:
- name: Update PR body
uses: actions/github-script@v6
with:
script: |
const pattern = new RegExp('(?<=\n)${{ inputs.pattern }}', 'g');
const replace = '${{ inputs.replace }}';
const whenNotFound = '${{ inputs.whenNotFound }}';
const skipReplaceIf = '${{ inputs.skipReplaceIf }}';
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];
console.log({ pattern, body, matches });
let updatedBody = body;
if (!lastMatch) {
if (whenNotFound === "append") {
updatedBody += `\n\n${replace}`;
} else if (whenNotFound !== "ignore") {
throw new Error(`No match found for pattern '${pattern}'`);
}
} else {
updatedBody = body.replaceAll(
pattern,
(...groups) => {
groups.pop()
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)) return match;
// Replace $1, $2, etc. with captured elements
return captureGroups.reduce((a, b, i) => a.replace(`$${i+1}`, b), replace);
}
// Else remove the match
return '';
},
);
}
// Update PR body
await github.rest.pulls.update({
owner,
repo,
pull_number,
body: updatedBody
});
- name: Update PR body
uses: actions/github-script@v6
with:
script: |
await require('.github/actions/update-pr-body')({
inputs: ${{toJSON(inputs)}},
github,
context,
});
59 changes: 59 additions & 0 deletions .github/actions/update-pr-body/index.js
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,
});
};
4 changes: 2 additions & 2 deletions .github/workflows/deploy-chromatic-pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
// StoryBook URL
const utils = require('./.github/actions/utils');
const storybook_url = await utils.getStoryBookURL('${{github.sha}}');
const storybook_url = await utils.getStoryBookURL('${{github.event.pull_request.head.sha}}');
return { run_html_url, storybook_url };
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:

- name: "Get current short SHA"
id: short_sha
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
run: echo "short_sha=$(git rev-parse --short ${{github.event.pull_request.head.sha}})" >> $GITHUB_OUTPUT

- name: "Mark storybook as outdated"
uses: ./.github/actions/update-pr-body
Expand Down

0 comments on commit 5ecb6dd

Please sign in to comment.