Skip to content

Commit

Permalink
feat: add PR expire reusable workflow (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
sqin2019 authored Jun 15, 2023
1 parent c2c109c commit cee65a3
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/expire.yml
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`);
}
});

0 comments on commit cee65a3

Please sign in to comment.