Skip to content

Commit dcebc8d

Browse files
Apply PR #11310: ci: fixed stale pr workflow
2 parents 533db63 + e898f93 commit dcebc8d

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

.github/workflows/close-stale-prs.yml

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,98 @@ jobs:
2828
const cutoff = new Date(Date.now() - DAYS_INACTIVE * 24 * 60 * 60 * 1000)
2929
const { owner, repo } = context.repo
3030
const dryRun = context.payload.inputs?.dryRun === "true"
31-
const stalePrs = []
3231
3332
core.info(`Dry run mode: ${dryRun}`)
33+
core.info(`Cutoff date: ${cutoff.toISOString()}`)
3434
35-
const prs = await github.paginate(github.rest.pulls.list, {
36-
owner,
37-
repo,
38-
state: "open",
39-
per_page: 100,
40-
sort: "updated",
41-
direction: "asc",
42-
})
43-
44-
for (const pr of prs) {
45-
const lastUpdated = new Date(pr.updated_at)
46-
if (lastUpdated > cutoff) {
47-
core.info(`PR ${pr.number} is fresh`)
48-
continue
35+
const query = `
36+
query($owner: String!, $repo: String!, $cursor: String) {
37+
repository(owner: $owner, name: $repo) {
38+
pullRequests(first: 100, states: OPEN, after: $cursor) {
39+
pageInfo {
40+
hasNextPage
41+
endCursor
42+
}
43+
nodes {
44+
number
45+
title
46+
author {
47+
login
48+
}
49+
createdAt
50+
commits(last: 1) {
51+
nodes {
52+
commit {
53+
committedDate
54+
}
55+
}
56+
}
57+
comments(last: 1) {
58+
nodes {
59+
createdAt
60+
}
61+
}
62+
reviews(last: 1) {
63+
nodes {
64+
createdAt
65+
}
66+
}
67+
}
68+
}
69+
}
4970
}
71+
`
72+
73+
const allPrs = []
74+
let cursor = null
75+
let hasNextPage = true
76+
77+
while (hasNextPage) {
78+
const result = await github.graphql(query, {
79+
owner,
80+
repo,
81+
cursor,
82+
})
5083
51-
stalePrs.push(pr)
84+
allPrs.push(...result.repository.pullRequests.nodes)
85+
hasNextPage = result.repository.pullRequests.pageInfo.hasNextPage
86+
cursor = result.repository.pullRequests.pageInfo.endCursor
5287
}
5388
89+
core.info(`Found ${allPrs.length} open pull requests`)
90+
91+
const stalePrs = allPrs.filter((pr) => {
92+
const dates = [
93+
new Date(pr.createdAt),
94+
pr.commits.nodes[0] ? new Date(pr.commits.nodes[0].commit.committedDate) : null,
95+
pr.comments.nodes[0] ? new Date(pr.comments.nodes[0].createdAt) : null,
96+
pr.reviews.nodes[0] ? new Date(pr.reviews.nodes[0].createdAt) : null,
97+
].filter((d) => d !== null)
98+
99+
const lastActivity = dates.sort((a, b) => b.getTime() - a.getTime())[0]
100+
101+
if (!lastActivity || lastActivity > cutoff) {
102+
core.info(`PR #${pr.number} is fresh (last activity: ${lastActivity?.toISOString() || "unknown"})`)
103+
return false
104+
}
105+
106+
core.info(`PR #${pr.number} is STALE (last activity: ${lastActivity.toISOString()})`)
107+
return true
108+
})
109+
54110
if (!stalePrs.length) {
55111
core.info("No stale pull requests found.")
56112
return
57113
}
58114
115+
core.info(`Found ${stalePrs.length} stale pull requests`)
116+
59117
for (const pr of stalePrs) {
60118
const issue_number = pr.number
61119
const closeComment = `Closing this pull request because it has had no updates for more than ${DAYS_INACTIVE} days. If you plan to continue working on it, feel free to reopen or open a new PR.`
62120
63121
if (dryRun) {
64-
core.info(`[dry-run] Would close PR #${issue_number} from ${pr.user.login}`)
122+
core.info(`[dry-run] Would close PR #${issue_number} from ${pr.author.login}: ${pr.title}`)
65123
continue
66124
}
67125
@@ -79,5 +137,5 @@ jobs:
79137
state: "closed",
80138
})
81139
82-
core.info(`Closed PR #${issue_number} from ${pr.user.login}`)
140+
core.info(`Closed PR #${issue_number} from ${pr.author.login}: ${pr.title}`)
83141
}

0 commit comments

Comments
 (0)