Skip to content

Commit

Permalink
fix #42 - overwrite body and name as well
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed May 23, 2023
1 parent f0dfba8 commit 210500d
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 56 deletions.
135 changes: 83 additions & 52 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v3
- run: |
npm install
npm run all
- uses: actions/checkout@v3
- run: |
npm install
npm run all
test: # make sure the action works on a clean machine without building
name: E2E test
Expand All @@ -21,51 +21,82 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v3
- name: Make test pre-release
uses: ./
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: README.md
asset_name: TEST.md
tag: ci-test-${{ matrix.os }}-${{ github.run_id }}
overwrite: true
prerelease: true
make_latest: true
body: "rofl lol test%0Aianal %25 fubar"
- name: Check that the uploaded asset is readable
uses: actions/github-script@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs')
const assert = require('assert').strict;
const expected = fs.readFileSync("README.md", "utf-8")
const release = await github.repos.getReleaseByTag({
...context.repo,
tag: "ci-test-${{ matrix.os }}-${{ github.run_id }}",
})
assert.deepStrictEqual(release.data.prerelease, true)
assert.deepStrictEqual(release.data.body, "rofl lol test\nianal % fubar")
assert.deepStrictEqual(release.data.assets[0].name, "TEST.md")
const actual = await github.request(release.data.assets[0].browser_download_url)
assert.deepStrictEqual(expected, actual.data)
- name: Clean up
if: ${{ always() }}
uses: actions/github-script@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const release = await github.repos.getReleaseByTag({
...context.repo,
tag: "ci-test-${{ matrix.os }}-${{ github.run_id }}",
})
await github.repos.deleteRelease({
...context.repo,
release_id: release.data.id,
})
await github.git.deleteRef({
...context.repo,
ref: "tags/ci-test-${{ matrix.os }}-${{ github.run_id }}",
})
- uses: actions/checkout@v3
- name: Make test pre-release
uses: ./
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: README.md
asset_name: TEST.md
tag: ci-test-${{ matrix.os }}-${{ github.run_id }}
overwrite: true
prerelease: true
make_latest: true
body: "rofl lol test%0Aianal %25 fubar"
- name: Check that the uploaded asset is readable
uses: actions/github-script@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs')
const assert = require('assert').strict;
const expected = fs.readFileSync("README.md", "utf-8")
const release = await github.repos.getReleaseByTag({
...context.repo,
tag: "ci-test-${{ matrix.os }}-${{ github.run_id }}",
})
assert.deepStrictEqual(release.data.prerelease, true)
assert.deepStrictEqual(release.data.body, "rofl lol test\nianal % fubar")
assert.deepStrictEqual(release.data.assets[0].name, "TEST.md")
const actual = await github.request(release.data.assets[0].browser_download_url)
assert.deepStrictEqual(expected, actual.data)
- name: Make test promote
uses: ./
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: README.md
asset_name: TEST.md
tag: ci-test-${{ matrix.os }}-${{ github.run_id }}
overwrite: true
promote: true
prerelease: false
make_latest: true
body: "new body"
- name: Check that the release is promoted
uses: actions/github-script@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
retries: 15
script: |
const fs = require('fs')
const assert = require('assert').strict;
const expected = fs.readFileSync("README.md", "utf-8")
const release = await github.repos.getReleaseByTag({
...context.repo,
tag: "ci-test-${{ matrix.os }}-${{ github.run_id }}",
})
assert.deepStrictEqual(release.data.prerelease, false)
assert.deepStrictEqual(release.data.body, "new body")
assert.deepStrictEqual(release.data.assets[0].name, "TEST.md")
const actual = await github.request(release.data.assets[0].browser_download_url)
assert.deepStrictEqual(expected, actual.data)
- name: Clean up
if: ${{ always() }}
uses: actions/github-script@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const release = await github.repos.getReleaseByTag({
...context.repo,
tag: "ci-test-${{ matrix.os }}-${{ github.run_id }}",
})
await github.repos.deleteRelease({
...context.repo,
release_id: release.data.id,
})
await github.git.deleteRef({
...context.repo,
ref: "tags/ci-test-${{ matrix.os }}-${{ github.run_id }}",
})
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Optional Arguments
This is not used if `file_glob` is set to `true`.
- `file_glob`: If set to true, the `file` argument can be a glob pattern (`asset_name` is ignored in this case) (Default: `false`)
- `overwrite`: If an asset with the same name already exists, overwrite it (Default: `false`).
- `promote`: If a prerelease already exists, promote it to a release (Default: `false`).
- `prerelease`: Mark the release as a pre-release (Default: `false`).
- `make_latest`: Mark the release as the latest release for the repository (Default: `true`).
- `release_name`: Explicitly set a release name. (Defaults: implicitly same as `tag` via GitHub API).
Expand Down Expand Up @@ -59,6 +60,7 @@ jobs:
asset_name: mything
tag: ${{ github.ref }}
overwrite: true
promote: true
body: "This is my release text"
```
Expand Down Expand Up @@ -126,6 +128,7 @@ jobs:
file: target/release/my*
tag: ${{ github.ref }}
overwrite: true
promote: true
file_glob: true
```

Expand Down Expand Up @@ -159,6 +162,7 @@ jobs:
asset_name: mything
tag: ${{ github.ref }}
overwrite: true
promote: true
body: "This is my release text"
```
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ inputs:
description: 'Name of the asset. When not provided will use the file name. Unused if file_glob is set to "true".'
overwrite:
description: 'Overwrite the release in case it already exists.'
promote:
description: 'Promote a prerelease to release. Defaults to "false".'
file_glob:
description: 'If true the file can be a glob pattern, asset_name is ignored if this is true.'
prerelease:
Expand Down
10 changes: 8 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const updateRelease = 'PATCH /repos/{owner}/{repo}/releases/{release_id}';
const repoAssets = 'GET /repos/{owner}/{repo}/releases/{release_id}/assets';
const uploadAssets = 'POST {origin}/repos/{owner}/{repo}/releases/{release_id}/assets{?name,label}';
const deleteAssets = 'DELETE /repos/{owner}/{repo}/releases/assets/{asset_id}';
function get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit, overwrite) {
function get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit, overwrite, promote) {
return __awaiter(this, void 0, void 0, function* () {
let release;
try {
Expand All @@ -69,6 +69,11 @@ function get_release_by_tag(tag, prerelease, make_latest, release_name, body, oc
}
}
let updateObject;
if (promote && release.data.prerelease) {
core.debug(`The ${tag} is a prerelease, promoting it to a release.`);
updateObject = updateObject || {};
updateObject.prerelease = false;
}
if (overwrite) {
if (release.data.name !== release_name) {
core.debug(`The ${tag} release already exists with a different name ${release.data.name} so we'll overwrite it.`);
Expand Down Expand Up @@ -158,6 +163,7 @@ function run() {
.replace('refs/heads/', '');
const file_glob = core.getInput('file_glob') == 'true' ? true : false;
const overwrite = core.getInput('overwrite') == 'true' ? true : false;
const promote = core.getInput('promote') == 'true' ? true : false;
const prerelease = core.getInput('prerelease') == 'true' ? true : false;
const make_latest = core.getInput('make_latest') != 'false' ? true : false;
const release_name = core.getInput('release_name');
Expand All @@ -167,7 +173,7 @@ function run() {
.replace(/%0D/gi, '\r')
.replace(/%25/g, '%');
const octokit = github.getOctokit(token);
const release = yield get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit, overwrite);
const release = yield get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit, overwrite, promote);
if (file_glob) {
const files = glob.sync(file);
if (files.length > 0) {
Expand Down
12 changes: 10 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ async function get_release_by_tag(
release_name: string,
body: string,
octokit: Octokit,
overwrite: boolean
overwrite: boolean,
promote: boolean
): Promise<ReleaseByTagResp | CreateReleaseResp | UpdateReleaseResp> {
let release: ReleaseByTagResp
try {
Expand Down Expand Up @@ -60,6 +61,11 @@ async function get_release_by_tag(
}
}
let updateObject: Partial<UpdateReleaseParams> | undefined
if (promote && release.data.prerelease) {
core.debug(`The ${tag} is a prerelease, promoting it to a release.`)
updateObject = updateObject || {}
updateObject.prerelease = false
}
if (overwrite) {
if (release.data.name !== release_name) {
core.debug(
Expand Down Expand Up @@ -184,6 +190,7 @@ async function run(): Promise<void> {

const file_glob = core.getInput('file_glob') == 'true' ? true : false
const overwrite = core.getInput('overwrite') == 'true' ? true : false
const promote = core.getInput('promote') == 'true' ? true : false
const prerelease = core.getInput('prerelease') == 'true' ? true : false
const make_latest = core.getInput('make_latest') != 'false' ? true : false
const release_name = core.getInput('release_name')
Expand All @@ -201,7 +208,8 @@ async function run(): Promise<void> {
release_name,
body,
octokit,
overwrite
overwrite,
promote
)

if (file_glob) {
Expand Down

0 comments on commit 210500d

Please sign in to comment.