|
| 1 | +import fs from 'fs/promises'; |
| 2 | +import pathlib from 'path'; |
| 3 | +import memoize from 'lodash/memoize'; |
| 4 | +import uniq from 'lodash/uniq'; |
| 5 | +import * as core from '@actions/core'; |
| 6 | +import { getExecOutput } from '@actions/exec'; |
| 7 | +import { extractPkgsFromYarnLockV2 } from "snyk-nodejs-lockfile-parser"; |
| 8 | +import { gitRoot } from './gitRoot.js'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Parses and lockfile's contents and extracts all the different dependencies and |
| 12 | + * versions |
| 13 | + */ |
| 14 | +function processLockFileText(contents: string) { |
| 15 | + const lockFile = extractPkgsFromYarnLockV2(contents); |
| 16 | + return Object.entries(lockFile).reduce<Record<string, Set<string>>>((res, [key, { version }]) => { |
| 17 | + const newKey = key.split('@')[0]; |
| 18 | + |
| 19 | + if (!(newKey in res)) { |
| 20 | + res[newKey] = new Set() |
| 21 | + } |
| 22 | + |
| 23 | + res[newKey].add(version); |
| 24 | + return res; |
| 25 | + }, {}); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Retrieves the contents of the lockfile on the master branch |
| 30 | + */ |
| 31 | +async function getMasterLockFile() { |
| 32 | + const { stdout, exitCode } = await getExecOutput( |
| 33 | + 'git', |
| 34 | + [ |
| 35 | + '--no-pager', |
| 36 | + 'show', |
| 37 | + 'origin/master:yarn.lock' |
| 38 | + ], |
| 39 | + ); |
| 40 | + |
| 41 | + if (exitCode !== 0) { |
| 42 | + throw new Error('git show exited with non-zero error-code'); |
| 43 | + } |
| 44 | + |
| 45 | + return processLockFileText(stdout); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Retrieves the contents of the lockfile in the repo |
| 50 | + */ |
| 51 | +async function getCurrentLockFile() { |
| 52 | + const lockFilePath = pathlib.join(gitRoot, 'yarn.lock'); |
| 53 | + const contents = await fs.readFile(lockFilePath, 'utf-8'); |
| 54 | + return processLockFileText(contents); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Determines the names of the packages that have changed versions |
| 59 | + */ |
| 60 | +async function getPackageDiffs() { |
| 61 | + const [currentLockFile, masterLockFile] = await Promise.all([ |
| 62 | + getCurrentLockFile(), |
| 63 | + getMasterLockFile() |
| 64 | + ]); |
| 65 | + |
| 66 | + const packages = []; |
| 67 | + for (const [depName, versions] of Object.entries(currentLockFile)) { |
| 68 | + if (!(depName in masterLockFile)) { |
| 69 | + core.info(`${depName} in current lockfile, not in master lock file`); |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + const args = depName.split('@'); |
| 74 | + let needToAdd = false; |
| 75 | + for (const version of versions) { |
| 76 | + if (!masterLockFile[depName].has(version)) { |
| 77 | + core.info(`${depName} has ${version} in current lockfile but not in master`); |
| 78 | + needToAdd = true; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + for (const version of masterLockFile[depName]) { |
| 83 | + if (!versions.has(version)) { |
| 84 | + core.info(`${depName} has ${version} in master lockfile but not in current`); |
| 85 | + needToAdd = true; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (needToAdd) { |
| 90 | + packages.push(args[0]); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return uniq(packages); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Using `yarn why`, determine why the given package is present in the |
| 99 | + * lockfile. |
| 100 | + */ |
| 101 | +async function getPackageReason(pkg: string) { |
| 102 | + const packageNameRE = /^(@?sourceacademy\/.+)@.+$/ |
| 103 | + |
| 104 | + const { stdout, exitCode } = await getExecOutput('yarn why', [pkg, '-R', '--json']); |
| 105 | + if (exitCode !== 0) { |
| 106 | + throw new Error('yarn why exited with non-zero exit code!'); |
| 107 | + } |
| 108 | + |
| 109 | + return stdout.trim().split('\n').map(each => { |
| 110 | + const entry = JSON.parse(each).value; |
| 111 | + const match = packageNameRE.exec(entry); |
| 112 | + if (!match) { |
| 113 | + throw new Error('failed to identify a package!'); |
| 114 | + } |
| 115 | + |
| 116 | + return match[1]; |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Returns `true` if there are changes present in the given directory relative to |
| 122 | + * the master branch\ |
| 123 | + * Used to determine, particularly for libraries, if running tests and tsc are necessary |
| 124 | + */ |
| 125 | +export const hasLockFileChanged = memoize(async () => { |
| 126 | + const { exitCode } = await getExecOutput( |
| 127 | + 'git', |
| 128 | + ['--no-pager', 'diff', '--quiet', 'origin/master', '--', 'yarn.lock'], |
| 129 | + { |
| 130 | + failOnStdErr: false, |
| 131 | + ignoreReturnCode: true |
| 132 | + } |
| 133 | + ); |
| 134 | + return exitCode !== 0; |
| 135 | +}); |
| 136 | + |
| 137 | +/** |
| 138 | + * Gets a list of packages that have had some dependency of theirs |
| 139 | + * change according to the lockfile but not their package.json |
| 140 | + */ |
| 141 | +export async function getPackagesWithResolutionChanges() { |
| 142 | + const packages = await getPackageDiffs(); |
| 143 | + const workspaces = await Promise.all(packages.map(getPackageReason)); |
| 144 | + return uniq(workspaces.flat()); |
| 145 | +} |
0 commit comments