Skip to content

Commit

Permalink
fix: docs automerge workflow
Browse files Browse the repository at this point in the history
Because `docs` is now tracked, the logic to decide when to push changes
or create a new PR has changed. This code searches now for new files
instead of just checking if `package.json` has been modified.

This bug was discovered via #113
  • Loading branch information
molant committed Oct 8, 2021
1 parent b6fc413 commit 60000eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 3 additions & 1 deletion scripts/__tests__/process-doc-changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ describe('process-docs-changes', () => {
});

it('does create a PR if more files than package.json are modified', async () => {
gitMock.getChanges.mockResolvedValue('M package.json\nM sidebars.json');
gitMock.getChanges.mockResolvedValue(
'M package.json\nM sidebars.json\nU randomDoc.md'
);

await processDocsChanges();

Expand Down
26 changes: 23 additions & 3 deletions scripts/process-docs-changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

//@ts-check
if (!(process.env.CI || process.env.NODE_ENV === 'test') && !GITHUB_TOKEN) {
if (!(process.env.CI || process.env.NODE_ENV === 'development') && !GITHUB_TOKEN) {
console.error('Missing GITHUB_TOKEN environment variable');
process.exit(1);
}
Expand All @@ -32,6 +32,25 @@ const changeExitCodeIfException = async (func) => {
}
};

/**
* Checks if there are new document files by parsing the given
* `git status --porcelain` input.
* This is done by looking at the status of each file:
* - `A` means it is new and has been staged
* - `??` means it is a new file and has not been staged yet
*
* @param {string} gitOutput
*/
const newDocFiles = (gitOutput) => {
const lines = gitOutput.split('\n');
const newFiles = lines.filter((line) => {
const trimmedLine = line.trim();
return trimmedLine.startsWith('U') || trimmedLine.startsWith('??');
});

return newFiles;
};

const processDocsChanges = async () => {
const output = await getChanges();

Expand All @@ -42,9 +61,10 @@ const processDocsChanges = async () => {
console.log('package.json is not modified, skipping');
return;
} else {
const lines = output.split('\n');
if (lines.length > 1) {
const newFiles = newDocFiles(output);
if (newFiles.length > 0) {
console.log(`New documents available, creating PR.`);
console.log(`${newFiles.join('\n')}`);
await createPR(PR_BRANCH, HEAD, EMAIL, NAME, COMMIT_MESSAGE);
} else {
console.log(
Expand Down

0 comments on commit 60000eb

Please sign in to comment.