Check Pull Requests #87
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: Check Pull Requests | |
on: | |
schedule: | |
- cron: "* */1 * * *" # Run every hour | |
workflow_dispatch: # Allow manual triggering | |
permissions: | |
contents: read | |
pull-requests: write | |
statuses: write | |
jobs: | |
check_prs: | |
runs-on: ubuntu-latest | |
outputs: | |
prs: ${{ steps.filter_prs.outputs.prs }} | |
steps: | |
- name: List Open PRs | |
id: list_prs | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const { data: pullRequests } = await github.rest.pulls.list({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
state: 'open' | |
}); | |
core.exportVariable('PRS', JSON.stringify(pullRequests)); | |
- name: Filter PRs | |
id: filter_prs | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const prsRaw = process.env.PRS; | |
if (!prsRaw || prsRaw === '[]') { | |
console.log("No PRs to validate."); | |
core.setOutput("prs", []); | |
return; | |
} | |
const prs = JSON.parse(prsRaw); | |
const validate = []; | |
for (const pr of prs) { | |
const { data: statuses } = await github.rest.repos.listCommitStatusesForRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: pr.head.sha | |
}); | |
// const hasValidatedStatus = statuses.some(status => status.context === 'PR Validation / Valid'); | |
// const hasInProgressStatus = statuses.some(status => status.context === 'PR Validation / In Progress'); | |
// const hasFailed = statuses.some(status => status.context === 'PR Validation / Failed'); | |
// if (!hasValidatedStatus && !hasInProgressStatus && !hasFailed) { | |
// validate.push(pr); | |
// } | |
const hasStatus = statuses.some(status => status.context === 'PR Validation / Auto'); | |
if (!hasStatus) { | |
validate.push(pr); | |
} | |
} | |
core.setOutput("prs", validate); | |
trigger_validation: | |
needs: check_prs | |
if: ${{ fromJSON(needs.check_prs.outputs.prs)[0] }} | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
pr: ${{ fromJSON(needs.check_prs.outputs.prs) }} | |
steps: | |
# - name: Debug | |
# run: | | |
# echo "PR Number: ${{ matrix.pr.number }}" | |
- name: Set pending Status | |
uses: actions/github-script@v6 | |
env: | |
prSha: ${{ matrix.pr.head.sha }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
await github.rest.repos.createCommitStatus({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
sha: process.env.prSha, | |
state: 'pending', | |
context: 'PR Validation / Auto', | |
description: 'Validation is in progress...', | |
}); | |
- name: Prepare Validation | |
uses: actions/checkout@v4 | |
with: | |
path: ./ | |
repository: RestApia/RestApia.Extensions.Private | |
token: ${{ secrets.PRIVATE_REPO_TOKEN }} | |
- name: Checkout PR Code | |
uses: actions/checkout@v4 | |
with: | |
ref: refs/pull/${{ matrix.pr.number }}/merge | |
path: .local/pr | |
- name: Run Validation | |
run: | | |
chmod +x ./build.sh | |
./build.sh Git_PR_Handle --PrNumber ${{ matrix.pr.number }} | |
env: | |
PRIVATE_REPO_TOKEN: ${{ secrets.PRIVATE_REPO_TOKEN }} | |
- name: Upload Artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: extension-package-${{ matrix.pr.number }} # Unique name per PR | |
path: ${{ env.ARTIFACT_PATH }} | |
- name: Set success Status | |
if: success() | |
uses: actions/github-script@v6 | |
env: | |
prSha: ${{ matrix.pr.head.sha }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
await github.rest.repos.createCommitStatus({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
sha: process.env.prSha, | |
state: 'success', | |
context: 'PR Validation / Auto', | |
description: 'Validation passed successfully.', | |
}); | |
- name: Post failure Failure | |
if: failure() | |
uses: actions/github-script@v6 | |
env: | |
prSha: ${{ matrix.pr.head.sha }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
await github.rest.repos.createCommitStatus({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
sha: process.env.prSha, | |
state: 'failure', | |
context: 'PR Validation / Auto', | |
description: 'Validation failed.', | |
}); | |