5
0
Fork 0
mirror of https://github.com/wagoid/commitlint-github-action.git synced 2025-11-07 08:06:54 +00:00
commitlint-github-action/src/testUtils.js
wagoid 5463926c07 feat: update dependencies that needed to switch to ESM syntax
Some packages were updated to versions that now use ECMAScript modules,
so this repo was updated to use ES modules using Node.js built-in support.

Update was done using the great guide from @sindresorhus:
https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
2021-07-07 15:17:00 -03:00

75 lines
1.7 KiB
JavaScript

import path from 'path'
import fs from 'fs'
import { promisify } from 'util'
import execa from 'execa'
const writeFile = promisify(fs.writeFile)
export const updateEnvVars = (envVars) => {
Object.keys(envVars).forEach((key) => {
process.env[key] = envVars[key]
})
}
export const gitEmptyCommit = (cwd, message) =>
execa('git', ['commit', '--allow-empty', '-m', message], { cwd })
export const getCommitHashes = async (cwd) => {
const { stdout } = await execa.command('git log --pretty=%H', { cwd })
const hashes = stdout.split('\n').reverse()
return hashes
}
export const updatePushEnvVars = (cwd, to) => {
updateEnvVars({
GITHUB_WORKSPACE: cwd,
GITHUB_EVENT_NAME: 'push',
GITHUB_SHA: to,
})
}
export const createPushEventPayload = async (
cwd,
{ before = null, to, forced = false },
) => {
const payload = {
after: to,
before,
forced,
}
const eventPath = path.join(cwd, 'pushEventPayload.json')
updateEnvVars({ GITHUB_EVENT_PATH: eventPath })
await writeFile(eventPath, JSON.stringify(payload), 'utf8')
}
export const createPullRequestEventPayload = async (cwd) => {
const payload = {
number: '1',
repository: {
owner: {
login: 'wagoid',
},
name: 'commitlint-github-action',
},
}
const eventPath = path.join(cwd, 'pullRequestEventPayload.json')
updateEnvVars({
GITHUB_EVENT_PATH: eventPath,
GITHUB_REPOSITORY: 'wagoid/commitlint-github-action',
})
await writeFile(eventPath, JSON.stringify(payload), 'utf8')
}
export const updatePullRequestEnvVars = (cwd, to, options = {}) => {
const { eventName = 'pull_request' } = options
updateEnvVars({
GITHUB_WORKSPACE: cwd,
GITHUB_EVENT_NAME: eventName,
GITHUB_SHA: to,
})
}