This step succeeds when the tag exists and fails (Unhandled error: HttpError: Not Found) when the tag doesn't. Instead I would like the step to succeed for both 200 and 404 responses (but not 400, 401 etc) and to be able to tell the difference between 200 and 404 in the following steps. For example it would return an empty result when the response is 404, or it could return the response status itself (since I don't use the body of the getReleaseByTag() response at the moment). I found discussions on how to do this by invoking the underlying REST API (#386) but I'd rather find a way to do it using the more convenient getReleaseByTag() method. Any suggestion on how to do this?
Regards,
\nThomas
","upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"Hi @joshmgross,
\nThanks, you've pointed me in the right direction. With the code that you've posted, the step was still failing with an HttpError but then I noticed an extra message: Promise { <pending> }. I believe the issue is that getReleaseByTag() returns a Promise and so I modified your code like this and it seems to work now:
- name: Check if release already exists\n if: env.VERSION != ''\n uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1\n id: check-release\n with:\n script: |\n const { VERSION } = process.env\n return github.rest.repos.getReleaseByTag({\n owner: context.repo.owner,\n repo: context.repo.repo,\n tag: `v${VERSION}`,\n }).then(function(result) {\n core.info(`Release ${result.data.name} found`)\n return result.data.name\n }).catch(function(error) {\n if (error.status === 404) {\n core.info(`Release v${VERSION} not found`)\n return\n } else {\n throw error\n }\n })\n result-encoding: stringAgain I am not a node expert so maybe I am missing something but the workflow seems to work now so thanks again,
\nThomas
","upvoteCount":1,"url":"https://github.com/actions/github-script/discussions/468#discussioncomment-9194478"}}}-
|
Hi, I apologize in advance if this is a beginner's question but I am not a node expert so I am struggling a bit to do something that is probably pretty simple. I currently have the following step in my workflow: - name: Check if release already exists
id: check-release
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
script: |
const { VERSION } = process.env
github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: `v${VERSION}`,
})This step succeeds when the tag exists and fails ( Regards, Thomas |
Beta Was this translation helpful? Give feedback.
-
|
👋 Hey @thomasleplus, I think something like this would work for you: try {
const { VERSION } = process.env
github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: `v${VERSION}`,
})
core.info(`Release v${VERSION} found`)
} catch (error) {
if (error.status === 404) {
core.info(`Release v${VERSION} not found`)
} else {
throw error
}
}That's based on the error handling example in https://github.com/octokit/request-error.js |
Beta Was this translation helpful? Give feedback.
-
|
Hi @joshmgross, Thanks, you've pointed me in the right direction. With the code that you've posted, the step was still failing with an HttpError but then I noticed an extra message: - name: Check if release already exists
if: env.VERSION != ''
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
id: check-release
with:
script: |
const { VERSION } = process.env
return github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: `v${VERSION}`,
}).then(function(result) {
core.info(`Release ${result.data.name} found`)
return result.data.name
}).catch(function(error) {
if (error.status === 404) {
core.info(`Release v${VERSION} not found`)
return
} else {
throw error
}
})
result-encoding: stringAgain I am not a node expert so maybe I am missing something but the workflow seems to work now so thanks again, Thomas |
Beta Was this translation helpful? Give feedback.
Hi @joshmgross,
Thanks, you've pointed me in the right direction. With the code that you've posted, the step was still failing with an HttpError but then I noticed an extra message:
Promise { <pending> }. I believe the issue is that getReleaseByTag() returns a Promise and so I modified your code like this and it seems to work now: