Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix: pr related github scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh committed Mar 8, 2023
1 parent 6e1e88c commit 90d2e64
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 32 deletions.
43 changes: 33 additions & 10 deletions .github/workflows/create-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,37 @@ jobs:
- name: Create PRs
env:
BRANCH: web3-bot/sync
GITHUB_TOKEN: ${{ secrets.WEB3_BOT_GITHUB_TOKEN }}
uses: actions/github-script@v6
with:
retries: 12
github-token: ${{ secrets.WEB3_BOT_GITHUB_TOKEN }}
retries: 0
script: |
const request = async function(req, opts) {
try {
return await req(opts)
} catch(err) {
opts.request.retries = (opts.request.retries || 0) + 1
if (err.status === 403) {
if (err.response.headers['x-ratelimit-remaining'] === '0') {
const retryAfter = err.response.headers['x-ratelimit-reset'] - Math.floor(Date.now() / 1000) || 1
core.info(`Rate limit exceeded, retrying in ${retryAfter} seconds`)
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000))
return request(req, opts)
}
if (err.message.toLowerCase().includes('secondary rate limit')) {
const retryAfter = Math.pow(2, opts.request.retries)
core.info(`Secondary rate limit exceeded, retrying in ${retryAfter} seconds`)
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000))
return request(req, opts)
}
}
throw err
}
}
github.hook.wrap('request', request)
core.info(`Looking for repositories the user has direct access to`)
const items = await github.paginate(octokit.rest.repos.listForAuthenticatedUser, {
const items = await github.paginate(github.rest.repos.listForAuthenticatedUser, {
affiliation: 'collaborator'
})
core.info(`Filtering out the repositories without unmerged branches`)
Expand All @@ -30,24 +54,23 @@ jobs:
core.info(`Checking if a PR can be created for ${item.html_url}`)
let branch
try {
branch = await github.rest.repos.getBranch({
branch = (await github.rest.repos.getBranch({
owner: item.owner.login,
repo: item.name,
branch: process.env.BRANCH
})
}))?.data
} catch(error) {
if (error.status != 404) {
throw error
}
core.info(error)
}
if (branch != undefined) {
core.info(`The branch exists in ${item.html_url}`)
} else {
core.info(`The branch does not exist in ${item.html_url}`)
continue
}
const compare = await github.rest.repos.compareCommitsWithBasehead({
const {data: compare} = await github.rest.repos.compareCommitsWithBasehead({
owner: item.owner.login,
repo: item.name,
basehead: `${item.default_branch}...${branch.name}`
Expand All @@ -58,7 +81,7 @@ jobs:
core.info(`PR for ${item.html_url} cannot be created`)
continue
}
const pulls = await github.rest.repos.listPullRequestsAssociatedWithCommit({
const {data: pulls} = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: item.owner.login,
repo: item.name,
commit_sha: branch.commit.sha
Expand All @@ -67,7 +90,7 @@ jobs:
core.info(`The PR does not exist yet in ${item.html_url}`)
repos.push(item)
} else {
core.info('The PR already exists at ${pulls[0].html_url}')
core.info(`The PR already exists at ${pulls[0].html_url}`)
}
}
core.info(`Attempting to create the PRs`)
Expand All @@ -81,7 +104,7 @@ jobs:
head: process.env.BRANCH,
base: repo.default_branch
})
core.info(`Created PR at ${pr.html_url}`)
core.info(`${pr.html_url} created successfully`)
} catch(error) {
core.error(`Couldn't create a PR for ${repo.html_url}, got: ${error}`)
failed.push(repo)
Expand Down
61 changes: 45 additions & 16 deletions .github/workflows/delete-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,75 @@ jobs:
steps:
- name: Create PRs
env:
DRY_RUN: ${{ github.event.inputs.dry-run }}
DRY_RUN: ${{ github.event.inputs.dry-run || 'true' }}
PATH: ${{ github.event.inputs.path }}
GITHUB_TOKEN: ${{ secrets.WEB3BOT_GITHUB_TOKEN }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.WEB3_BOT_GITHUB_TOKEN }}
retries: 0
script: |
const request = async function(req, opts) {
try {
return await req(opts)
} catch(err) {
opts.request.retries = (opts.request.retries || 0) + 1
if (err.status === 403) {
if (err.response.headers['x-ratelimit-remaining'] === '0') {
const retryAfter = err.response.headers['x-ratelimit-reset'] - Math.floor(Date.now() / 1000) || 1
core.info(`Rate limit exceeded, retrying in ${retryAfter} seconds`)
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000))
return request(req, opts)
}
if (err.message.toLowerCase().includes('secondary rate limit')) {
const retryAfter = Math.pow(2, opts.request.retries)
core.info(`Secondary rate limit exceeded, retrying in ${retryAfter} seconds`)
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000))
return request(req, opts)
}
}
throw err
}
}
github.hook.wrap('request', request)
core.info(`Looking for repositories the user has direct access to`)
const items = await github.paginate(octokit.rest.repos.listForAuthenticatedUser, {
const items = await github.paginate(github.rest.repos.listForAuthenticatedUser, {
affiliation: 'collaborator'
})
core.info(`Filtering out the repositories without the file`)
const files = []
for (const item of items) {
core.info(`Checking if the repo at ${item.html_url} contains the file`)
core.info(`Checking if there is a file in ${item.html_url}`)
let file
try {
const file = await github.rest.repos.getContent({
file = (await github.rest.repos.getContent({
owner: item.owner.login,
repo: item.name,
path: process.env.PATH
})
core.debug(`The file exists in ${item.html_url}`)
}))?.data
} catch(error) {
if (error.status != 404) {
throw error
}
}
if (file) {
core.info(`${file.html_url} exists`)
files.push({
...file,
repo: item
})
} catch (err) {
if (err.status != 404) {
throw err
}
core.debug(`The file does not exist in ${item.html_url}`)
} else {
core.info(`The file does not exist in ${item.html_url}`)
}
}
core.info(`Attempting to delete the files`)
const failed = []
for (const file of files) {
if (process.env.DRY_RUN == 'true') {
core.info(`Would delete the file ${file.html_url}`)
core.info(`Would have deleted ${file.html_url}`)
continue
}
core.debug(`Deleting the file ${file.html_url}`)
core.debug(`Deleting ${file.html_url}`)
try {
await github.rest.repos.deleteFile({
owner: file.repo.owner.login,
Expand All @@ -66,9 +95,9 @@ jobs:
message: `ci: delete ${process.env.PATH}`,
sha: file.sha
})
core.debug(`Deleted the file ${file.html_url}`)
core.info(`${file.html_url} deleted successfully`)
} catch(error) {
core.error(`Couldn't delete the file ${file.html_url}, got: ${error}`)
core.error(`Couldn't delete ${file.html_url}, got: ${error}`)
failed.push(file)
}
}
Expand Down
35 changes: 29 additions & 6 deletions .github/workflows/merge-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,39 @@ jobs:
dispatch:
name: Merge PRs
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.WEB3BOT_GITHUB_TOKEN }}
steps:
- name: Merge PRs
env:
QUERY: is:pr author:web3-bot state:open status:success head:web3-bot/sync archived:false
QUERY: is:pr author:web3-bot state:open head:web3-bot/sync archived:false
uses: actions/github-script@v6
with:
retries: 12
github-token: ${{ secrets.WEB3_BOT_GITHUB_TOKEN }}
retries: 0
script: |
const request = async function(req, opts) {
try {
return await req(opts)
} catch(err) {
opts.request.retries = (opts.request.retries || 0) + 1
if (err.status === 403) {
if (err.response.headers['x-ratelimit-remaining'] === '0') {
const retryAfter = err.response.headers['x-ratelimit-reset'] - Math.floor(Date.now() / 1000) || 1
core.info(`Rate limit exceeded, retrying in ${retryAfter} seconds`)
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000))
return request(req, opts)
}
if (err.message.toLowerCase().includes('secondary rate limit')) {
const retryAfter = Math.pow(2, opts.request.retries)
core.info(`Secondary rate limit exceeded, retrying in ${retryAfter} seconds`)
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000))
return request(req, opts)
}
}
throw err
}
}
github.hook.wrap('request', request)
core.info(`Looking for PRs matching the query: ${process.env.QUERY}`)
const items = await github.paginate(github.rest.search.issuesAndPullRequests, {
q: process.env.QUERY
Expand All @@ -28,7 +51,7 @@ jobs:
for (const item of items) {
core.info(`Retrieving ${item.html_url}`)
const [_, owner, repo] = item.url.match(/repos\/(.+?)\/(.+?)\/issues/)
const pr = await github.rest.pulls.get({
const {data: pr} = await github.rest.pulls.get({
owner,
repo,
pull_number: item.number
Expand All @@ -51,7 +74,7 @@ jobs:
pull_number: pr.number,
merge_method: 'squash'
})
core.info(`Merged ${pr.html_url}`)
core.info(`${pr.html_url} merged successfully`)
} catch(error) {
core.error(`Couldn't merge ${pr.html_url}, got: ${error}`)
failed.push(pr)
Expand Down

0 comments on commit 90d2e64

Please sign in to comment.