Skip to content

Commit

Permalink
Merge pull request #10 from gregoranders/development
Browse files Browse the repository at this point in the history
0.0.6 Release
  • Loading branch information
gregoranders authored Jun 6, 2020
2 parents 4550aed + 53475a3 commit 4d3a534
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 63 deletions.
31 changes: 13 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ This action uploads an asset to a release, so your workflow can access it.
[![License][license-image]][license-url]
[![Issues][issues-image]][issues-url]


[![Master Build][master-build-image]][master-url] [![Master Coverage][master-coveralls-image]][master-coveralls-url] [![Master Version][master-version-image]][master-version-url]

[![Development Build][development-build-image]][development-url] [![Development Coverage][development-coveralls-image]][development-coveralls-url] [![Development Version][development-version-image]][development-version-url]
Expand All @@ -20,14 +19,15 @@ This action uploads an asset to a release, so your workflow can access it.
[![Main Language](https://img.shields.io/github/languages/top/gregoranders/nodejs-upload-asset)][code-metric-url] [![Languages](https://img.shields.io/github/languages/count/gregoranders/nodejs-upload-asset)][code-metric-url] [![Code Size](https://img.shields.io/github/languages/code-size/gregoranders/nodejs-upload-asset)][code-metric-url] [![Repo-Size](https://img.shields.io/github/repo-size/gregoranders/nodejs-upload-asset)][code-metric-url]

## Usage

```YML
...
- name: nodejs project information
id: projectinfo
uses: gregoranders/[email protected].5
uses: gregoranders/[email protected].6
- name: create release
id: createrelease
uses: gregoranders/[email protected].5
uses: gregoranders/[email protected].6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
Expand All @@ -36,21 +36,22 @@ This action uploads an asset to a release, so your workflow can access it.
target: ${{ github.ref }}
- name: upload asset
id: uploadasset
uses: gregoranders/[email protected].5
uses: gregoranders/[email protected].6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
url: ${{ steps.createrelease.outputs.upload_url }}
id: ${{ steps.createrelease.outputs.id }}
path: package.json
name: '${{ steps.projectinfo.outputs.name }} - ${{ steps.projectinfo.outputs.version }}'
...
```

#### Inputs/Outputs

```YML
inputs:
url:
description: 'Release Asset Upload Url'
id:
description: 'Release Id'
required: true
path:
description: 'Release Asset Path'
Expand All @@ -73,11 +74,13 @@ outputs:
## Development
### Clone repository
```SH
git clone https://github.com/gregoranders/nodejs-upload-asset
```

### Install dependencies

```SH
npm install
```
Expand All @@ -91,16 +94,19 @@ npm run build
### Testing

#### Test using [Jest](https://jestjs.io/)

```SH
npm test
```

### Run

```SH
npm start
```

### Clear

```SH
npm run clear
```
Expand All @@ -110,38 +116,27 @@ npm run clear
[development-url]: https://github.com/gregoranders/nodejs-upload-asset/tree/development
[repository-url]: https://github.com/gregoranders/nodejs-upload-asset
[code-metric-url]: https://github.com/gregoranders/nodejs-upload-asset/search?l=TypeScript

[travis-url]: https://travis-ci.org/gregoranders/nodejs-upload-asset
[travis-image]: https://travis-ci.org/gregoranders/nodejs-upload-asset.svg?branch=master

[daviddm-url]: https://david-dm.org/gregoranders/nodejs-upload-asset
[daviddm-image]: https://david-dm.org/gregoranders/nodejs-upload-asset.svg?branch=master

[license-url]: https://github.com/gregoranders/nodejs-upload-asset/blob/master/LICENSE
[license-image]: https://img.shields.io/github/license/gregoranders/nodejs-upload-asset.svg

[master-version-url]: https://github.com/gregoranders/nodejs-upload-asset/blob/master/package.json
[master-version-image]: https://img.shields.io/github/package-json/v/gregoranders/nodejs-upload-asset/master

[development-version-url]: https://github.com/gregoranders/nodejs-upload-asset/blob/development/package.json
[development-version-image]: https://img.shields.io/github/package-json/v/gregoranders/nodejs-upload-asset/development

[issues-url]: https://github.com/gregoranders/nodejs-upload-asset/issues
[issues-image]: https://img.shields.io/github/issues-raw/gregoranders/nodejs-upload-asset.svg

[master-build-image]: https://github.com/gregoranders/nodejs-upload-asset/workflows/Master%20CI/badge.svg
[development-build-image]: https://github.com/gregoranders/nodejs-upload-asset/workflows/Development%20CI/badge.svg

[master-coveralls-url]: https://coveralls.io/github/gregoranders/nodejs-upload-asset?branch=master
[master-coveralls-image]: https://img.shields.io/coveralls/github/gregoranders/nodejs-upload-asset/master
[development-coveralls-image]: https://img.shields.io/coveralls/github/gregoranders/nodejs-upload-asset/development
[development-coveralls-url]: https://coveralls.io/github/gregoranders/nodejs-upload-asset?branch=development

[code-maintainability-url]: https://codeclimate.com/github/gregoranders/nodejs-upload-asset/maintainability
[code-maintainability-image]: https://img.shields.io/codeclimate/maintainability/gregoranders/nodejs-upload-asset

[code-issues-url]: https://codeclimate.com/github/gregoranders/nodejs-upload-asset/maintainability
[code-issues-image]: https://img.shields.io/codeclimate/issues/gregoranders/nodejs-upload-asset

[code-tech-debt-url]: https://codeclimate.com/github/gregoranders/nodejs-upload-asset/maintainability
[code-tech-debt-image]: https://img.shields.io/codeclimate/tech-debt/gregoranders/nodejs-upload-asset
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: 'NodeJS Upload Asset'
description: 'Upload asset'
author: 'Gregor Anders <[email protected]>'
inputs:
url:
description: 'Release Asset Upload Url'
id:
description: 'Release ID'
required: true
path:
description: 'Release Asset Path'
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodejs-upload-asset",
"version": "0.0.5",
"version": "0.0.6",
"description": "NodeJS Upload Asset",
"main": "./dist/index.js",
"scripts": {
Expand Down Expand Up @@ -62,25 +62,25 @@
},
"homepage": "https://gregoranders.github.io/nodejs-upload-asset/",
"dependencies": {
"@actions/core": "1.2.3",
"@actions/github": "2.1.1"
"@actions/core": "1.2.4",
"@actions/github": "3.0.0"
},
"devDependencies": {
"@types/jest": "25.2.1",
"@types/mime": "2.0.1",
"@zeit/ncc": "0.22.1",
"@typescript-eslint/eslint-plugin": "2.28.0",
"@typescript-eslint/parser": "2.28.0",
"eslint": "6.8.0",
"eslint-config-prettier": "6.10.1",
"@types/jest": "25.2.3",
"@types/mime": "2.0.2",
"@zeit/ncc": "0.22.3",
"@typescript-eslint/eslint-plugin": "3.1.0",
"@typescript-eslint/parser": "3.1.0",
"eslint": "7.2.0",
"eslint-config-prettier": "6.11.0",
"eslint-plugin-prettier": "3.1.3",
"husky": "4.2.5",
"jest": "25.4.0",
"prettier": "2.0.4",
"mime": "2.4.4",
"jest": "26.0.1",
"prettier": "2.0.5",
"mime": "2.4.6",
"rimraf": "3.0.2",
"ts-jest": "25.4.0",
"ts-node": "8.8.2",
"typescript": "3.8.3"
"ts-jest": "26.1.0",
"ts-node": "8.10.2",
"typescript": "3.9.5"
}
}
17 changes: 7 additions & 10 deletions src/action.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/camelcase */
import { clearTestEnvironment, uploadReleaseAssetMock, setInput, setOutputMock } from './testUtils';

import { run as testSubject } from './action';
Expand All @@ -9,31 +8,31 @@ describe('nodejs-upload-asset', () => {
Reflect.deleteProperty(process.env, 'GITHUB_TOKEN');
});

it('no url provided', async () => {
it('no id provided', async () => {
setInput('path', 'bar');
return expect(testSubject()).rejects.toStrictEqual(Error('Input required and not supplied: url'));
return expect(testSubject()).rejects.toStrictEqual(Error('Input required and not supplied: id'));
});

it('no path provided', async () => {
setInput('url', 'foo');
setInput('id', '1');
return expect(testSubject()).rejects.toStrictEqual(Error('Input required and not supplied: path'));
});

it('missing GITHUB_TOKEN', async () => {
setInput('url', 'foo');
setInput('id', '1');
setInput('path', 'bar');
return expect(testSubject()).resolves.toHaveCoreError(/Missing GITHUB_TOKEN/);
});

it('invalid path', async () => {
setInput('url', 'foo');
setInput('id', '1');
setInput('path', 'bar');
process.env.GITHUB_TOKEN = 'abcd';
return expect(testSubject()).resolves.toHaveCoreError(/lstat/);
});

it('valid input and GITHUB_TOKEN - upload', async () => {
setInput('url', 'foo');
setInput('id', '1');
setInput('path', '.editorconfig');
process.env.GITHUB_TOKEN = 'abcd';
uploadReleaseAssetMock.mockReturnValue({
Expand All @@ -45,7 +44,6 @@ describe('nodejs-upload-asset', () => {
return testSubject().then(() => {
expect(uploadReleaseAssetMock).toHaveBeenCalledNthWith(1, {
name: '.editorconfig',
url: 'foo',
});
expect(setOutputMock).toHaveBeenCalledTimes(2);
expect(setOutputMock).toHaveCoreOutput('id', '1');
Expand All @@ -54,7 +52,7 @@ describe('nodejs-upload-asset', () => {
});

it('valid input and GITHUB_TOKEN - upload - custom mime type', async () => {
setInput('url', 'foo');
setInput('id', '1');
setInput('path', 'package.json');
setInput('mime', 'application/json');
process.env.GITHUB_TOKEN = 'abcd';
Expand All @@ -67,7 +65,6 @@ describe('nodejs-upload-asset', () => {
return testSubject().then(() => {
expect(uploadReleaseAssetMock).toHaveBeenCalledNthWith(1, {
name: 'package.json',
url: 'foo',
});
expect(setOutputMock).toHaveBeenCalledTimes(2);
expect(setOutputMock).toHaveCoreOutput('id', '1');
Expand Down
36 changes: 21 additions & 15 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import * as core from '@actions/core';
import { GitHub } from '@actions/github';
import { context, getOctokit } from '@actions/github';
import { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods/dist-types/';

import * as fs from 'fs';
import { getType } from 'mime';
import { basename, resolve } from 'path';

import { Octokit } from '@octokit/rest';

type ReposUploadReleaseAssetParams = Octokit.ReposUploadReleaseAssetParams;
type GitHub = ReturnType<typeof getOctokit>;
type ReposUploadReleaseAssetParams = RestEndpointMethodTypes['repos']['uploadReleaseAsset']['parameters'];

const uploadAsset = async (client: GitHub, params: ReposUploadReleaseAssetParams) => {
core.startGroup(`Uploading asset ${params.name} to release ${params.url}`);
// tslint:disable-next-line: no-any
const response = (await client.repos.uploadReleaseAsset(params)) as any;
const response = await client.repos.uploadReleaseAsset(params);
core.info(`Release asset ${response.data.name} created [id: ${response.data.id}]`);
core.endGroup();
return response.data;
Expand All @@ -26,40 +25,47 @@ const prepareHeaders = (fullPathChecked: string, mime: string) => {
};

const prepareParams = (
data: Buffer,
data: string,
headers: { 'content-length': number; 'content-type': string },
label: string,
name: string,
url: string,
): Octokit.ReposUploadReleaseAssetParams => {
release_id: string,
owner: string,
repo: string,
) => {
return {
data,
headers,
label,
name,
url,
release_id: parseInt(release_id),
owner,
repo,
};
};

export const run = async () => {
export const run = async (): Promise<void> => {
const path = core.getInput('path', { required: true });

const name = core.getInput('name', { required: false }) || basename(path);
const label = core.getInput('label', { required: false });
const url = core.getInput('url', { required: true });
const id = core.getInput('id', { required: true });
const mime = core.getInput('mime', { required: false });

try {
if (!process.env.GITHUB_TOKEN) {
throw Error('Missing GITHUB_TOKEN');
}

const github = new GitHub(process.env.GITHUB_TOKEN);
const github = getOctokit(process.env.GITHUB_TOKEN);
const fullPathChecked: fs.PathLike = resolve(fs.realpathSync(path));
const headers = prepareHeaders(fullPathChecked, mime);
const data = fs.readFileSync(fullPathChecked);
const data = fs.readFileSync(fullPathChecked).toString('binary');

const asset = await uploadAsset(github, prepareParams(data, headers, label, name, url));
const asset = await uploadAsset(
github,
prepareParams(data, headers, label, name, id, context.repo.owner, context.repo.repo),
);

core.setOutput('id', asset.id.toString());
core.setOutput('url', asset.browser_download_url);
Expand Down
2 changes: 1 addition & 1 deletion src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const uploadReleaseAssetMock = jest.fn();

const githubMock = jest.mock('@actions/github', () => {
return {
GitHub: jest.fn().mockImplementation(() => {
getOctokit: jest.fn().mockImplementation(() => {
return {
repos: {
uploadReleaseAsset: uploadReleaseAssetMock,
Expand Down

0 comments on commit 4d3a534

Please sign in to comment.