Skip to content

chore(deps): bump the development-dependencies group with 4 updates #31

chore(deps): bump the development-dependencies group with 4 updates

chore(deps): bump the development-dependencies group with 4 updates #31

# SPDX-License-Identifier: Apache-2.0
# Managed by github.com/igorjs-iac/repo-config
#
# Templated by repo-config/modules/hardened-repo/main.tf (variable
# manage_workflow_close_superseded). Direct edits in the consuming repo are
# overwritten on the next `terraform apply` against repo-config.
#
# When a PR is merged or a commit lands directly on the base branch (e.g.
# via Terraform), finds all other open PRs that touch at least one of the
# same files AND that GitHub considers no longer mergeable (real conflict
# against the new base), labels them "superseded", posts a comment, and
# closes them.
#
# Rationale: file-level overlap is a poor predictor of conflict. Two PRs
# that add resources to different sections of the same file merge cleanly.
# After a merge, GitHub recomputes the `mergeable_state` of every open PR;
# this workflow polls that state and only closes PRs that GitHub flagged as
# `dirty` (real conflict). PRs that touch the same files but merge cleanly
# are left alone.
#
# Opt-out: a PR carrying the label `keep-open` is not closed by this
# workflow, but only when a trusted human added the label. Bots, GitHub
# Apps, and the default GITHUB_TOKEN cannot bypass the auto-close gate by
# labeling their own PRs. The check inspects the issue timeline to find
# the most recent labeled / unlabeled event for `keep-open` and respects
# the opt-out only when the actor's type is `User` (not `Bot`). Adding
# a label already requires repo triage permission or above, so the
# actor-type check is sufficient: any User who could label the PR has
# been explicitly granted access by the repo owner.
name: Close Superseded PRs
on:
pull_request:
types: [closed]
push:
branches: [main]
permissions: {}
jobs:
close-superseded:
if: >-
(github.event_name == 'pull_request' && github.event.pull_request.merged == true) ||
github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: write
contents: read
steps:
- uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4
with:
egress-policy: block
allowed-endpoints: >
api.github.com:443
github.com:443
objects.githubusercontent.com:443
- name: Close PRs superseded by this merge
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const isPR = context.eventName === 'pull_request';
const isPush = context.eventName === 'push';
let mergedSet;
let baseBranch;
let mergedRef; // human-readable source for comments
if (isPR) {
const mergedPR = context.payload.pull_request;
baseBranch = mergedPR.base.ref;
mergedRef = '#' + mergedPR.number + ' ("' + mergedPR.title + '")';
const { data: mergedFiles } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: mergedPR.number,
per_page: 100,
});
mergedSet = new Set(mergedFiles.map(f => f.filename));
} else {
// Direct push (e.g. Terraform commit). Use the compare API to
// determine which files changed between the previous and new HEAD.
baseBranch = context.payload.ref.replace('refs/heads/', '');
mergedRef = 'commit ' + context.sha.slice(0, 7);
const before = context.payload.before;
if (!before || before === '0000000000000000000000000000000000000000') {
core.info('Push has no prior base commit; nothing to supersede.');
return;
}
const { data: compare } = await github.rest.repos.compareCommits({
owner: context.repo.owner,
repo: context.repo.repo,
base: before,
head: context.sha,
});
mergedSet = new Set((compare.files || []).map(f => f.filename));
}
if (mergedSet.size === 0) {
core.info('No changed files detected; nothing to supersede.');
return;
}
// All open PRs targeting the same base branch.
const { data: openPRs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
base: baseBranch,
per_page: 100,
});
for (const pr of openPRs) {
// Opt-out: PRs carrying the `keep-open` label are not closed
// by this workflow, but the label must have been added by a
// trusted human. Bots and GitHub Apps cannot bypass the
// auto-close gate by labeling their own PRs.
//
// Inspect the issue timeline to find the most recent
// labeled / unlabeled event for `keep-open`. If the last
// labeling actor has type `User`, respect the opt-out (since
// adding a label already requires triage permission, which
// is granted explicitly per actor). Otherwise log a warning
// and proceed with the normal conflict check.
if (pr.labels.some(l => l.name === 'keep-open')) {
let trusted = false;
let actor = null;
try {
const events = await github.paginate(
github.rest.issues.listEventsForTimeline,
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
per_page: 100,
}
);
let lastLabeled = null;
for (const e of events) {
if (!e.label || e.label.name !== 'keep-open') continue;
if (e.event === 'labeled') lastLabeled = e;
if (e.event === 'unlabeled') lastLabeled = null;
}
if (lastLabeled && lastLabeled.actor) {
actor = lastLabeled.actor;
if (actor.type === 'User') {
trusted = true;
} else {
core.warning(
'PR #' + pr.number + ' keep-open label was added by ' +
actor.type + ' ' + actor.login + '; ignoring opt-out.'
);
}
} else {
core.warning(
'PR #' + pr.number + ' has keep-open label but no ' +
'labeled event was found in the timeline; ignoring opt-out.'
);
}
} catch (err) {
core.warning(
'Could not verify keep-open label author on PR #' +
pr.number + ': ' + err.message + '; ignoring opt-out.'
);
}
if (trusted) {
core.info(
'PR #' + pr.number + ' has trusted keep-open label from ' +
actor.login + '; skipping.'
);
continue;
}
}
let prFiles;
try {
const { data } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
per_page: 100,
});
prFiles = data;
} catch (err) {
core.warning('Could not list files for PR #' + pr.number + ': ' + err.message);
continue;
}
const overlap = prFiles.filter(f => mergedSet.has(f.filename));
if (overlap.length === 0) continue;
// File overlap is necessary but not sufficient. Poll the PR's
// mergeable state until GitHub finishes recomputing it (the
// base branch just moved). Only close when GitHub reports a
// real conflict (`mergeable === false` AND
// `mergeable_state === 'dirty'`). Skip when the PR still
// merges cleanly, when GitHub cannot determine the state
// within the poll window, or when any other state surfaces.
let mergeable = null;
let state = null;
for (let attempt = 0; attempt < 8; attempt++) {
const { data: fresh } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
});
if (fresh.mergeable !== null && fresh.mergeable_state !== 'unknown') {
mergeable = fresh.mergeable;
state = fresh.mergeable_state;
break;
}
await new Promise(r => setTimeout(r, 3000));
}
if (mergeable === null) {
core.warning('PR #' + pr.number + ' mergeable state still unknown after poll; leaving open.');
continue;
}
if (mergeable === true || state !== 'dirty') {
core.info('PR #' + pr.number + ' overlaps files but is still mergeable (state=' + state + '); leaving open.');
continue;
}
core.info('PR #' + pr.number + ' has real conflict against ' + baseBranch + ' (state=dirty); closing.');
const listed = overlap.slice(0, 10).map(f => '- `' + f.filename + '`').join('\n');
const extra = overlap.length > 10 ? '\n- and ' + (overlap.length - 10) + ' more' : '';
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: [
'## Superseded by ' + mergedRef,
'',
mergedRef + ' landed on `' + baseBranch + '` and modifies the same files as this PR:',
'',
listed + extra,
'',
'This PR is being closed as superseded. If the work is still relevant, please rebase on `' + baseBranch + '` and reopen.',
].join('\n'),
});
try {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: ['superseded'],
});
} catch { /* label may not exist; non-fatal */ }
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
state: 'closed',
});
try {
await github.rest.issues.lock({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
lock_reason: 'resolved',
});
} catch { /* non-fatal; locking may fail on repos without the permission */ }
core.info('Closed PR #' + pr.number + ' as superseded by ' + mergedRef);
}