Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Catching 404 response when calling github.rest.repos.getReleaseByTag()#468

Answeredbythomasleplus
thomasleplus asked this question inQ&A
Discussion options

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 existsid:check-releaseuses:actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea# v7.0.1script:|    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 (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,

Thomas

You must be logged in to vote

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:

-name:Check if release already existsif:env.VERSION != ''uses:actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea# v7.0.1id:check-releasewith:script:|      const { VERSION } = process.env      return github.rest.repos.getReleaseByTag({        owner: context.repo.owner,        repo: context.repo.repo,        tag: `v${VERSIO…

Replies: 2 comments

Comment options

👋 Hey@thomasleplus, I think something like this would work for you:

try{const{VERSION}=process.envgithub.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{throwerror}}

That's based on the error handling example inhttps://github.com/octokit/request-error.js

You must be logged in to vote
0 replies
Comment options

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:

-name:Check if release already existsif:env.VERSION != ''uses:actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea# v7.0.1id:check-releasewith: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:string

Again I am not a node expert so maybe I am missing something but the workflow seems to work now so thanks again,

Thomas

You must be logged in to vote
0 replies
Answer selected bythomasleplus
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@thomasleplus@joshmgross

[8]ページ先頭

©2009-2025 Movatter.jp