Skip to content

Commit

Permalink
Try cloning without actions/checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
lerebear committed Dec 31, 2023
1 parent 2747dbb commit e3f8d5b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 63 deletions.
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 22 additions & 23 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 24 additions & 27 deletions src/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ export function pullRequestAuthorHasNotOptedIn(
return false
}

export async function clone(pull: PullRequest): Promise<void> {
const basicCredential = Buffer.from(
`x-access-token:${core.getInput('token')}`,
'utf8'
).toString('base64')
core.setSecret(basicCredential)

const authorizationHeader = `AUTHORIZATION: basic ${basicCredential}`
const git = simpleGit('.', {
trimmed: true,
config: [`http.extraheader=${authorizationHeader}`]
})
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://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.
Expand All @@ -72,42 +95,16 @@ export async function fetchDiff(
): Promise<string | undefined> {
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+/)
)
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as fs from 'fs'
import * as path from 'path'
import { Configuration } from './configuration'
import {
clone,
fetchDiff,
loadConfiguration,
pullRequestAuthorHasNotOptedIn,
Expand All @@ -32,8 +33,9 @@ export async function run(): Promise<void> {
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

Expand Down

0 comments on commit e3f8d5b

Please sign in to comment.