Skip to content

Commit

Permalink
Update for releases
Browse files Browse the repository at this point in the history
  • Loading branch information
rfarine committed Nov 11, 2020
1 parent 4207629 commit 3989b12
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
tags:
- '*'

jobs:
prerelease:
runs-on: ubuntu-latest
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/testRelease.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: On Release
on:
release:
types: ['released']

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
path: action
- name: Fetch Latest Release
uses: thebritican/fetch-latest-release@v2.0.0
id: latestRelease
- name: Test farmersdog/release action
uses: './action'
with:
ghToken: ${{ secrets.GITHUB_TOKEN }}
prerelease: 'false'
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ Run `yarn lint:watch` to watch for ESLint errors/warnings.

## Example usage

### Prerelease

```
on:
push:
tags:
- '*'
```
...
```
uses: actions/release@v1
with:
ghToken: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -52,3 +53,21 @@ with:
## prerelease: false # default is true
previousTag: "v0.0.9"
```

### Release

One might want to create a simple make command that triggers this workflow from the command line or a slack notification
that allows this to be triggered.

```
on:
release:
types: ['released']
...
uses: actions/release@v1
with:
ghToken: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ github.event.release.tag_name }}
prerelease: false
```
1 change: 0 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ inputs:
required: true
chStoryUrl:
description: 'Clubhouse URL (ie. https://app.clubhouse.io/org/story)'
required: true
tag:
description: 'Github tag'
required: true
Expand Down
41 changes: 24 additions & 17 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ async function run() {
try {
const previousTag = core.getInput('previousTag');
const prerelease = core.getInput('prerelease');
const isPreRelease = prerelease === 'true';
const ghToken = core.getInput('ghToken');
const chStoryUrl = core.getInput('chStoryUrl');
const { ref } = github.context;
Expand All @@ -105,13 +106,7 @@ async function run() {
return core.setFailed('Must provide ghToken');
}

if (!chStoryUrl) {
return core.setFailed('Must provide chStoryUrl');
}

// Mask tokens:
core.setSecret('ghToken');
core.setSecret('chStoryUrl');

if (!validTag || !validTag.groups.tag) {
return core.setFailed('Tag must follow format rules: v##.##.##');
Expand All @@ -120,20 +115,28 @@ async function run() {
const {
groups: { tag },
} = validTag;
const octokit = github.getOctokit(ghToken);

core.info(
`Tag ${tag}: Creating a ${isPreRelease ? 'prerelease' : 'release'}...`
);

/* CREATE PRERELEASE */
if (isPreRelease) {
// CH Story URL is required for prerelease
if (!chStoryUrl) {
return core.setFailed('Must provide chStoryUrl');
}

// Create prerelease
// Note on bools: https://github.com/actions/toolkit/issues/361
if (prerelease === 'true') {
core.setSecret('chStoryUrl');

// Previous Tag is required for prerelease
if (!previousTag) {
return core.setFailed(
'Must provide a previousTag to create a prerelease'
);
}

core.info(`Tag ${tag}: Creating a prerelease...`);

// Get list of commits
const octokit = github.getOctokit(ghToken);
const {
data: { commits },
} = await octokit.repos.compareCommits({
Expand All @@ -144,8 +147,8 @@ async function run() {

const formattedCommits = formatCommits(commits);
const changelog = generateChangelog(formattedCommits);

core.info(changelog);
// Create a github release (type: prerelease) w/ changelog attached

return await octokit.repos.createRelease({
...github.context.repo,
Expand All @@ -156,9 +159,13 @@ async function run() {
});
}

// If already a prerelease, move to release state
if (prerelease === 'false') {
core.info('hello');
/* UPDATE PRERELEASE TO RELEASE */
if (!isPreRelease) {
return await octokit.repos.updateRelease({
...github.context.repo,
tag_name: tag,
prerelease: false,
});
}

return core.setFailed(
Expand Down
41 changes: 24 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export async function run() {
try {
const previousTag = core.getInput('previousTag');
const prerelease = core.getInput('prerelease');
const isPreRelease = prerelease === 'true';
const ghToken = core.getInput('ghToken');
const chStoryUrl = core.getInput('chStoryUrl');
const { ref } = github.context;
Expand All @@ -89,13 +90,7 @@ export async function run() {
return core.setFailed('Must provide ghToken');
}

if (!chStoryUrl) {
return core.setFailed('Must provide chStoryUrl');
}

// Mask tokens:
core.setSecret('ghToken');
core.setSecret('chStoryUrl');

if (!validTag || !validTag.groups.tag) {
return core.setFailed('Tag must follow format rules: v##.##.##');
Expand All @@ -104,20 +99,28 @@ export async function run() {
const {
groups: { tag },
} = validTag;
const octokit = github.getOctokit(ghToken);

core.info(
`Tag ${tag}: Creating a ${isPreRelease ? 'prerelease' : 'release'}...`
);

/* CREATE PRERELEASE */
if (isPreRelease) {
// CH Story URL is required for prerelease
if (!chStoryUrl) {
return core.setFailed('Must provide chStoryUrl');
}

// Create prerelease
// Note on bools: https://github.com/actions/toolkit/issues/361
if (prerelease === 'true') {
core.setSecret('chStoryUrl');

// Previous Tag is required for prerelease
if (!previousTag) {
return core.setFailed(
'Must provide a previousTag to create a prerelease'
);
}

core.info(`Tag ${tag}: Creating a prerelease...`);

// Get list of commits
const octokit = github.getOctokit(ghToken);
const {
data: { commits },
} = await octokit.repos.compareCommits({
Expand All @@ -128,8 +131,8 @@ export async function run() {

const formattedCommits = formatCommits(commits);
const changelog = generateChangelog(formattedCommits);

core.info(changelog);
// Create a github release (type: prerelease) w/ changelog attached

return await octokit.repos.createRelease({
...github.context.repo,
Expand All @@ -140,9 +143,13 @@ export async function run() {
});
}

// If already a prerelease, move to release state
if (prerelease === 'false') {
core.info('hello');
/* UPDATE PRERELEASE TO RELEASE */
if (!isPreRelease) {
return await octokit.repos.updateRelease({
...github.context.repo,
tag_name: tag,
prerelease: false,
});
}

return core.setFailed(
Expand Down
23 changes: 23 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,28 @@ describe('Release', () => {
);
});
});

describe('on release', () => {
beforeEach(() => {
// Reset inputs
inputs = {};
});

test('should call core.info', async () => {
github.context.ref = 'refs/tags/v20.0.0';
inputs = {
chStoryUrl,
ghToken: '123',
prerelease: 'false',
previousTag: 'v20.0.0',
};

await run();

expect(core.info).toHaveBeenCalledWith(
'Tag v20.0.0: Creating a release...'
);
});
});
});
});

0 comments on commit 3989b12

Please sign in to comment.