Skip to content

Check Pull Requests #85

Check Pull Requests

Check Pull Requests #85

Workflow file for this run

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
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...',
});
env:
prSha: ${{ matrix.pr.head.sha }}
- 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:
pr: ${{ matrix.pr }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: process.env.pr.head.sha,
state: 'success',
context: 'PR Validation / Auto',
description: 'Validation passed successfully.',
});
- name: Post failure Failure
if: failure()
uses: actions/github-script@v6
env:
pr: ${{ matrix.pr }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: process.env.pr.head.sha,
state: 'failure',
context: 'PR Validation / Auto',
description: 'Validation failed.',
});