Skip to content

Commit

Permalink
Add test for guard
Browse files Browse the repository at this point in the history
  • Loading branch information
hanseartic committed Dec 18, 2024
1 parent 72a3e3b commit 37f11e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
24 changes: 23 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
version: [20]
version: [20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand All @@ -28,3 +28,25 @@ jobs:
- uses: ./
with:
token: ${{ github.token }}

test_guard:
name: Test Action guard
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- uses: ./
with:
token: ${{ github.token }}
id: run-guard
continue-on-error: true
- if: ${{ (success() || failure()) && steps.run-guard.outcome == 'failure' }}
run: |
echo "Guard failed as expected"
- if: ${{ (success() || failure()) && steps.run-guard.outcome != 'failure' }}
run: |
echo "Guard did not fail as expected"
exit 1
2 changes: 1 addition & 1 deletion index.guard.js

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions index.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ type Release = {
prerelease: boolean
tag_name: string
}

const [ owner, repo ] = process.env.GITHUB_ACTION_REPOSITORY?.split('/') ?? [ undefined, undefined ]
const actionRepo = process.env.GITHUB_ACTION_REPOSITORY != ''
? process.env.GITHUB_ACTION_REPOSITORY
: process.env.GITHUB_REPOSITORY
const [ owner, repo ] = actionRepo?.split('/') ?? [ undefined, undefined ]

async function getReleases(): Promise<Release[]> {
const octokit = new Octokit({auth: core.getInput('token') || process.env.GITHUB_TOKEN})
Expand All @@ -19,8 +21,8 @@ async function getReleases(): Promise<Release[]> {
return octokit.rest.repos.listReleases({ owner, repo })
.then(({ data }) => data
.map((release): Release => ({
html_url: release.html_url,
draft: release.draft,
html_url: release.html_url,
prerelease: release.prerelease,
tag_name: release.tag_name,
}))
Expand All @@ -31,15 +33,15 @@ core.setFailed('Must use a tagged release of this action! See summary for more d
getReleases()
.then((releases) => {
if (releases.length === 0) {
core.debug(`No releases for this action (${process.env.GITHUB_ACTION_REPOSITORY}) found on GitHub.`)
core.debug(`No releases for this action (${actionRepo}) found on GitHub.`)
return
}
const suggestedVersion = releases.find((release: Release) => !release.prerelease && !release.draft)?.tag_name ?? releases[0].tag_name
return core.summary
.addHeading('🏷️ Only tagged releases of this action can be used in workflows', 3)
.addRaw('Only tagged releases of this action can be used, e.g.\n', true)
.addRaw('```yaml', true)
.addRaw(`- uses: ${process.env.GITHUB_ACTION_REPOSITORY}@${suggestedVersion}`, true)
.addRaw(`- uses: ${actionRepo}@${suggestedVersion}`, true)
.addRaw('```\n', true)
.addRaw('----\nThe following releases are available:', true)
.addRaw(releases.map((release) => `* [${release.tag_name}](${release.html_url})`).join('\n'), true)
Expand Down

0 comments on commit 37f11e7

Please sign in to comment.