Auto-Test Dependabot PRs #1127
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Auto-Test Dependabot PRs | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: "0 14 * * *" | |
jobs: | |
auto-test: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Dispatch | |
uses: actions/github-script@v7 | |
with: | |
github-token: "${{ secrets.PERSONAL_TOKEN }}" | |
script: | | |
const DEPENDABOT_NAME = "dependabot[bot]"; | |
const pullRequests = await github.rest.pulls.list({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
state: "open", | |
sort: "created", | |
direction: "asc", | |
}); | |
let dependabotPRs = []; | |
for (const pr of pullRequests.data){ | |
if(pr.user.login === DEPENDABOT_NAME){ | |
const commits = await github.rest.pulls.listCommits({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: pr.number, | |
}); | |
if(commits.data.some(x => x.author.login !== DEPENDABOT_NAME)){ | |
core.info(`Non-${DEPENDABOT_NAME} commits found in PR ${pr.url}. Skipping.`); | |
continue; | |
} | |
const comments = await github.rest.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: pr.number, | |
per_page: 100, | |
}); | |
if(comments.data.some(x => x.body === `@dependabot recreate sha=${pr.head.sha}`)){ | |
core.info(`${pr.url} already has the test comment for sha=${pr.head.sha} issued. Skipping.`); | |
continue; | |
} | |
dependabotPRs.push({ | |
pr: pr, | |
comments: comments.data.length, | |
}); | |
} | |
} | |
if(dependabotPRs.length === 0){ | |
core.info("No dependabot PRs found. Stopping."); | |
return; | |
} | |
dependabotPRs.sort((a, b) => a.comments - b.comments); | |
let dependabotPR = dependabotPRs[0]; | |
core.info(`Selected PR with fewest comments: ${dependabotPR.pr.url}`); | |
// Truncate the body to keep the size of the payload under the max | |
if (dependabotPR.pr.body) { | |
dependabotPR.pr.body = dependabotPR.pr.body.slice(0, 1000) | |
} | |
const clientPayload = { | |
pull_request: dependabotPR.pr, | |
slash_command: { | |
sha: dependabotPR.pr.head.sha, | |
} | |
}; | |
core.info("Dispatching ok-to-test-command."); | |
await github.rest.repos.createDispatchEvent({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
event_type: "ok-to-test-command", | |
client_payload: clientPayload, | |
}); | |
const commentBody = `@dependabot recreate sha=${dependabotPR.pr.head.sha}`; | |
core.info(`Issuing comment: "${commentBody}"`); | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: dependabotPR.pr.number, | |
body: commentBody, | |
}); |