-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add PR expire reusable workflow (#41)
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
name: 'expire' | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
owner: | ||
description: 'The GitHub organization or repository owner.' | ||
type: 'string' | ||
required: true | ||
repo: | ||
description: 'The repository name.' | ||
type: 'string' | ||
required: true | ||
expiryHours: | ||
description: 'The number of hours to keep an AOD PR open since last updated.' | ||
type: 'number' | ||
default: 24 | ||
required: false | ||
|
||
permissions: | ||
pull-requests: 'write' | ||
|
||
jobs: | ||
expire: | ||
runs-on: 'ubuntu-latest' | ||
steps: | ||
- uses: 'actions/github-script@98814c53be79b1d30f795b907e553d8679345975' # ratchet:actions/github-script@v6 | ||
with: | ||
result-encoding: 'string' | ||
retries: 3 | ||
script: |- | ||
const cutoffMs = ${{ inputs.expiryHours }} * 60 * 60 * 1000 | ||
const now = new Date(); | ||
const pulls = await github.rest.pulls.list({ | ||
owner: '${{ inputs.owner }}', | ||
repo: '${{ inputs.repo }}', | ||
state: 'open', | ||
per_page: 100, | ||
}); | ||
if (!pulls) { | ||
return; | ||
} | ||
core.info(`Found ${pulls.data.length} pulls`); | ||
pulls.data.forEach(async (pull) => { | ||
const files = await github.rest.pulls.listFiles({ | ||
owner: '${{ inputs.owner }}', | ||
repo: '${{ inputs.repo }}', | ||
pull_number: pull['number'], | ||
}) | ||
// Check if the PR is an AOD request by checking the AOD file existence. | ||
if (files.data.find(f => f.filename === 'iam.yaml')) { | ||
// Check if the PR is within the cutoff threshold | ||
const updatedAt = new Date(Date.parse(pull['updated_at'])); | ||
if (now.getTime() - updatedAt.getTime() < cutoffMs) { | ||
core.info(`Skipping #${pull['number']} (${pull['title']}) - within threshold`); | ||
return; | ||
} | ||
core.info(`Closing #${pull['number']} (${pull['title']})`); | ||
await github.rest.pulls.update({ | ||
owner: '${{ inputs.owner }}', | ||
repo: '${{ inputs.repo }}', | ||
pull_number: pull['number'], | ||
state: 'closed', | ||
}); | ||
} else { | ||
core.info(`Skipping #${pull['number']} (${pull['title']}) - not an AOD request`); | ||
} | ||
}); |