Skip to content

Commit

Permalink
Clone repo before loading configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lerebear committed Aug 4, 2024
1 parent 639f56a commit bce49bb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 53 deletions.
24 changes: 9 additions & 15 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
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.
37 changes: 21 additions & 16 deletions dist/index.js

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

8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
38 changes: 22 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export async function run(): Promise<void> {

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
Expand Down Expand Up @@ -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})` : ''
Expand Down

0 comments on commit bce49bb

Please sign in to comment.