Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Resolve content-type from file extension #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
asset_path: ./my-artifact.zip
asset_name: my-artifact.zip
asset_content_type: application/zip
# asset_content_type: application/zip # Optional, will be derived from file extension
```

This will upload a release artifact to an existing release, outputting the `browser_download_url` for the asset which could be handled by a third party service, or by GitHub Actions for additional uses. For more information, see the GitHub Documentation for the [upload a release asset](https://developer.github.com/v3/repos/releases/#upload-a-release-asset) endpoint.
Expand Down
149 changes: 145 additions & 4 deletions dist/index.js

Large diffs are not rendered by default.

46 changes: 16 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"dependencies": {
"@actions/core": "^1.0.0",
"@actions/exec": "^1.0.0",
"@actions/github": "^1.0.0"
"@actions/github": "^1.0.0",
"mime": "^2.4.4"
},
"devDependencies": {
"@zeit/ncc": "^0.20.4",
Expand Down
16 changes: 14 additions & 2 deletions src/upload-release-asset.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const core = require('@actions/core');
const { GitHub } = require('@actions/github');
const fs = require('fs');
const mime = require('mime');

async function run() {
try {
Expand All @@ -11,13 +12,24 @@ async function run() {
const uploadUrl = core.getInput('upload_url', { required: true });
const assetPath = core.getInput('asset_path', { required: true });
const assetName = core.getInput('asset_name', { required: true });
const assetContentType = core.getInput('asset_content_type', { required: true });
const assetContentType = core.getInput('asset_content_type', { required: false });
let contentType;

if (assetContentType) {
contentType = assetContentType;
} else {
contentType = mime.getType(assetPath);

if (!contentType) {
throw new Error('Mime type could not be resolved from file extension');
}
}

// Determine content-length for header to upload asset
const contentLength = filePath => fs.statSync(filePath).size;

// Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset for more information
const headers = { 'content-type': assetContentType, 'content-length': contentLength(assetPath) };
const headers = { 'content-type': contentType, 'content-length': contentLength(assetPath) };

// Upload a release asset
// API Documentation: https://developer.github.com/v3/repos/releases/#upload-a-release-asset
Expand Down
34 changes: 34 additions & 0 deletions tests/upload-release-asset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,38 @@ describe('Upload Release Asset', () => {
expect(core.setFailed).toHaveBeenCalledWith('Error uploading release asset');
expect(core.setOutput).toHaveBeenCalledTimes(0);
});

test('Mime type is resolved from file extension', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('upload_url')
.mockReturnValueOnce('asset_path.zip')
.mockReturnValueOnce('asset_name');

await run();

expect(uploadReleaseAsset).toHaveBeenCalledWith({
url: 'upload_url',
headers: { 'content-type': 'application/zip', 'content-length': 527 },
name: 'asset_name',
file: content
});
});

test('Action fails if mime type could not be resolved', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('upload_url')
.mockReturnValueOnce('asset_path')
.mockReturnValueOnce('asset_name');

core.setOutput = jest.fn();

core.setFailed = jest.fn();

await run();

expect(core.setFailed).toHaveBeenCalledWith('Mime type could not be resolved from file extension');
expect(core.setOutput).toHaveBeenCalledTimes(0);
});
});