Skip to content

Commit

Permalink
Revert "refactor runSpawn to use @actions/exec (#205)" (#208)
Browse files Browse the repository at this point in the history
This reverts commit 66727ac.
  • Loading branch information
maksimovicdanijel authored Dec 16, 2022
1 parent 627170c commit d8b9ae5
Show file tree
Hide file tree
Showing 21 changed files with 357 additions and 1,944 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ node_modules
.nyc_output
.idea
.vscode
coverage
4 changes: 4 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
statements: 90
lines: 90
functions: 78
branches: 90
1,685 changes: 186 additions & 1,499 deletions dist/index.js

Large diffs are not rendered by default.

27 changes: 0 additions & 27 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"license": "MIT",
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1",
"@actions/github": "^5.1.1",
"adm-zip": "^0.5.9",
"lodash.isequal": "^4.5.0",
Expand Down
17 changes: 9 additions & 8 deletions src/openPr.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const _template = require('lodash.template')
const core = require('@actions/core')

const { PR_TITLE_PREFIX } = require('./const')
const { execWithOutput } = require('./utils/execWithOutput')
const { runSpawn } = require('./utils/runSpawn')
const { callApi } = require('./utils/callApi')
const transformCommitMessage = require('./utils/commitMessage')
const { logInfo } = require('./log')
Expand All @@ -26,7 +26,8 @@ const addArtifact = async (inputs, releaseId) => {

const createDraftRelease = async (inputs, newVersion) => {
try {
const releaseCommitHash = await execWithOutput('git', ['rev-parse', 'HEAD'])
const run = runSpawn()
const releaseCommitHash = await run('git', ['rev-parse', 'HEAD'])

logInfo(`Creating draft release from commit: ${releaseCommitHash}`)

Expand All @@ -52,6 +53,7 @@ const createDraftRelease = async (inputs, newVersion) => {

module.exports = async function ({ context, inputs, packageVersion }) {
logInfo('** Starting Opening Release PR **')
const run = runSpawn()

if (!packageVersion) {
throw new Error('packageVersion is missing!')
Expand All @@ -62,15 +64,15 @@ module.exports = async function ({ context, inputs, packageVersion }) {
const branchName = `release/${newVersion}`

const messageTemplate = inputs['commit-message']
await execWithOutput('git', ['checkout', '-b', branchName])
await execWithOutput('git', ['add', '-A'])
await execWithOutput('git', [
await run('git', ['checkout', '-b', branchName])
await run('git', ['add', '-A'])
await run('git', [
'commit',
'-m',
`"${transformCommitMessage(messageTemplate, newVersion)}"`,
])

await execWithOutput('git', ['push', 'origin', branchName])
await run('git', ['push', 'origin', branchName])

const draftRelease = await createDraftRelease(inputs, newVersion)

Expand Down Expand Up @@ -103,15 +105,14 @@ module.exports = async function ({ context, inputs, packageVersion }) {
},
inputs
)
/* istanbul ignore else */
if (response?.status !== 201) {
const errMessage = response?.message || 'PR creation failed'
throw new Error(errMessage)
}
} catch (err) {
let message = `Unable to create the pull request ${err.message}`
try {
await execWithOutput('git', ['push', 'origin', '--delete', branchName])
await run('git', ['push', 'origin', '--delete', branchName])
} catch (error) {
message += `\n Unable to delete branch ${branchName}: ${error.message}`
}
Expand Down
5 changes: 3 additions & 2 deletions src/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const semver = require('semver')
const { PR_TITLE_PREFIX } = require('./const')
const { callApi } = require('./utils/callApi')
const { tagVersionInGit } = require('./utils/tagVersion')
const { execWithOutput } = require('./utils/execWithOutput')
const { runSpawn } = require('./utils/runSpawn')
const { revertCommit } = require('./utils/revertCommit')
const { publishToNpm } = require('./utils/publishToNpm')
const { notifyIssues } = require('./utils/notifyIssues')
Expand Down Expand Up @@ -60,13 +60,14 @@ module.exports = async function ({ github, context, inputs }) {
return
}

const run = runSpawn()
const branchName = `release/${version}`

try {
// We "always" delete the release branch, if anything fails, the whole
// workflow has to be restarted from scratch.
logInfo(`deleting ${branchName}`)
await execWithOutput('git', ['push', 'origin', '--delete', branchName])
await run('git', ['push', 'origin', '--delete', branchName])
} catch (err) {
// That's not a big problem, so we don't want to mark the action as failed.
logWarning('Unable to delete the release branch')
Expand Down
1 change: 0 additions & 1 deletion src/utils/artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const { ZIP_EXTENSION } = require('../const')
const attach = async (path, releaseId, token) => {
const filename = deriveFilename(path, ZIP_EXTENSION)

/* istanbul ignore else */
if (!path.endsWith(ZIP_EXTENSION)) {
await archiveItem(path, filename)
}
Expand Down
61 changes: 0 additions & 61 deletions src/utils/execWithOutput.js

This file was deleted.

26 changes: 12 additions & 14 deletions src/utils/publishToNpm.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict'

const { execWithOutput } = require('./execWithOutput')
const { runSpawn } = require('./runSpawn')

async function allowNpmPublish(version) {
const run = runSpawn()

// We need to check if the package was already published. This can happen if
// the action was already executed before, but it failed in its last step
// (GH release).
let packageName = null
try {
const packageInfo = await execWithOutput('npm', ['view', '--json'])
const packageInfo = await run('npm', ['view', '--json'])
packageName = packageInfo ? JSON.parse(packageInfo).name : null
} catch (error) {
if (!error?.message?.match(/code E404/)) {
Expand All @@ -29,10 +31,7 @@ async function allowNpmPublish(version) {
try {
// npm < v8.13.0 returns empty output, newer versions throw a E404
// We handle both and consider them as package version not existing
packageVersionInfo = await execWithOutput('npm', [
'view',
`${packageName}@${version}`,
])
packageVersionInfo = await run('npm', ['view', `${packageName}@${version}`])
} catch (error) {
if (!error?.message?.match(/code E404/)) {
throw error
Expand All @@ -49,22 +48,21 @@ async function publishToNpm({
npmTag,
version,
}) {
await execWithOutput('npm', [
const run = runSpawn()

await run('npm', [
'config',
'set',
`//registry.npmjs.org/:_authToken=${npmToken}`,
])

if (await allowNpmPublish(version)) {
await execWithOutput('npm', ['pack', '--dry-run'])
await run('npm', ['pack', '--dry-run'])
if (opticToken) {
const otp = await execWithOutput('curl', [
'-s',
`${opticUrl}${opticToken}`,
])
await execWithOutput('npm', ['publish', '--otp', otp, '--tag', npmTag])
const otp = await run('curl', ['-s', `${opticUrl}${opticToken}`])
await run('npm', ['publish', '--otp', otp, '--tag', npmTag])
} else {
await execWithOutput('npm', ['publish', '--tag', npmTag])
await run('npm', ['publish', '--tag', npmTag])
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/utils/revertCommit.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict'
const { execWithOutput } = require('./execWithOutput')
const { runSpawn } = require('./runSpawn')

async function revertCommit(baseRef) {
await execWithOutput('git', ['revert', 'HEAD'])
await execWithOutput('git', ['push', 'origin', baseRef])
const run = runSpawn()

await run('git', ['revert', 'HEAD'])
await run('git', ['push', 'origin', baseRef])
}

exports.revertCommit = revertCommit
36 changes: 36 additions & 0 deletions src/utils/runSpawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const { spawn } = require('child_process')

function runSpawn({ cwd } = {}) {
return (cmd, args) => {
return new Promise((resolve, reject) => {
const cli = spawn(cmd, args, { cwd, env: process.env, shell: true })
cli.stdout.setEncoding('utf8')
cli.stderr.setEncoding('utf8')

let stdout = ''
let stderr = ''
cli.stdout.on('data', data => {
stdout += data
})
cli.stderr.on('data', data => {
stderr += data
})
cli.on('close', (code, signal) => {
if (code === 0) {
return resolve(stdout.trim())
}
reject(
new Error(
`${cmd} ${args.join(
' '
)} returned code ${code} and signal ${signal}\nSTDOUT: ${stdout}\nSTDERR: ${stderr}`
)
)
})
})
}
}

exports.runSpawn = runSpawn
10 changes: 6 additions & 4 deletions src/utils/tagVersion.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict'
const { execWithOutput } = require('./execWithOutput')
const { runSpawn } = require('./runSpawn')

async function tagVersionInGit(version) {
await execWithOutput('git', ['push', 'origin', `:refs/tags/${version}`])
await execWithOutput('git', ['tag', '-f', `"${version}"`])
await execWithOutput('git', ['push', 'origin', `--tags`])
const run = runSpawn()

await run('git', ['push', 'origin', `:refs/tags/${version}`])
await run('git', ['tag', '-f', `"${version}"`])
await run('git', ['push', 'origin', `--tags`])
}

exports.tagVersionInGit = tagVersionInGit
Loading

0 comments on commit d8b9ae5

Please sign in to comment.