From b700cc643f4310e2adce042fd64bc846dca77977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9r=C3=A8?= Date: Sun, 31 Dec 2023 01:46:13 -0800 Subject: [PATCH] Try cloning without actions/checkout --- README.md | 11 ----------- __tests__/main.test.ts | 3 +++ badges/coverage.svg | 2 +- dist/index.js | 39 ++++++++++++++++----------------------- src/initializer.ts | 42 +++++++++++++++--------------------------- src/main.ts | 4 +++- 6 files changed, 38 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index e4888ae..3ba42cd 100644 --- a/README.md +++ b/README.md @@ -27,17 +27,6 @@ jobs: runs-on: ubuntu-latest steps: - # Perform a minimal checkout to set up this repository for the rest of the - # workflow. - # - # This step must be configured exactly as below: using different - # actions/checkout arguments is not supported. - - name: Checkout this repository - uses: actions/checkout@v4 - with: - filter: tree:0 - - # Run the estimation tool - name: Run sizeup # TODO: Replace the version below with your desired version. uses: lerebear/sizeup-action@v0.4.2 diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 7ed6295..d126637 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -69,6 +69,9 @@ describe('action', () => { // Shallow clone original @actions/github context const originalContext = { ...github.context } + // Mock cloning the repo + jest.spyOn(initializer, 'clone').mockImplementation(async () => {}) + // Mock the diff that we use for evaluation. jest .spyOn(initializer, 'fetchDiff') diff --git a/badges/coverage.svg b/badges/coverage.svg index d179b1d..4697c6a 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -Coverage: 63.23%Coverage63.23% \ No newline at end of file +Coverage: 62.85%Coverage62.85% \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 8cef7fe..114148a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -16348,7 +16348,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.fetchDiff = exports.pullRequestAuthorHasNotOptedIn = exports.workflowTriggeredForUnsupportedEvent = exports.loadConfiguration = void 0; +exports.fetchDiff = exports.clone = exports.pullRequestAuthorHasNotOptedIn = exports.workflowTriggeredForUnsupportedEvent = exports.loadConfiguration = void 0; const core = __importStar(__nccwpck_require__(2186)); const github = __importStar(__nccwpck_require__(5438)); const fs = __importStar(__nccwpck_require__(7147)); @@ -16393,6 +16393,18 @@ function pullRequestAuthorHasNotOptedIn(config, pullRequest) { return false; } exports.pullRequestAuthorHasNotOptedIn = pullRequestAuthorHasNotOptedIn; +async function clone(pull) { + const git = (0, simple_git_1.simpleGit)('.', { trimmed: true }); + const repo = `${github.context.repo.owner}/${github.context.repo.repo}`; + core.info(`Cloning ${repo} with the single branch "${pull.head.ref}"`); + await git.clone(`https://${core.getInput('token')}@github.com/${repo}`, '.', [ + `--branch=${pull.head.ref}`, + '--filter=tree:0', + '--no-tags', + '--single-branch' + ]); +} +exports.clone = clone; /** * Retrieves the diff of the pull request that triggered this workflow which we * will use for evaluation. @@ -16402,35 +16414,15 @@ exports.pullRequestAuthorHasNotOptedIn = pullRequestAuthorHasNotOptedIn; */ async function fetchDiff(pull) { const git = (0, simple_git_1.simpleGit)('.', { trimmed: true }); - // let baseRefExists = false - // try { - // baseRefExists = !!(await git.raw('rev-parse', '--verify', pull.base.ref)) - // } catch (e) { - // core.error( - // `Error from 'git rev-parse --verfy ${pull.base.ref}': ${ - // (e as Error).message - // }` - // ) - // } - // if (!baseRefExists) { - // core.setFailed( - // `Could not find pull request base branch ${pull.base.ref}. ` + - // `Please make sure actions/checkout was used beforehand to fetch ${pull.base.ref}.` - // ) - // return - // } - core.debug(`Fetching base ref "${pull.base.ref}" and head ref "${pull.head.ref}"`); + core.debug(`Fetching base ref "${pull.base.ref}"`); await git.fetch([ 'origin', `+${pull.base.ref}:${pull.base.ref}`, - `+${pull.head.ref}:${pull.head.ref}`, `--filter=tree:0`, '--no-tags', '--prune', '--no-recurse-submodules' ]); - core.debug(`Switching to head ref "${pull.head.ref}"`); - await git.raw('switch', pull.head.ref); const diffArgs = ['--merge-base', pull.base.ref].concat(core.getInput('git-diff-options').split(/\s+/)); core.info(`Retrieving diff with \`git diff ${diffArgs.join(' ')}\``); return git.diff(diffArgs); @@ -16495,8 +16487,9 @@ async function run() { try { if ((0, initializer_1.workflowTriggeredForUnsupportedEvent)()) return; - const config = (0, initializer_1.loadConfiguration)(); const pullRequest = github.context.payload.pull_request; + await (0, initializer_1.clone)(pullRequest); + const config = (0, initializer_1.loadConfiguration)(); if ((0, initializer_1.pullRequestAuthorHasNotOptedIn)(config, pullRequest)) return; const diff = await (0, initializer_1.fetchDiff)(pullRequest); diff --git a/src/initializer.ts b/src/initializer.ts index c4eb934..8392ab5 100644 --- a/src/initializer.ts +++ b/src/initializer.ts @@ -60,6 +60,20 @@ export function pullRequestAuthorHasNotOptedIn( return false } +export async function clone(pull: PullRequest): Promise { + const git = simpleGit('.', { trimmed: true }) + const repo = `${github.context.repo.owner}/${github.context.repo.repo}` + + core.info(`Cloning ${repo} with the single branch "${pull.head.ref}"`) + + await git.clone(`https://${core.getInput('token')}@github.com/${repo}`, '.', [ + `--branch=${pull.head.ref}`, + '--filter=tree:0', + '--no-tags', + '--single-branch' + ]) +} + /** * Retrieves the diff of the pull request that triggered this workflow which we * will use for evaluation. @@ -72,42 +86,16 @@ export async function fetchDiff( ): Promise { const git = simpleGit('.', { trimmed: true }) - // let baseRefExists = false - // try { - // baseRefExists = !!(await git.raw('rev-parse', '--verify', pull.base.ref)) - // } catch (e) { - // core.error( - // `Error from 'git rev-parse --verfy ${pull.base.ref}': ${ - // (e as Error).message - // }` - // ) - // } - - // if (!baseRefExists) { - // core.setFailed( - // `Could not find pull request base branch ${pull.base.ref}. ` + - // `Please make sure actions/checkout was used beforehand to fetch ${pull.base.ref}.` - // ) - // return - // } - - core.debug( - `Fetching base ref "${pull.base.ref}" and head ref "${pull.head.ref}"` - ) - + core.debug(`Fetching base ref "${pull.base.ref}"`) await git.fetch([ 'origin', `+${pull.base.ref}:${pull.base.ref}`, - `+${pull.head.ref}:${pull.head.ref}`, `--filter=tree:0`, '--no-tags', '--prune', '--no-recurse-submodules' ]) - core.debug(`Switching to head ref "${pull.head.ref}"`) - await git.raw('switch', pull.head.ref) - const diffArgs = ['--merge-base', pull.base.ref].concat( core.getInput('git-diff-options').split(/\s+/) ) diff --git a/src/main.ts b/src/main.ts index cf531cc..1eecb8c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,6 +7,7 @@ import * as fs from 'fs' import * as path from 'path' import { Configuration } from './configuration' import { + clone, fetchDiff, loadConfiguration, pullRequestAuthorHasNotOptedIn, @@ -32,8 +33,9 @@ export async function run(): Promise { try { if (workflowTriggeredForUnsupportedEvent()) return - const config = loadConfiguration() const pullRequest = github.context.payload.pull_request as PullRequest + await clone(pullRequest) + const config = loadConfiguration() if (pullRequestAuthorHasNotOptedIn(config, pullRequest)) return