|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import simpleGit from 'simple-git/promise'; |
| 19 | +import { spawn } from 'child-process-promise'; |
| 20 | +import * as tmp from 'tmp'; |
| 21 | + |
| 22 | +/** |
| 23 | + * check if there is any doc changes from baseSha to headSha |
| 24 | + */ |
| 25 | +async function docChanged(baseSha: string, headSha: string): Promise<boolean> { |
| 26 | + const tmpDir = tmp.dirSync().name; |
| 27 | + console.log('tmpDir is', tmpDir); |
| 28 | + // clone repo to the tmpDir |
| 29 | + await spawn( |
| 30 | + 'git', |
| 31 | + ['clone', 'https://github.com/firebase/firebase-js-sdk.git', tmpDir], |
| 32 | + { stdio: 'inherit' } |
| 33 | + ); |
| 34 | + |
| 35 | + const git = simpleGit(tmpDir); |
| 36 | + |
| 37 | + // generate reference docs using the baseSha |
| 38 | + await git.checkout(baseSha); |
| 39 | + await spawn('yarn', [], { cwd: tmpDir, stdio: 'inherit' }); |
| 40 | + await spawn('yarn', ['docgen:exp', 'devsite'], { |
| 41 | + cwd: tmpDir, |
| 42 | + stdio: 'inherit' |
| 43 | + }); |
| 44 | + |
| 45 | + // revert yarn.lock, so we can checkout the headSha without conflict. |
| 46 | + await git.checkout('yarn.lock'); |
| 47 | + // stash changes, so we can checkout the headSha without conflict. |
| 48 | + await git.stash(); |
| 49 | + |
| 50 | + // generate reference docs using the headSha |
| 51 | + await git.checkout(headSha); |
| 52 | + await spawn('yarn', [], { cwd: tmpDir, stdio: 'inherit' }); |
| 53 | + |
| 54 | + // stage the docs generated by baseSha, so we can use git diff to tell if any docs are different in the headSha |
| 55 | + await git.stash(['pop']); |
| 56 | + await git.add('*'); |
| 57 | + await spawn('yarn', ['docgen:exp', 'devsite'], { |
| 58 | + cwd: tmpDir, |
| 59 | + stdio: 'inherit' |
| 60 | + }); |
| 61 | + |
| 62 | + // check if any file is changed. |
| 63 | + const diff = await git.diff(); |
| 64 | + // check if new files are created. |
| 65 | + const untracked = await git.raw([ |
| 66 | + 'ls-files', |
| 67 | + '-o', |
| 68 | + '--directory', |
| 69 | + '--exclude-standard' |
| 70 | + ]); |
| 71 | + |
| 72 | + return !!diff || !!untracked; |
| 73 | +} |
| 74 | + |
| 75 | +// this function is supposed to run in the Github action. |
| 76 | +async function runInCI() { |
| 77 | + const headSha = process.env.GITHUB_PULL_REQUEST_HEAD_SHA; |
| 78 | + const mergeBaseSha = process.env.GITHUB_PULL_REQUEST_BASE_SHA; |
| 79 | + |
| 80 | + if (!headSha || !mergeBaseSha) { |
| 81 | + console.log('No merge base or head found. Is it not a PR?'); |
| 82 | + process.exit(1); |
| 83 | + } |
| 84 | + |
| 85 | + if (await docChanged(mergeBaseSha, headSha)) { |
| 86 | + console.log(`::set-output name=DOC_CHANGED::true`); |
| 87 | + } else { |
| 88 | + console.log(`::set-output name=DOC_CHANGED::false`); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +runInCI(); |
0 commit comments