Skip to content

Commit

Permalink
Add release name generation
Browse files Browse the repository at this point in the history
  • Loading branch information
darioblanco committed Mar 9, 2020
1 parent 075026d commit de09441
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dist/run.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/lib/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import * as path from 'path';
import * as core from '@actions/core';
import { context, GitHub } from '@actions/github';

export function renderReleaseName(
draft: boolean,
prerelease: boolean,
app?: string,
) {
let releaseName = `release ${app || ''}`;
if (draft || prerelease) {
releaseName = `${draft ? 'draft ' : ''}${prerelease ? 'prerelease ' : ''}${app || ''}`;
}
// Capitalize string
return `${releaseName[0].toUpperCase()}${releaseName.slice(1)}`.trim();
}

export function renderReleaseBody(
templatePath: string,
app: string,
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core';
import { GitHub } from '@actions/github';

import { createGithubRelease, renderReleaseBody, createGitTag } from './lib/release';
import { createGithubRelease, renderReleaseBody, createGitTag, renderReleaseName } from './lib/release';
import { commitParser } from './lib/commits';
import { retrieveLastReleasedVersion, bumpVersion } from './lib/version';

Expand Down Expand Up @@ -43,8 +43,8 @@ export async function run() {
// Won't replace it if release tag is given manually
const releaseVersion = releaseTag.replace(tagPrefix, '');
const releaseName =
core.getInput('releaseName', { required: false }) || `${app} ${releaseVersion}`;

core.getInput('releaseName', { required: false }) ||
renderReleaseName(draft, prerelease, monorepo ? app : undefined);
const body = renderReleaseBody(
templatePath,
app,
Expand Down
49 changes: 39 additions & 10 deletions test/lib/release.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve as pathResolve } from 'path';
import { setOutput } from '@actions/core';

import {
createGithubRelease, renderReleaseBody, createGitTag
createGithubRelease, renderReleaseBody, createGitTag, renderReleaseName
} from '@minddocdev/mou-release-action/lib/release';

jest.mock('path');
Expand All @@ -26,6 +26,33 @@ const createReleaseResponse = {
};

describe('release', () => {
describe('render release name', () => {
test('with draft and prerelease', () => {
expect(renderReleaseName(true, true)).toBe('Draft prerelease');
});
test('with draft', () => {
expect(renderReleaseName(true, false)).toBe('Draft');
});
test('with prerelease', () => {
expect(renderReleaseName(false, true)).toBe('Prerelease');
});
test('when release is detected', () => {
expect(renderReleaseName(false, false)).toBe('Release');
});
test('with draft, prerelease and app', () => {
expect(renderReleaseName(true, true, 'myapp')).toBe('Draft prerelease myapp');
});
test('with draft and app', () => {
expect(renderReleaseName(true, false, 'myapp')).toBe('Draft myapp');
});
test('with prerelease and app', () => {
expect(renderReleaseName(false, true, 'myapp')).toBe('Prerelease myapp');
});
test('when release is detected and app is given', () => {
expect(renderReleaseName(false, false, 'myapp')).toBe('Release myapp');
});
});

describe('render release template', () => {
const app = 'myapp';
const releaseVersion = '1.0.0';
Expand All @@ -43,15 +70,17 @@ describe('release', () => {
(pathResolve as jest.Mock)
.mockImplementation(() => `${__dirname}/fixtures/with-changelog.md`);

const changes = `\
- [#1](https://commiturl) First commit message ([@darioblanco](https://github.com/darioblanco))
- [#2](https://commiturl) Second commit message ([@darioblanco](https://github.com/darioblanco))`;
const tasks = `\
- [JIRA-123](https://myorg.atlassian.net/browse/JIRA-123)
- [JIRA-456](https://myorg.atlassian.net/browse/JIRA-456)`;
const pullRequests = `\
- [#1716](https://github.com/myorg/myrepo/pull/1716)
- [#1717](https://github.com/myorg/myrepo/pull/1717)`;
const changes = '' +
'- [#1](https://commiturl) First commit message ' +
'([@darioblanco](https://github.com/darioblanco))\n' +
'- [#2](https://commiturl) Second commit message ' +
'([@darioblanco](https://github.com/darioblanco))';
const tasks = '' +
'- [JIRA-123](https://myorg.atlassian.net/browse/JIRA-123)\n' +
'- [JIRA-456](https://myorg.atlassian.net/browse/JIRA-456)';
const pullRequests = '' +
'- [#1716](https://github.com/myorg/myrepo/pull/1716)\n' +
'- [#1717](https://github.com/myorg/myrepo/pull/1717)';
expect(renderReleaseBody(
'myTemplatePath.md', app, releaseVersion, changes, tasks, pullRequests,
)).toMatchSnapshot();
Expand Down
9 changes: 6 additions & 3 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@minddocdev/mou-release-action/lib/version';
import { run } from '@minddocdev/mou-release-action/main';
import {
createGitTag, createGithubRelease, renderReleaseBody
createGitTag, createGithubRelease, renderReleaseBody, renderReleaseName,
} from '@minddocdev/mou-release-action/lib/release';
import { commitParser } from '@minddocdev/mou-release-action/lib/commits';

Expand All @@ -18,7 +18,6 @@ jest.mock('@minddocdev/mou-release-action/lib/version');
describe('run', () => {
// Required input values
const app = 'fake-app';
const releaseName = 'fake-app';
const templatePath = 'RELEASE_DRAFT/default.md';
const token = 'faketoken';
// Default input values
Expand Down Expand Up @@ -70,6 +69,8 @@ describe('run', () => {
const baseTag = 'v1.0.0';
(retrieveLastReleasedVersion as jest.Mock).mockImplementation(() => baseTag);

const releaseName = `draft prerelease ${app}`;
(renderReleaseName as jest.Mock).mockImplementation(() => releaseName);
const releaseVersion = '1.0.5';
const releaseTag = `${tagPrefix}${releaseVersion}`;
(bumpVersion as jest.Mock).mockImplementation(() => releaseTag);
Expand All @@ -84,6 +85,7 @@ describe('run', () => {
undefined,
undefined,
);
expect(renderReleaseName).toBeCalledWith(draft, prerelease, undefined);
expect(renderReleaseBody).toBeCalledWith(
templatePath,
app,
Expand All @@ -97,7 +99,7 @@ describe('run', () => {
expect(createGithubRelease).toBeCalledWith(
expect.any(GitHub),
releaseTag,
`${app} ${releaseVersion}`,
releaseName,
body,
draft,
prerelease,
Expand All @@ -109,6 +111,7 @@ describe('run', () => {
const baseTag = 'v1.0.4';
const givenDraft = false;
const givenPrerelease = false;
const releaseName = 'fake-app';
const releaseTag = `mycustomprefix-1.0.6`;
const taskBaseUrl = 'https://myfaketask.url';
(getInput as jest.Mock).mockImplementation((name: string) => {
Expand Down

0 comments on commit de09441

Please sign in to comment.