From 210500d479898d56b8abff48ced1e7771a918390 Mon Sep 17 00:00:00 2001 From: regevbr Date: Mon, 22 May 2023 21:56:34 +0300 Subject: [PATCH] fix #42 - overwrite body and name as well --- .github/workflows/ci.yml | 135 ++++++++++++++++++++++++--------------- README.md | 4 ++ action.yml | 2 + dist/index.js | 10 ++- src/main.ts | 12 +++- 5 files changed, 107 insertions(+), 56 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9db28b05..73cb211c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 }}", + }) diff --git a/README.md b/README.md index f31a4ea5..06b6c53f 100644 --- a/README.md +++ b/README.md @@ -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). @@ -59,6 +60,7 @@ jobs: asset_name: mything tag: ${{ github.ref }} overwrite: true + promote: true body: "This is my release text" ``` @@ -126,6 +128,7 @@ jobs: file: target/release/my* tag: ${{ github.ref }} overwrite: true + promote: true file_glob: true ``` @@ -159,6 +162,7 @@ jobs: asset_name: mything tag: ${{ github.ref }} overwrite: true + promote: true body: "This is my release text" ``` diff --git a/action.yml b/action.yml index a93b2365..7fd9e886 100644 --- a/action.yml +++ b/action.yml @@ -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: diff --git a/dist/index.js b/dist/index.js index 245c5ff5..c4fed6a5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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 { @@ -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.`); @@ -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'); @@ -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) { diff --git a/src/main.ts b/src/main.ts index 5d3f5fc2..2330b23e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -32,7 +32,8 @@ async function get_release_by_tag( release_name: string, body: string, octokit: Octokit, - overwrite: boolean + overwrite: boolean, + promote: boolean ): Promise { let release: ReleaseByTagResp try { @@ -60,6 +61,11 @@ async function get_release_by_tag( } } let updateObject: Partial | 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( @@ -184,6 +190,7 @@ async function run(): Promise { 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') @@ -201,7 +208,8 @@ async function run(): Promise { release_name, body, octokit, - overwrite + overwrite, + promote ) if (file_glob) {