diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 3c1c254..520fc41 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -9,10 +9,8 @@ import * as core from '@actions/core' import * as main from '../src/main' import * as initializer from '../src/initializer' -import { SizeUp, Score } from 'sizeup-core' +import { SizeUp } from 'sizeup-core' import * as github from '@actions/github' -import { Context } from 'sizeup-core/dist/context' -import { CategoryConfiguration } from 'sizeup-core/dist/category-configuration' function pullRequestEventContext(overrides = {}): object { return { @@ -82,21 +80,17 @@ describe('action', () => { // Shallow clone original @actions/github context const originalContext = { ...github.context } + // Mock cloning the repo + jest.spyOn(SizeUp, 'clone').mockImplementation(async () => {}) + // Mock the diff that we use for evaluation. - jest.spyOn(SizeUp, 'evaluate').mockImplementation(async () => - Promise.resolve( - new Score( - '', - 1, - new Context({ - categories: new CategoryConfiguration([ - { name: 'extra small', label: { name: 'xs' }, lte: 10 }, - { name: 'large', label: { name: 'l' } } - ]) - }) + jest + .spyOn(SizeUp, 'diff') + .mockImplementation(async () => + Promise.resolve( + '--- README.md 2023-10-16 16:35:38\n+++ README-AGAIN.md 2023-10-16 16:36:07\n@@ -0,0 +1 @@\n+# Hello, World!' ) ) - ) // Mock core.getInput() such that we verify that we retrieve the auth token // from the right input variable. diff --git a/badges/coverage.svg b/badges/coverage.svg index 299e8da..e258181 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -Coverage: 70.58%Coverage70.58% \ No newline at end of file +Coverage: 71.2%Coverage71.2% \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index ad46d2c..29656a7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -92483,7 +92483,7 @@ class Git { * @param cwd The local directory in which to run the `git` commands * @returns The diff of the given pull request or `undefined` if we failed to retrieve it. */ - async diff(baseRef, gitDiffOptions, cwd = '.') { + async diff(baseRef, gitDiffOptions = [], cwd = '.') { return await this .client .cwd({ path: cwd }) @@ -93009,6 +93009,14 @@ class SizeUp { const context = await this.context(diffOrRemote, userSuppliedConfig, defaultConfig); return formula.evaluate(context); } + static async clone(token, repo, headRef, targetDirectory) { + const git = new git_1.Git(token); + git.clone(repo, headRef, targetDirectory); + } + static async diff(token, baseRef, gitDiffOptions, cwd) { + const git = new git_1.Git(token); + return git.diff(baseRef, gitDiffOptions, cwd); + } static config(configPath) { if (!configPath) return {}; @@ -106596,6 +106604,9 @@ async function run() { return; } const pullRequest = github.context.payload.pull_request; + core.info(`Cwd before clone: ${JSON.stringify(fs.readdirSync('.'))}`); + await sizeup_core_1.SizeUp.clone(core.getInput('token'), pullRequest.base.repo.full_name, pullRequest.head.ref); + core.info(`Cwd after clone: ${JSON.stringify(fs.readdirSync('.'))}`); const config = (0, initializer_1.loadConfiguration)(); const optInStatus = (0, initializer_1.getOptInStatus)(pullRequest, config); if (optInStatus === initializer_1.OptInStatus.Out) @@ -106624,22 +106635,16 @@ exports.run = run; async function evaluatePullRequest(pull, config) { const pullRequestNickname = `${pull.base.repo.full_name}#${pull.number}`; core.info(`Evaluating pull request ${pullRequestNickname}`); - let sizeupConfigFile = undefined; + let sizeupConfigPath = undefined; if (config.sizeup) { - sizeupConfigFile = path.resolve(__dirname, './tmp/sizeup.yaml'); - fs.mkdirSync(path.dirname(sizeupConfigFile)); - fs.writeFileSync(sizeupConfigFile, YAML.stringify(config.sizeup)); - } - const score = await sizeup_core_1.SizeUp.evaluate({ - repo: pull.base.repo.full_name, - headRef: pull.head.ref, - baseRef: pull.base.ref, - diffOptions: core.getInput('git-diff-options').split(/\s+/), - token: core.getInput('token'), - cloneDirectory: '.' - }, sizeupConfigFile); - if (sizeupConfigFile) { - fs.rmSync(sizeupConfigFile, { force: true, recursive: true }); + sizeupConfigPath = path.resolve(__dirname, './tmp/sizeup.yaml'); + fs.mkdirSync(path.dirname(sizeupConfigPath)); + fs.writeFileSync(sizeupConfigPath, YAML.stringify(config.sizeup)); + } + const diff = await sizeup_core_1.SizeUp.diff(core.getInput('token'), pull.base.ref, core.getInput('git-diff-options').split(/\s+/)); + const score = await sizeup_core_1.SizeUp.evaluate(diff, sizeupConfigPath); + if (sizeupConfigPath) { + fs.rmSync(sizeupConfigPath, { force: true, recursive: true }); } const categoryDescription = score.category ? `(${score.category.name})` : ''; core.info(`Pull request ${pullRequestNickname} received a score of ${score.result} ${categoryDescription}`.trimEnd()); diff --git a/package-lock.json b/package-lock.json index 0551470..bb44040 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "@actions/github": "^5.1.1", "@octokit/webhooks": "^12.0.3", "simple-git": "^3.21.0", - "sizeup-core": "^0.5.4", + "sizeup-core": "^0.5.5", "yaml": "^2.3.2" }, "devDependencies": { @@ -7876,9 +7876,9 @@ "dev": true }, "node_modules/sizeup-core": { - "version": "0.5.4", - "resolved": "https://registry.npmjs.org/sizeup-core/-/sizeup-core-0.5.4.tgz", - "integrity": "sha512-bt6rKA3v/X2xxPyj+9oeWx/WEMozS1vBot60yMfGcscRCiKEVxbz3FaAjB1CsAqq7TyKVKFSPtMijpl9nfRrpA==", + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/sizeup-core/-/sizeup-core-0.5.5.tgz", + "integrity": "sha512-eBdJhta2898+YbagDJGrSzb7wDkORPSo2/NBdMxPJqM6HYJv9tXqaTXIUXQHh3EIL8WDjZ7GXEyW5Pw8K8J1Dw==", "dependencies": { "minimatch": "^9.0.3", "parse-diff": "^0.11.1", diff --git a/package.json b/package.json index d28f24b..53a10d9 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "@actions/github": "^5.1.1", "@octokit/webhooks": "^12.0.3", "simple-git": "^3.21.0", - "sizeup-core": "^0.5.4", + "sizeup-core": "^0.5.5", "yaml": "^2.3.2" }, "devDependencies": { diff --git a/src/main.ts b/src/main.ts index f82880d..d51d274 100644 --- a/src/main.ts +++ b/src/main.ts @@ -34,6 +34,16 @@ export async function run(): Promise { const pullRequest = github.context.payload.pull_request as PullRequest + core.info(`Cwd before clone: ${JSON.stringify(fs.readdirSync('.'))}`) + + await SizeUp.clone( + core.getInput('token'), + pullRequest.base.repo.full_name, + pullRequest.head.ref + ) + + core.info(`Cwd after clone: ${JSON.stringify(fs.readdirSync('.'))}`) + const config = loadConfiguration() const optInStatus = getOptInStatus(pullRequest, config) if (optInStatus === OptInStatus.Out) return @@ -70,27 +80,23 @@ async function evaluatePullRequest( const pullRequestNickname = `${pull.base.repo.full_name}#${pull.number}` core.info(`Evaluating pull request ${pullRequestNickname}`) - let sizeupConfigFile = undefined + let sizeupConfigPath = undefined if (config.sizeup) { - sizeupConfigFile = path.resolve(__dirname, './tmp/sizeup.yaml') - fs.mkdirSync(path.dirname(sizeupConfigFile)) - fs.writeFileSync(sizeupConfigFile, YAML.stringify(config.sizeup)) + sizeupConfigPath = path.resolve(__dirname, './tmp/sizeup.yaml') + fs.mkdirSync(path.dirname(sizeupConfigPath)) + fs.writeFileSync(sizeupConfigPath, YAML.stringify(config.sizeup)) } - const score = await SizeUp.evaluate( - { - repo: pull.base.repo.full_name, - headRef: pull.head.ref, - baseRef: pull.base.ref, - diffOptions: core.getInput('git-diff-options').split(/\s+/), - token: core.getInput('token'), - cloneDirectory: '.' - }, - sizeupConfigFile + const diff = await SizeUp.diff( + core.getInput('token'), + pull.base.ref, + core.getInput('git-diff-options').split(/\s+/) ) - if (sizeupConfigFile) { - fs.rmSync(sizeupConfigFile, { force: true, recursive: true }) + const score = await SizeUp.evaluate(diff, sizeupConfigPath) + + if (sizeupConfigPath) { + fs.rmSync(sizeupConfigPath, { force: true, recursive: true }) } const categoryDescription = score.category ? `(${score.category.name})` : ''