Repository navigation
87 lines (81 loc) · 3.44 KB
/
Copy pathpr_limit.yml
File metadata and controls
87 lines (81 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
name: pr-limit
# Contributors may have at most MAX_OPEN_PRS open pull requests at a time.
# This labels and comments on pull requests over the limit,
# and removes the label once the author is back under it.
#
# pull_request_target runs with a write token, so this workflow must never
# check out or run code from the pull request. It only calls the GitHub API.
on:
pull_request_target:
types: [opened, reopened, closed]
permissions:
issues: write
pull-requests: write
env:
MAX_OPEN_PRS: 3
LABEL: too-many-prs
jobs:
check:
runs-on: ubuntu-latest
# Maintainers and bots are exempt.
if: >-
github.event.pull_request.user.type != 'Bot' &&
!contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.pull_request.author_association)
steps:
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const max = Number(process.env.MAX_OPEN_PRS);
const label = process.env.LABEL;
const { owner, repo } = context.repo;
const pr = context.payload.pull_request;
const author = pr.user.login;
const open = (
await github.paginate(github.rest.pulls.list, {
owner, repo, state: "open", per_page: 100,
})
).filter((p) => p.user.login === author);
core.info(`${author} has ${open.length} open pull request(s).`);
const hasLabel = (p) => p.labels.some((l) => l.name === label);
if (context.payload.action === "closed") {
if (open.length > max) return;
for (const p of open.filter(hasLabel)) {
await github.rest.issues.removeLabel({
owner, repo, issue_number: p.number, name: label,
});
}
return;
}
if (open.length <= max) return;
try {
await github.rest.issues.getLabel({ owner, repo, name: label });
} catch (error) {
if (error.status !== 404) throw error;
await github.rest.issues.createLabel({
owner, repo, name: label, color: "D93F0B",
description: `Author has more than ${max} open pull requests`,
});
}
await github.rest.issues.addLabels({
owner, repo, issue_number: pr.number, labels: [label],
});
const readme = `${context.serverUrl}/${owner}/${repo}/blob/HEAD/README.md`;
const list = open
.filter((p) => p.number !== pr.number)
.map((p) => `- #${p.number}`)
.join("\n");
await github.rest.issues.createComment({
owner, repo, issue_number: pr.number,
body: [
`Merhaba @${author}, katkınız için teşekkürler!`,
"",
`Şu anda ${open.length} açık pull request'iniz var.`,
`Her katkıyı dikkatle inceleyebilmek için bir kişinin aynı anda en fazla ${max} açık pull request'i olabilir.`,
"Lütfen yeni bir pull request açmadan önce açık olanlardan birinin birleştirilmesini ya da kapatılmasını bekleyin.",
"",
"Diğer açık pull request'leriniz:",
list,
"",
`Ayrıntılar için [README](${readme}) dosyasına bakabilirsiniz.`,
].join("\n"),
});