Skip to content

Commit

Permalink
feat: set target release as bump commit
Browse files Browse the repository at this point in the history
This will avoid commits made on master after the release PR was created to be included in the release.
  • Loading branch information
guilhermelimak committed Nov 10, 2022
1 parent 1f7a386 commit b93ddf2
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 47 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ jobs:
npm-tag: ${{ github.event.inputs.tag }}
```

> Not all symbols that can be used in GitHub usernames are valid in secret names. One such example is the hyphen symbol (`-`). In such cases, this approach will not work.
> Not all symbols that can be used in GitHub usernames are valid in secret names. One such example is the hyphen symbol (`-`). In such cases, this approach will not work.

## How to add a build step to your workflow

Expand All @@ -142,7 +142,7 @@ It is important to be aware that you are responsible for:
You can customize it by executing additional steps before the `nearform/optic-release-automation-action` step execution as shown in the next example.
2. The command to build the project, starting from the installation to the cleanup if needed.<br /> You can set any automations like [git hooks](https://git-scm.com/book/it/v2/Customizing-Git-Git-Hooks) or [`pre/post` scripts](https://docs.npmjs.com/cli/v8/using-npm/scripts#pre--post-scripts) to execute within the `build-command` step.

The build's output will be committed to the `release/${new semver version}` branch, unless the project's `.gitignore` blocks it.
The build's output will be committed to the `release/${new semver version}` branch, unless the project's `.gitignore` blocks it.
In that case, the build's output will be packed into the Npm package during the release process.

Here an example using `npm` to build the project:
Expand Down Expand Up @@ -183,6 +183,7 @@ jobs:
| `npm-tag` | No | If you want to release to the Npm with a custom tag, say `next`. <br /> (_Default: `latest`_) |
| `build-command`| No | An optional build commit to run after the version bump and before releasing the package |
| `api-url` | No | GitHub App URL. You wouldn't need to set this unless you're deploying a custom GitHub app instead of [optic-release-automation](https://github.com/apps/optic-release-automation). <br /> (_Default: `https://optic-release-automation-ocrlhra4va-ue.a.run.app/`_) |
| `app-name` | No | GitHub App name. You also wouldn't need to set this unless you're deploying a custom GitHub app. <br /> (_Default: `optic-release-automation[bot]`_) |
| `sync-semver-tags`| No | If you want to keep the major and minor versions git tags synced to the latest appropriate commit <br /> (_Default: `false`_) |
| `notify-linked-issues`| No | An optional flag to enable an automatic comment on all issues linked to each release so that people following those issues get notified of the code being released. <br /> (_Default: `true`_) |
| `artifact-path`| No | Set this input to the distribution folder or file you want to add as the main asset for your release. It will be downloadable from the release page and a preview of it will be available in the pull request. |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ inputs:
description: 'Url of the API where the application is running'
required: false
default: 'https://optic-release-automation-ocrlhra4va-ue.a.run.app/'
app-name:
description: 'Name of the github app that authors optic PRs in the following format: `app-name[bot]`'
required: false
default: 'optic-release-automation[bot]'
sync-semver-tags:
description: 'If you want to keep the major and minor versions tag synced to the latest appropriate commit'
required: false
Expand Down
104 changes: 62 additions & 42 deletions dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

exports.PR_TITLE_PREFIX = '[OPTIC-RELEASE-AUTOMATION]'
exports.ZIP_EXTENSION = '.zip'
exports.APP_NAME = 'optic-release-automation[bot]'
4 changes: 4 additions & 0 deletions src/openPr.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ module.exports = async function ({ context, inputs, packageVersion }) {
'-m',
`"${transformCommitMessage(messageTemplate, newVersion)}"`,
])

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

const releaseCommitHash = await run('git', ['rev-parse', 'HEAD'])

const { data: draftRelease } = await callApi(
{
method: 'POST',
endpoint: 'release',
body: {
version: newVersion,
target: releaseCommitHash,
},
},
inputs
Expand Down
2 changes: 1 addition & 1 deletion src/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = async function ({ github, context, inputs }) {

if (
context.payload.action !== 'closed' ||
pr.user.login !== 'optic-release-automation[bot]' ||
pr.user.login !== inputs['app-name'] ||
!pr.title.startsWith(PR_TITLE_PREFIX)
) {
logWarning('skipping release.')
Expand Down
8 changes: 7 additions & 1 deletion test/bump.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const artifactAction = require('../src/utils/artifact')
const { PR_TITLE_PREFIX } = require('../src/const')

const TEST_VERSION = '3.1.1'
const runSpawnStub = sinon.stub().returns(TEST_VERSION)
const TEST_COMMIT_HASH = 'c86b0a35014a7036b245f81ff9de9bd738a5fe95'
const runSpawnStub = sinon.stub()

runSpawnStub.returns(TEST_VERSION)
runSpawnStub.withArgs('git', ['rev-parse', 'HEAD']).returns(TEST_COMMIT_HASH)

function setup() {
const coreStub = sinon.stub(core)
Expand Down Expand Up @@ -181,6 +185,7 @@ tap.test('should work with a custom version-prefix', async () => {
endpoint: 'release',
body: {
version: TEST_VERSION,
target: TEST_COMMIT_HASH,
},
},
prData.inputs
Expand All @@ -206,6 +211,7 @@ tap.test('should call the release endpoint with a new version', async () => {
endpoint: 'release',
body: {
version: `v${TEST_VERSION}`,
target: TEST_COMMIT_HASH,
},
},
DEFAULT_ACTION_DATA.inputs
Expand Down
22 changes: 21 additions & 1 deletion test/release.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const notifyIssuesAction = require('../src/utils/notifyIssues')
const revertCommitAction = require('../src/utils/revertCommit')
const callApiAction = require('../src/utils/callApi')

const { PR_TITLE_PREFIX } = require('../src/const')
const { PR_TITLE_PREFIX, APP_NAME } = require('../src/const')
const actionLog = require('../src/log')

let deleteReleaseStub = sinon.stub().resolves()
Expand All @@ -33,6 +33,7 @@ const DEFAULT_ACTION_DATA = {
},
inputs: {
semver: 'patch',
'app-name': APP_NAME,
},
context: {
eventName: 'pull_request',
Expand Down Expand Up @@ -187,6 +188,7 @@ tap.test('Should publish to npm without optic', async () => {
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
},
})
Expand All @@ -205,6 +207,7 @@ tap.test('Should not publish to npm if there is no npm token', async () => {
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
},
Expand All @@ -218,6 +221,7 @@ tap.test('Should publish to npm with optic', async () => {
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
'optic-token': 'optic-token',
},
Expand All @@ -235,6 +239,7 @@ tap.test('Should tag versions', async () => {
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -251,6 +256,7 @@ tap.test('Should call the release method', async () => {
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -268,6 +274,7 @@ tap.test('Should call the release method', async () => {
},
},
{
'app-name': APP_NAME,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -282,6 +289,7 @@ tap.test(
const data = clone(DEFAULT_ACTION_DATA)
data.context.payload.pull_request.merged = false
data.inputs = {
...data.inputs,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -297,6 +305,7 @@ tap.test("Should NOT use npm if the pr wasn't merged", async () => {
const data = clone(DEFAULT_ACTION_DATA)
data.context.payload.pull_request.merged = false
data.inputs = {
...data.inputs,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -310,6 +319,7 @@ tap.test("Should NOT tag version in git if the pr wasn't merged", async () => {
const data = clone(DEFAULT_ACTION_DATA)
data.context.payload.pull_request.merged = false
data.inputs = {
...data.inputs,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand Down Expand Up @@ -351,6 +361,7 @@ tap.test(
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -368,6 +379,7 @@ tap.test('Should call core.setFailed if the release fails', async () => {
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -388,6 +400,7 @@ tap.test(
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -408,6 +421,7 @@ tap.test(
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -426,6 +440,7 @@ tap.test('Should tag the major, minor & patch correctly for 0', async () => {

const data = clone(DEFAULT_ACTION_DATA)
data.inputs = {
...data.inputs,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -448,6 +463,7 @@ tap.test('Should tag the major, minor & patch correctly', async () => {

const data = clone(DEFAULT_ACTION_DATA)
data.inputs = {
...data.inputs,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -472,6 +488,7 @@ tap.test(
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -495,6 +512,7 @@ tap.test(

const data = clone(DEFAULT_ACTION_DATA)
data.inputs = {
...data.inputs,
'npm-token': 'a-token',
'optic-token': 'optic-token',
'sync-semver-tags': 'true',
Expand All @@ -519,6 +537,7 @@ tap.test(
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
'notify-linked-issues': 'true',
},
Expand All @@ -542,6 +561,7 @@ tap.test(
await release({
...DEFAULT_ACTION_DATA,
inputs: {
'app-name': APP_NAME,
'npm-token': 'a-token',
'notify-linked-issues': 'false',
},
Expand Down

0 comments on commit b93ddf2

Please sign in to comment.