create-release/tests/create-release.test.js
Christopher Sexton becafb2f61 Include body parameter for release
Fixes #4

This adds the ability to include the body parameter when creating the
release.

    - name: Checkout code
      uses: actions/checkout@master
    - name: Create Release
      id: create_release
      uses: actions/create-release@master
      with:
        tag_name: ${{ github.ref }}
        release_name: Release ${{ github.ref }}
        body: Release body

This also supports a multiline body:

    - name: Create Release
      id: create_release
      uses: actions/create-release@master
      with:
        tag_name: ${{ github.ref }}
        release_name: Release ${{ github.ref }}
        body: |
          This is a multiline body
          with more than one
          line

Or if you want the contents of a file:

    - name: Read CHANGELOG
      id: changelog
      run: |
        echo "::set-output name=body::$(cat CHANGELOG.md)"
    - name: Create Release
      id: create_release
      uses: actions/create-release@master
      with:
        release_name: Release ${{ github.ref }}
        body: ${{ steps.changelog.outputs.body }}

One decision I made in favor of less code was to send an empty body when
there was non present. If it would be preferred to send nothing in the
request for the `body` attribute I could instead compose the parameters
with an optional body attribute:

    releaseParams = {
      owner,
      repo,
      tag_name: tag,
      name: releaseName,
      draft,
      prerelease
    };

    if (body) releaseParams.body = body;

    const createReleaseResponse = await github.repos.createRelease( releaseParams );
2019-10-22 12:24:23 -04:00

166 lines
4.0 KiB
JavaScript

jest.mock('@actions/core');
jest.mock('@actions/github');
const core = require('@actions/core');
const { GitHub, context } = require('@actions/github');
const run = require('../src/create-release.js');
/* eslint-disable no-undef */
describe('Create Release', () => {
let createRelease;
beforeEach(() => {
createRelease = jest.fn().mockReturnValueOnce({
data: {
id: 'releaseId',
html_url: 'htmlUrl',
upload_url: 'uploadUrl'
}
});
context.repo = {
owner: 'owner',
repo: 'repo'
};
const github = {
repos: {
createRelease
}
};
GitHub.mockImplementation(() => github);
});
test('Create release endpoint is called', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('myBody')
.mockReturnValueOnce('false')
.mockReturnValueOnce('false');
await run();
expect(createRelease).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
tag_name: 'v1.0.0',
name: 'myRelease',
body: 'myBody',
draft: false,
prerelease: false
});
});
test('Draft release is created', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('myBody')
.mockReturnValueOnce('true')
.mockReturnValueOnce('false');
await run();
expect(createRelease).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
tag_name: 'v1.0.0',
name: 'myRelease',
body: 'myBody',
draft: true,
prerelease: false
});
});
test('Pre-release release is created', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('myBody')
.mockReturnValueOnce('false')
.mockReturnValueOnce('true');
await run();
expect(createRelease).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
tag_name: 'v1.0.0',
name: 'myRelease',
body: 'myBody',
draft: false,
prerelease: true
});
});
test('Release with empty body is created', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('') // <-- The default value for body in action.yml
.mockReturnValueOnce('false')
.mockReturnValueOnce('false');
await run();
expect(createRelease).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
tag_name: 'v1.0.0',
name: 'myRelease',
body: '',
draft: false,
prerelease: false
});
});
test('Outputs are set', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('myBody')
.mockReturnValueOnce('false')
.mockReturnValueOnce('false');
core.setOutput = jest.fn();
await run();
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'id', 'releaseId');
expect(core.setOutput).toHaveBeenNthCalledWith(2, 'html_url', 'htmlUrl');
expect(core.setOutput).toHaveBeenNthCalledWith(3, 'upload_url', 'uploadUrl');
});
test('Action fails elegantly', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('myBody')
.mockReturnValueOnce('false')
.mockReturnValueOnce('false');
createRelease.mockRestore();
createRelease.mockImplementation(() => {
throw new Error('Error creating release');
});
core.setOutput = jest.fn();
core.setFailed = jest.fn();
await run();
expect(createRelease).toHaveBeenCalled();
expect(core.setFailed).toHaveBeenCalledWith('Error creating release');
expect(core.setOutput).toHaveBeenCalledTimes(0);
});
});