Skip to content

Commit dfdf376

Browse files
Feat: Allow upload to draft release using a matrix (#136)
* Allow to upload to a draft release by its ID Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> * Update action inputs/outputs descriptions Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --------- Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Co-authored-by: Yair Morgenstern <yairm210@hotmail.com>
1 parent ebd922b commit dfdf376

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

‎action.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ inputs:
3838
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`).'
3939
repo_name:
4040
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'
41+
draft_id:
42+
description: 'Specify the ID of the draft release you want to upload to.'
4143
check_duplicates:
4244
description: 'Check for duplicate assets with the same name in the release and skip uploading of those. Defaults to "true".'
4345
default: true
4446
outputs:
4547
browser_download_url:
4648
description: 'The publicly available URL of the asset.'
49+
draft_id:
50+
description: 'The ID of the draft release created.'
4751
runs:
4852
using: 'node20'
4953
main: 'dist/index.js'

‎src/main.ts‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {retry} from '@lifeomic/attempt'
99

1010
const getRef = 'GET /repos/{owner}/{repo}/git/ref/{ref}'
1111
const releaseByTag = 'GET /repos/{owner}/{repo}/releases/tags/{tag}'
12+
const releaseByID = 'GET /repos/{owner}/{repo}/releases/{release_id}'
1213
const createRelease = 'POST /repos/{owner}/{repo}/releases'
1314
const updateRelease = 'PATCH /repos/{owner}/{repo}/releases/{release_id}'
1415
const repoAssets = 'GET /repos/{owner}/{repo}/releases/{release_id}/assets'
@@ -17,6 +18,7 @@ const uploadAssets =
1718
const deleteAssets = 'DELETE /repos/{owner}/{repo}/releases/assets/{asset_id}'
1819

1920
type ReleaseByTagResp = Endpoints[typeof releaseByTag]['response']
21+
type ReleaseByIDResp = Endpoints[typeof releaseByID]['response']
2022
type CreateReleaseResp = Endpoints[typeof createRelease]['response']
2123
type RepoAssetsResp = Endpoints[typeof repoAssets]['response']['data']
2224
type UploadAssetResp = Endpoints[typeof uploadAssets]['response']
@@ -33,17 +35,32 @@ async function get_release_by_tag(
3335
octokit: Octokit,
3436
overwrite: boolean,
3537
promote: boolean,
36-
target_commit: string
38+
target_commit: string,
39+
known_draft_id = 0
3740
): Promise<ReleaseByTagResp | CreateReleaseResp | UpdateReleaseResp> {
38-
let release: ReleaseByTagResp
41+
let release: ReleaseByTagResp | ReleaseByIDResp
3942
try {
40-
core.debug(`Getting release by tag ${tag}.`)
43+
core.debug(`Draft ID: ${known_draft_id}`)
4144

42-
// @ts-ignore
43-
release = await octokit.request(releaseByTag, {
44-
...repo(),
45-
tag: tag
46-
})
45+
if (draft === true && known_draft_id !== 0) {
46+
// We are working with a draft release and we already created it
47+
core.debug(
48+
`Getting release by id ${known_draft_id} because we're working with a draft release.`
49+
)
50+
release = await octokit.request(releaseByID, {
51+
...repo(),
52+
release_id: known_draft_id
53+
})
54+
55+
core.debug(`The release has the following ID: ${release.data.id}`)
56+
} else {
57+
core.debug(`Getting release by tag ${tag}.`)
58+
// @ts-ignore
59+
release = await octokit.request(releaseByTag, {
60+
...repo(),
61+
tag: tag
62+
})
63+
}
4764
} catch (error: any) {
4865
// If this returns 404, we need to create the release first.
4966
if (error.status !== 404) throw error
@@ -63,7 +80,7 @@ async function get_release_by_tag(
6380
}
6481
}
6582
// @ts-ignore
66-
return await octokit.request(createRelease, {
83+
const _release = await octokit.request(createRelease, {
6784
...repo(),
6885
tag_name: tag,
6986
draft: draft,
@@ -73,6 +90,8 @@ async function get_release_by_tag(
7390
body: body,
7491
target_commitish: target_commit
7592
})
93+
core.setOutput('draft_id', _release.data.id)
94+
return _release
7695
}
7796
return await update_release(
7897
promote,
@@ -236,6 +255,7 @@ async function run(): Promise<void> {
236255
const make_latest = core.getInput('make_latest') != 'false'
237256
const release_name = core.getInput('release_name')
238257
const target_commit = core.getInput('target_commit')
258+
const draft_release_id = core.getInput('draft_id')
239259
const check_duplicates =
240260
core.getInput('check_duplicates') != 'false' ? true : false
241261
const body = core
@@ -255,7 +275,8 @@ async function run(): Promise<void> {
255275
octokit,
256276
overwrite,
257277
promote,
258-
target_commit
278+
target_commit,
279+
Number(draft_release_id)
259280
)
260281

261282
if (file_glob) {

0 commit comments

Comments
 (0)