Skip to content

Commit

Permalink
- add setting to make the action fail if an asset fails to upload, se…
Browse files Browse the repository at this point in the history
…t to false by default
  • Loading branch information
mikepenz committed Mar 14, 2024
1 parent 7c0c85c commit 947b616
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The following are optional as `step.with` keys
| `name` | String | Name of the release. defaults to tag name |
| `tag_name` | String | Name of a tag. defaults to `github.ref` |
| `fail_on_unmatched_files` | Boolean | Indicator of whether to fail if any of the `files` globs match nothing |
| `fail_on_asset_upload_issue` | Boolean | Indicator of whether to fail if any of the `assets` fails to upload |
| `repository` | String | Name of a target repository in `<owner>/<repo>` format. Defaults to GITHUB_REPOSITORY env variable |
| `target_commitish` | String | Commitish value that determines where the Git tag is created from. Can be any branch or commit SHA. Defaults to repository default branch. |
| `token` | String | Secret GitHub Personal Access Token. Defaults to `${{ github.token }}` |
Expand Down
8 changes: 8 additions & 0 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ describe('util', () => {
input_name: undefined,
input_tag_name: undefined,
input_fail_on_unmatched_files: false,
input_fail_on_asset_upload_issue: false,
input_target_commitish: undefined,
input_discussion_category_name: undefined,
input_generate_release_notes: false
Expand All @@ -141,6 +142,7 @@ describe('util', () => {
input_name: undefined,
input_tag_name: undefined,
input_fail_on_unmatched_files: false,
input_fail_on_asset_upload_issue: false,
input_target_commitish: 'affa18ef97bc9db20076945705aba8c516139abd',
input_discussion_category_name: undefined,
input_generate_release_notes: false
Expand All @@ -165,6 +167,7 @@ describe('util', () => {
input_name: undefined,
input_tag_name: undefined,
input_fail_on_unmatched_files: false,
input_fail_on_asset_upload_issue: false,
input_target_commitish: undefined,
input_discussion_category_name: 'releases',
input_generate_release_notes: false
Expand All @@ -190,6 +193,7 @@ describe('util', () => {
input_name: undefined,
input_tag_name: undefined,
input_fail_on_unmatched_files: false,
input_fail_on_asset_upload_issue: false,
input_target_commitish: undefined,
input_discussion_category_name: undefined,
input_generate_release_notes: true
Expand Down Expand Up @@ -218,6 +222,7 @@ describe('util', () => {
input_name: undefined,
input_tag_name: undefined,
input_fail_on_unmatched_files: false,
input_fail_on_asset_upload_issue: false,
input_target_commitish: undefined,
input_discussion_category_name: undefined,
input_generate_release_notes: false
Expand All @@ -244,6 +249,7 @@ describe('util', () => {
input_name: undefined,
input_tag_name: undefined,
input_fail_on_unmatched_files: false,
input_fail_on_asset_upload_issue: false,
input_target_commitish: undefined,
input_discussion_category_name: undefined,
input_generate_release_notes: false
Expand All @@ -269,6 +275,7 @@ describe('util', () => {
input_name: undefined,
input_tag_name: undefined,
input_fail_on_unmatched_files: false,
input_fail_on_asset_upload_issue: false,
input_target_commitish: undefined,
input_discussion_category_name: undefined,
input_generate_release_notes: false
Expand All @@ -293,6 +300,7 @@ describe('util', () => {
input_name: undefined,
input_tag_name: undefined,
input_fail_on_unmatched_files: false,
input_fail_on_asset_upload_issue: false,
input_target_commitish: undefined,
input_discussion_category_name: undefined,
input_generate_release_notes: false
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ inputs:
fail_on_unmatched_files:
description: "Fails if any of the `files` globs match nothing. Defaults to false"
required: false
fail_on_asset_upload_issue:
description: "Fails if any of the `files` fail to upload during the release. Defaults to false"
required: false
repository:
description: "Repository to make releases against, in <owner>/<repo> format"
required: false
Expand Down
62 changes: 47 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,41 @@ const upload = async (config, github, url, path, currentAssets) => {
core.info(`⬆️ Uploading ${name}...`);
const endpoint = new URL(url);
endpoint.searchParams.append('name', name);
const resp = await fetch(endpoint, {
headers: {
'content-length': `${size}`,
'content-type': mime,
authorization: `token ${config.github_token}`
},
method: 'POST',
body
});
const json = await resp.json();
if (resp.status !== 201) {
throw new Error(`Failed to upload release asset ${name}. received status code ${resp.status}\n${json.message}\n${JSON.stringify(json.errors)}`);
try {
const resp = await fetch(endpoint, {
headers: {
'content-length': `${size}`,
'content-type': mime,
authorization: `token ${config.github_token}`
},
method: 'POST',
body
});
try {
const json = await resp.json();
if (resp.status !== 201) {
throw new Error(`Failed to upload release asset ${name}. received status code ${resp.status}\n${json.message}\n${JSON.stringify(json.errors)}`);
}
return json;
}
catch (jsonError) {
if (config.input_fail_on_asset_upload_issue) {
throw jsonError;
}
else {
core.error(`Failed to parse server response for asset ${name}. Received error ${jsonError}`);
}
}
}
return json;
catch (error) {
if (config.input_fail_on_asset_upload_issue) {
throw error;
}
else {
core.error(`Failed to upload the asset ${name}. Received error ${error}`);
}
}
return {};
};
exports.upload = upload;
const release = async (config, releaser, maxRetries = 3) => {
Expand Down Expand Up @@ -261,7 +282,12 @@ async function run() {
if (config.input_files && config.input_files?.length > 0) {
const patterns = (0, util_1.unmatchedPatterns)(config.input_files);
for (const pattern of patterns) {
core.warning(`🤔 Pattern '${pattern}' does not match any files.`);
if (config.input_fail_on_unmatched_files) {
throw new Error(`⚠️ Pattern '${pattern}' does not match any files.`);
}
else {
core.warning(`🤔 Pattern '${pattern}' does not match any files.`);
}
}
if (patterns.length > 0 && config.input_fail_on_unmatched_files) {
throw new Error(`⚠️ There were unmatched files`);
Expand All @@ -288,7 +314,12 @@ async function run() {
if (config.input_files && config.input_files?.length > 0) {
const files = (0, util_1.paths)(config.input_files);
if (files.length === 0) {
core.warning(`🤔 ${config.input_files} not include valid file.`);
if (config.input_fail_on_unmatched_files) {
throw new Error(`⚠️ ${config.input_files} not include valid file.`);
}
else {
core.warning(`🤔 ${config.input_files} not include valid file.`);
}
}
const currentAssets = rel.assets;
const assets = await Promise.all(files.map(async (path) => {
Expand Down Expand Up @@ -378,6 +409,7 @@ const parseConfig = (env) => {
input_draft: env.INPUT_DRAFT ? env.INPUT_DRAFT === 'true' : undefined,
input_prerelease: env.INPUT_PRERELEASE ? env.INPUT_PRERELEASE === 'true' : undefined,
input_fail_on_unmatched_files: env.INPUT_FAIL_ON_UNMATCHED_FILES === 'true',
input_fail_on_asset_upload_issue: env.INPUT_FAIL_ON_ASSET_UPLOAD_ISSUE === 'true',
input_target_commitish: env.INPUT_TARGET_COMMITISH || undefined,
input_discussion_category_name: env.INPUT_DISCUSSION_CATEGORY_NAME || undefined,
input_generate_release_notes: env.INPUT_GENERATE_RELEASE_NOTES === 'true',
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

52 changes: 35 additions & 17 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,42 @@ export const upload = async (
core.info(`⬆️ Uploading ${name}...`)
const endpoint = new URL(url)
endpoint.searchParams.append('name', name)
const resp = await fetch(endpoint, {
headers: {
'content-length': `${size}`,
'content-type': mime,
authorization: `token ${config.github_token}`
},
method: 'POST',
body
})
const json = await resp.json()
if (resp.status !== 201) {
throw new Error(
`Failed to upload release asset ${name}. received status code ${resp.status}\n${json.message}\n${JSON.stringify(
json.errors
)}`
)
try {
const resp = await fetch(endpoint, {
headers: {
'content-length': `${size}`,
'content-type': mime,
authorization: `token ${config.github_token}`
},
method: 'POST',
body
})

try {
const json = await resp.json()
if (resp.status !== 201) {
throw new Error(
`Failed to upload release asset ${name}. received status code ${resp.status}\n${json.message}\n${JSON.stringify(
json.errors
)}`
)
}
return json
} catch (jsonError) {
if (config.input_fail_on_asset_upload_issue) {
throw jsonError
} else {
core.error(`Failed to parse server response for asset ${name}. Received error ${jsonError}`)
}
}
} catch (error) {
if (config.input_fail_on_asset_upload_issue) {
throw error
} else {
core.error(`Failed to upload the asset ${name}. Received error ${error}`)
}
}
return json
return {}
}

export const release = async (config: Config, releaser: Releaser, maxRetries = 3): Promise<Release> => {
Expand Down
12 changes: 10 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ async function run(): Promise<void> {
if (config.input_files && config.input_files?.length > 0) {
const patterns = unmatchedPatterns(config.input_files)
for (const pattern of patterns) {
core.warning(`🤔 Pattern '${pattern}' does not match any files.`)
if (config.input_fail_on_unmatched_files) {
throw new Error(`⚠️ Pattern '${pattern}' does not match any files.`)
} else {
core.warning(`🤔 Pattern '${pattern}' does not match any files.`)
}
}
if (patterns.length > 0 && config.input_fail_on_unmatched_files) {
throw new Error(`⚠️ There were unmatched files`)
Expand Down Expand Up @@ -42,7 +46,11 @@ async function run(): Promise<void> {
if (config.input_files && config.input_files?.length > 0) {
const files = paths(config.input_files)
if (files.length === 0) {
core.warning(`🤔 ${config.input_files} not include valid file.`)
if (config.input_fail_on_unmatched_files) {
throw new Error(`⚠️ ${config.input_files} not include valid file.`)
} else {
core.warning(`🤔 ${config.input_files} not include valid file.`)
}
}
const currentAssets = rel.assets
const assets = await Promise.all(
Expand Down
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Config {
input_draft?: boolean
input_prerelease?: boolean
input_fail_on_unmatched_files?: boolean
input_fail_on_asset_upload_issue?: boolean
input_target_commitish?: string
input_discussion_category_name?: string
input_generate_release_notes?: boolean
Expand Down Expand Up @@ -59,6 +60,7 @@ export const parseConfig = (env: Env): Config => {
input_draft: env.INPUT_DRAFT ? env.INPUT_DRAFT === 'true' : undefined,
input_prerelease: env.INPUT_PRERELEASE ? env.INPUT_PRERELEASE === 'true' : undefined,
input_fail_on_unmatched_files: env.INPUT_FAIL_ON_UNMATCHED_FILES === 'true',
input_fail_on_asset_upload_issue: env.INPUT_FAIL_ON_ASSET_UPLOAD_ISSUE === 'true',
input_target_commitish: env.INPUT_TARGET_COMMITISH || undefined,
input_discussion_category_name: env.INPUT_DISCUSSION_CATEGORY_NAME || undefined,
input_generate_release_notes: env.INPUT_GENERATE_RELEASE_NOTES === 'true',
Expand Down

0 comments on commit 947b616

Please sign in to comment.