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 385b768
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 156 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
15 changes: 14 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,31 @@
import * as core from '@actions/core'
import * as main from '../src/main'
import * as initializer from '../src/initializer'
import { Git } from '../src/git'
import * as github from '@actions/github'

function pullRequestEventContext(overrides = {}): object {
return {
eventName: 'pull_request',
repo: {
owner: 'lerebear',
name: 'sizeup-action'
},
payload: {
pull_request: {
base: {
ref: 'main',
repo: {
full_name: 'lerebear/sizeup-action',
name: 'sizeup-action',
owner: {
login: 'lerebear'
}
}
},
head: {
ref: 'topic'
},
labels: [],
number: 1,
user: {
Expand Down Expand Up @@ -69,9 +79,12 @@ describe('action', () => {
// Shallow clone original @actions/github context
const originalContext = { ...github.context }

// Mock cloning the repo
jest.spyOn(Git.prototype, 'clone').mockImplementation(async () => {})

// Mock the diff that we use for evaluation.
jest
.spyOn(initializer, 'fetchDiff')
.spyOn(Git.prototype, '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!'
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.
165 changes: 103 additions & 62 deletions dist/index.js

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

70 changes: 70 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as core from '@actions/core'
import * as lib from 'simple-git'

export class Git {
private client: lib.SimpleGit

constructor() {
const basicCredential = Buffer.from(
`x-access-token:${core.getInput('token')}`,
'utf8'
).toString('base64')
const authorizationHeader = `AUTHORIZATION: basic ${basicCredential}`
core.setSecret(basicCredential)

this.client = lib.simpleGit('.', {
trimmed: true,
config: [`http.extraheader=${authorizationHeader}`]
})
}

/**
* Clones the repository from which this workflow was triggered.
*
* @param repo The repository to clone in the format "<owner>/<name>"
* @param headRef The single branch to clone, which should correspond to the
* head ref of the pull request that triggered this workflow. This is
* required for efficiency.
* @param targetDirectory The directory in which to clone the repository.
*/
async clone(
repo: string,
headRef: string,
targetDirectory = '.'
): Promise<void> {
core.info(`Cloning ${repo} with the single branch "${headRef}"`)

await this.client.clone(`https://github.com/${repo}`, targetDirectory, [
`--branch=${headRef}`,
'--filter=tree:0',
'--no-tags',
'--single-branch'
])
}

/**
* Retrieves the diff of the pull request that triggered this workflow which we
* will use for evaluation.
*
* @param baseRef The base branch relative to which we should produce a diff. This method assumes
* that the head ref containing the changes has already been fetched.
* @returns The diff of the given pull request or `undefined` if we failed to retrieve it.
*/
async diff(baseRef: string): Promise<string | undefined> {
core.debug(`Fetching base ref "${baseRef}"`)
await this.client.fetch([
'origin',
`+${baseRef}:${baseRef}`,
`--filter=tree:0`,
'--no-tags',
'--prune',
'--no-recurse-submodules'
])

const diffArgs = ['--merge-base', baseRef].concat(
core.getInput('git-diff-options').split(/\s+/)
)
core.info(`Retrieving diff with \`git diff ${diffArgs.join(' ')}\``)
return this.client.diff(diffArgs)
}
}
Loading

0 comments on commit 385b768

Please sign in to comment.