Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI(Preview): add retries to find PR script #20

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions .github/workflows/preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,29 @@ jobs:
uses: actions/github-script@v7
with:
script: |
const response = await github.rest.search.issuesAndPullRequests({
q: 'repo:${{ github.repository }} is:pr sha:${{ github.event.workflow_run.head_sha }}',
per_page: 1,
})
const items = response.data.items
if (items.length < 1) {
console.error('No PRs found')
return
async function findPR() {
for (let retries = 3; retries > 0; retries--) {
try {
const response = await github.rest.search.issuesAndPullRequests({
q: 'repo:${{ github.repository }} is:pr sha:${{ github.event.workflow_run.head_sha }}',
per_page: 1,
});
const items = response.data.items;
if (items.length < 1) {
throw new Error('No PRs found');
}
const pullRequestNumber = items[0].number
console.info("Pull request number is", pullRequestNumber);
return pullRequestNumber
} catch (error) {
console.error(`Retrying... (${3 - retries + 1}/3)`);
if (retries === 1) throw error;
await new Promise(res => setTimeout(res, 10000));
}
}
}
const pullRequestNumber = items[0].number
console.info("Pull request number is", pullRequestNumber)
return pullRequestNumber
return findPR();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to wrap this in a function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe wrapping it in a function is not needed here, but the intention was to make it easier to manage, to debug, and easier to simulate different scenarios to make sure the retry logic works as expected. But maybe it’s overkill? Let me know if you prefer it a certain way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to just set retries on the action, it will handle the Retry-After header for us
https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api#handle-rate-limit-errors-appropriately

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Thanks for the fix!

github-token: ${{ secrets.GITHUB_TOKEN }}

# deploy
- name: Deploy to Cloudflare Pages
Expand Down