({repo, assetsPath, destPath})
| 25 | import {GithubClient} from './github-client.mjs'; |
| 26 | |
| 27 | export async function updateAssets({repo, assetsPath, destPath}) { |
| 28 | console.log('\n-----------------------------------------------'); |
| 29 | console.log(`Processing: ${repo}`); |
| 30 | console.log('-----------------------------------------------\n'); |
| 31 | |
| 32 | const buildInfoPath = join(destPath, '_build-info.json'); |
| 33 | if (!existsSync(buildInfoPath)) { |
| 34 | throw new Error(`${buildInfoPath} does not exist.`); |
| 35 | } |
| 36 | |
| 37 | assert(process.env.GITHUB_REF); |
| 38 | const currentBranch = process.env.GITHUB_REF; |
| 39 | |
| 40 | const {sha: storedSha, branchName: storedBranch} = JSON.parse( |
| 41 | await readFile(buildInfoPath, 'utf-8'), |
| 42 | ); |
| 43 | |
| 44 | const shaRegex = /^[0-9a-f]{40}$/i; |
| 45 | const branchRegex = /^(?!.*\.\.)[a-zA-Z0-9/_.-]+$/; |
| 46 | |
| 47 | if (!shaRegex.test(storedSha)) { |
| 48 | throw new Error(`Invalid SHA in build info: ${storedSha}`); |
| 49 | } |
| 50 | if (!branchRegex.test(storedBranch)) { |
| 51 | throw new Error(`Invalid branch name in build info: ${storedBranch}`); |
| 52 | } |
| 53 | |
| 54 | assert(process.env.ANGULAR_READONLY_GITHUB_TOKEN); |
| 55 | const githubApi = new GithubClient( |
| 56 | repo, |
| 57 | process.env.ANGULAR_READONLY_GITHUB_TOKEN, |
| 58 | 'ADEV_Cross_Repo_Docs_Update', |
| 59 | ); |
| 60 | |
| 61 | let downstreamBranch = currentBranch; |
| 62 | let latestSha = await githubApi.getShaForBranch(currentBranch); |
| 63 | if ( |
| 64 | latestSha.includes('No commit') && |
| 65 | currentBranch !== 'refs/heads/main' && |
| 66 | currentBranch !== storedBranch |
| 67 | ) { |
| 68 | // In some cases, such as when a new branch is created for a feature, |
| 69 | // the branch may not exist in the downstream repo. For example, during an |
| 70 | // exceptional minor release (e.g. FW 20.3.x and Components: 20.2.x). |
| 71 | // In such scenarios, we fallback to the last known branch. |
| 72 | latestSha = await githubApi.getShaForBranch(storedBranch); |
| 73 | downstreamBranch = storedBranch; |
| 74 | } |
| 75 | |
| 76 | if (!shaRegex.test(latestSha)) { |
| 77 | throw new Error(`Invalid SHA resolved: ${latestSha}`); |
| 78 | } |
| 79 | |
| 80 | console.log(`Comparing ${storedSha}...${latestSha}.`); |
| 81 | const affectedFiles = await githubApi.getAffectedFiles(storedSha, latestSha); |
| 82 | const changedFiles = affectedFiles.filter((file) => file.startsWith(`${assetsPath}/`)); |
| 83 | |
| 84 | let shaWhenFilesChanged; |
no test coverage detected
searching dependent graphs…