Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Optional Arguments
- `target_commit`: Sets the commit hash or branch for the tag to be based on (Default: the default branch, usually `main`).
- `body`: Content of the release text (Default: `""`).
- `repo_name`: Specify the name of the GitHub repository in which the GitHub release will be created, edited, and deleted. If the repository is other than the current, it is required to create a personal access token with `repo`, `user`, `admin:repo_hook` scopes to the foreign repository and add it as a secret. (Default: current repository).
- `check_duplicates`: Enable or disable the check for existing assets with the same name. If enabled, the action will skip uploading the asset if it already exists. (Default: `true`).

## Output variables

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ inputs:
description: 'Specifies the commitish value that determines where the Git tag is created from. Can be any branch or commit SHA. Unused if the Git tag already exists. Default: the repository\"s default branch (usually `main`).'
repo_name:
description: 'Specify the name of the GitHub repository in which the GitHub release will be created, edited, and deleted. If the repository is other than the current, it is required to create a personal access token with `repo`, `user`, `admin:repo_hook` scopes to the foreign repository and add it as a secret. Defaults to the current repository'
check_duplicates:
description: 'Check for duplicate assets with the same name in the release and skip uploading of those. Defaults to "true".'
default: true
outputs:
browser_download_url:
description: 'The publicly available URL of the asset.'
Expand Down
33 changes: 18 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function get_release_by_tag(tag, draft, prerelease, make_latest, release_name, b
return release;
});
}
function upload_to_release(release, file, asset_name, tag, overwrite, octokit) {
function upload_to_release(release, file, asset_name, tag, overwrite, octokit, check_duplicates) {
return __awaiter(this, void 0, void 0, function* () {
const stat = fs.statSync(file);
if (!stat.isFile()) {
Expand All @@ -129,22 +129,24 @@ function upload_to_release(release, file, asset_name, tag, overwrite, octokit) {
core.debug(`Skipping ${file}, since its size is 0`);
return;
}
// Check for duplicates.
const assets = yield octokit.paginate(repoAssets, Object.assign(Object.assign({}, repo()), { release_id: release.data.id }));
const duplicate_asset = assets.find(a => a.name === asset_name);
if (duplicate_asset !== undefined) {
if (overwrite) {
core.debug(`An asset called ${asset_name} already exists in release ${tag} so we'll overwrite it.`);
yield octokit.request(deleteAssets, Object.assign(Object.assign({}, repo()), { asset_id: duplicate_asset.id }));
if (check_duplicates) {
// Check for duplicates.
const assets = yield octokit.paginate(repoAssets, Object.assign(Object.assign({}, repo()), { release_id: release.data.id }));
const duplicate_asset = assets.find(a => a.name === asset_name);
if (duplicate_asset !== undefined) {
if (overwrite) {
core.debug(`An asset called ${asset_name} already exists in release ${tag} so we'll overwrite it.`);
yield octokit.request(deleteAssets, Object.assign(Object.assign({}, repo()), { asset_id: duplicate_asset.id }));
}
else {
core.setFailed(`An asset called ${asset_name} already exists.`);
return duplicate_asset.browser_download_url;
}
}
else {
core.setFailed(`An asset called ${asset_name} already exists.`);
return duplicate_asset.browser_download_url;
core.debug(`No pre-existing asset called ${asset_name} found in release ${tag}. All good.`);
}
}
else {
core.debug(`No pre-existing asset called ${asset_name} found in release ${tag}. All good.`);
}
core.debug(`Uploading ${file} to ${asset_name} in release ${tag}.`);
// @ts-ignore
const uploaded_asset = yield (0, attempt_1.retry)(() => __awaiter(this, void 0, void 0, function* () {
Expand Down Expand Up @@ -195,6 +197,7 @@ function run() {
const make_latest = core.getInput('make_latest') != 'false' ? true : false;
const release_name = core.getInput('release_name');
const target_commit = core.getInput('target_commit');
const check_duplicates = core.getInput('check_duplicates') != 'false' ? true : false;
const body = core
.getInput('body')
.replace(/%0A/gi, '\n')
Expand All @@ -207,7 +210,7 @@ function run() {
if (files.length > 0) {
for (const file_ of files) {
const asset_name = path.basename(file_);
const asset_download_url = yield upload_to_release(release, file_, asset_name, tag, overwrite, octokit);
const asset_download_url = yield upload_to_release(release, file_, asset_name, tag, overwrite, octokit, check_duplicates);
core.setOutput('browser_download_url', asset_download_url);
}
}
Expand All @@ -219,7 +222,7 @@ function run() {
const asset_name = core.getInput('asset_name') !== ''
? core.getInput('asset_name').replace(/\$tag/g, tag)
: path.basename(file);
const asset_download_url = yield upload_to_release(release, file, asset_name, tag, overwrite, octokit);
const asset_download_url = yield upload_to_release(release, file, asset_name, tag, overwrite, octokit, check_duplicates);
core.setOutput('browser_download_url', asset_download_url);
}
}
Expand Down
53 changes: 30 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ async function upload_to_release(
asset_name: string,
tag: string,
overwrite: boolean,
octokit: ReturnType<(typeof github)['getOctokit']>
octokit: ReturnType<(typeof github)['getOctokit']>,
check_duplicates: boolean
): Promise<undefined | string> {
const stat = fs.statSync(file)
if (!stat.isFile()) {
Expand All @@ -132,29 +133,31 @@ async function upload_to_release(
return
}

// Check for duplicates.
const assets: RepoAssetsResp = await octokit.paginate(repoAssets, {
...repo(),
release_id: release.data.id
})
const duplicate_asset = assets.find(a => a.name === asset_name)
if (duplicate_asset !== undefined) {
if (overwrite) {
if (check_duplicates) {
// Check for duplicates.
const assets: RepoAssetsResp = await octokit.paginate(repoAssets, {
...repo(),
release_id: release.data.id
})
const duplicate_asset = assets.find(a => a.name === asset_name)
if (duplicate_asset !== undefined) {
if (overwrite) {
core.debug(
`An asset called ${asset_name} already exists in release ${tag} so we'll overwrite it.`
)
await octokit.request(deleteAssets, {
...repo(),
asset_id: duplicate_asset.id
})
} else {
core.setFailed(`An asset called ${asset_name} already exists.`)
return duplicate_asset.browser_download_url
}
} else {
core.debug(
`An asset called ${asset_name} already exists in release ${tag} so we'll overwrite it.`
`No pre-existing asset called ${asset_name} found in release ${tag}. All good.`
)
await octokit.request(deleteAssets, {
...repo(),
asset_id: duplicate_asset.id
})
} else {
core.setFailed(`An asset called ${asset_name} already exists.`)
return duplicate_asset.browser_download_url
}
} else {
core.debug(
`No pre-existing asset called ${asset_name} found in release ${tag}. All good.`
)
}

core.debug(`Uploading ${file} to ${asset_name} in release ${tag}.`)
Expand Down Expand Up @@ -219,6 +222,8 @@ async function run(): Promise<void> {
const make_latest = core.getInput('make_latest') != 'false' ? true : false
const release_name = core.getInput('release_name')
const target_commit = core.getInput('target_commit')
const check_duplicates =
core.getInput('check_duplicates') != 'false' ? true : false
const body = core
.getInput('body')
.replace(/%0A/gi, '\n')
Expand Down Expand Up @@ -250,7 +255,8 @@ async function run(): Promise<void> {
asset_name,
tag,
overwrite,
octokit
octokit,
check_duplicates
)
core.setOutput('browser_download_url', asset_download_url)
}
Expand All @@ -268,7 +274,8 @@ async function run(): Promise<void> {
asset_name,
tag,
overwrite,
octokit
octokit,
check_duplicates
)
core.setOutput('browser_download_url', asset_download_url)
}
Expand Down