Skip to content

Commit 011f795

Browse files
author
Akos Kitta
committed
GH-24: Support for custom owner/repo config.
To be able to manage foreign repositories. The `owner` and `repo` is extracted from the `repo_name`. Otherwise, it uses the current repository. Closes #24 Signed-off-by: Akos Kitta <[email protected]>
1 parent 4f71b6d commit 011f795

File tree

4 files changed

+86
-9
lines changed

4 files changed

+86
-9
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Optional Arguments
1919
- `overwrite`: If an asset with the same name already exists, overwrite it (Default: `false`).
2020
- `prerelease`: Mark the release as a pre-release (Default: `false`).
2121
- `release_name`: Explicitly set a release name. (Defaults: implicitly same as `tag` via GitHub API).
22-
- `body`: Content of the release text (Defaut: `""`).
22+
- `body`: Content of the release text (Default: `""`).
23+
- `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).
2324

2425
## Output variables
2526

@@ -101,12 +102,14 @@ jobs:
101102
```
102103
103104
Example with `file_glob`:
105+
104106
```yaml
105107
name: Publish
106108
on:
107109
push:
108110
tags:
109111
- '*'
112+
110113
jobs:
111114
build:
112115
name: Publish binaries
@@ -125,6 +128,39 @@ jobs:
125128
file_glob: true
126129
```
127130

131+
Example for creating a release in a foreign repository using `repo_name`:
132+
133+
```yaml
134+
name: Publish
135+
136+
on:
137+
push:
138+
tags:
139+
- '*'
140+
141+
jobs:
142+
build:
143+
name: Publish binaries
144+
runs-on: ubuntu-latest
145+
146+
steps:
147+
- uses: actions/checkout@v2
148+
- name: Build
149+
run: cargo build --release
150+
- name: Upload binaries to release
151+
uses: svenstaro/upload-release-action@v2
152+
with:
153+
repo_name: owner/repository-name
154+
# A personal access token for the GitHub repository in which the release will be created and edited.
155+
# It is recommended to create the access token with the following scopes: `repo, user, admin:repo_hook`.
156+
repo_token: ${{ secrets.YOUR_PERSONAL_ACCESS_TOKEN }}
157+
file: target/release/mything
158+
asset_name: mything
159+
tag: ${{ github.ref }}
160+
overwrite: true
161+
body: "This is my release text"
162+
```
163+
128164
## Releasing
129165
130166
To release this Action:

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ inputs:
2626
description: 'Explicitly set a release name. Defaults to empty which will cause the release to take the tag as name on GitHub.'
2727
body:
2828
description: 'Content of the release text. Empty by default.'
29+
repo_name:
30+
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'
2931
outputs:
3032
browser_download_url:
3133
description: 'The publicly available URL of the asset.'

dist/index.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,13 +2205,13 @@ function get_release_by_tag(tag, prerelease, release_name, body, octokit) {
22052205
return __awaiter(this, void 0, void 0, function* () {
22062206
try {
22072207
core.debug(`Getting release by tag ${tag}.`);
2208-
return yield octokit.repos.getReleaseByTag(Object.assign(Object.assign({}, github.context.repo), { tag: tag }));
2208+
return yield octokit.repos.getReleaseByTag(Object.assign(Object.assign({}, repo()), { tag: tag }));
22092209
}
22102210
catch (error) {
22112211
// If this returns 404, we need to create the release first.
22122212
if (error.status === 404) {
22132213
core.debug(`Release for tag ${tag} doesn't exist yet so we'll create it now.`);
2214-
return yield octokit.repos.createRelease(Object.assign(Object.assign({}, github.context.repo), { tag_name: tag, prerelease: prerelease, name: release_name, body: body }));
2214+
return yield octokit.repos.createRelease(Object.assign(Object.assign({}, repo()), { tag_name: tag, prerelease: prerelease, name: release_name, body: body }));
22152215
}
22162216
else {
22172217
throw error;
@@ -2229,12 +2229,12 @@ function upload_to_release(release, file, asset_name, tag, overwrite, octokit) {
22292229
const file_size = stat.size;
22302230
const file_bytes = fs.readFileSync(file);
22312231
// Check for duplicates.
2232-
const assets = yield octokit.repos.listReleaseAssets(Object.assign(Object.assign({}, github.context.repo), { release_id: release.data.id }));
2232+
const assets = yield octokit.repos.listReleaseAssets(Object.assign(Object.assign({}, repo()), { release_id: release.data.id }));
22332233
const duplicate_asset = assets.data.find(a => a.name === asset_name);
22342234
if (duplicate_asset !== undefined) {
22352235
if (overwrite) {
22362236
core.debug(`An asset called ${asset_name} already exists in release ${tag} so we'll overwrite it.`);
2237-
yield octokit.repos.deleteReleaseAsset(Object.assign(Object.assign({}, github.context.repo), { asset_id: duplicate_asset.id }));
2237+
yield octokit.repos.deleteReleaseAsset(Object.assign(Object.assign({}, repo()), { asset_id: duplicate_asset.id }));
22382238
}
22392239
else {
22402240
core.setFailed(`An asset called ${asset_name} already exists.`);
@@ -2257,6 +2257,25 @@ function upload_to_release(release, file, asset_name, tag, overwrite, octokit) {
22572257
return uploaded_asset.data.browser_download_url;
22582258
});
22592259
}
2260+
function repo() {
2261+
const repo_name = core.getInput('repo_name');
2262+
// If we're not targeting a foreign repository, we can just return immediately and don't have to do extra work.
2263+
if (!repo_name) {
2264+
return github.context.repo;
2265+
}
2266+
const owner = repo_name.substr(0, repo_name.indexOf('/'));
2267+
if (!owner) {
2268+
throw new Error(`Could not extract 'owner' from 'repo_name': ${repo_name}.`);
2269+
}
2270+
const repo = repo_name.substr(repo_name.indexOf('/') + 1);
2271+
if (!repo) {
2272+
throw new Error(`Could not extract 'repo' from 'repo_name': ${repo_name}.`);
2273+
}
2274+
return {
2275+
owner,
2276+
repo
2277+
};
2278+
}
22602279
function run() {
22612280
return __awaiter(this, void 0, void 0, function* () {
22622281
try {

src/main.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function get_release_by_tag(
2121
try {
2222
core.debug(`Getting release by tag ${tag}.`)
2323
return await octokit.repos.getReleaseByTag({
24-
...github.context.repo,
24+
...repo(),
2525
tag: tag
2626
})
2727
} catch (error) {
@@ -31,7 +31,7 @@ async function get_release_by_tag(
3131
`Release for tag ${tag} doesn't exist yet so we'll create it now.`
3232
)
3333
return await octokit.repos.createRelease({
34-
...github.context.repo,
34+
...repo(),
3535
tag_name: tag,
3636
prerelease: prerelease,
3737
name: release_name,
@@ -61,7 +61,7 @@ async function upload_to_release(
6161

6262
// Check for duplicates.
6363
const assets: RepoAssetsResp = await octokit.repos.listReleaseAssets({
64-
...github.context.repo,
64+
...repo(),
6565
release_id: release.data.id
6666
})
6767
const duplicate_asset = assets.data.find(a => a.name === asset_name)
@@ -71,7 +71,7 @@ async function upload_to_release(
7171
`An asset called ${asset_name} already exists in release ${tag} so we'll overwrite it.`
7272
)
7373
await octokit.repos.deleteReleaseAsset({
74-
...github.context.repo,
74+
...repo(),
7575
asset_id: duplicate_asset.id
7676
})
7777
} else {
@@ -99,6 +99,26 @@ async function upload_to_release(
9999
return uploaded_asset.data.browser_download_url
100100
}
101101

102+
function repo(): {owner: string; repo: string} {
103+
const repo_name = core.getInput('repo_name')
104+
// If we're not targeting a foreign repository, we can just return immediately and don't have to do extra work.
105+
if (!repo_name) {
106+
return github.context.repo
107+
}
108+
const owner = repo_name.substr(0, repo_name.indexOf('/'))
109+
if (!owner) {
110+
throw new Error(`Could not extract 'owner' from 'repo_name': ${repo_name}.`)
111+
}
112+
const repo = repo_name.substr(repo_name.indexOf('/') + 1)
113+
if (!repo) {
114+
throw new Error(`Could not extract 'repo' from 'repo_name': ${repo_name}.`)
115+
}
116+
return {
117+
owner,
118+
repo
119+
}
120+
}
121+
102122
async function run(): Promise<void> {
103123
try {
104124
// Get the inputs from the workflow file: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs

0 commit comments

Comments
 (0)