-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
190 lines (162 loc) · 5.32 KB
/
server.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import express from 'express'
import { Webhooks } from '@octokit/webhooks'
import { handleIssueComment, handlePRComment } from './pr-bot'
import { conventionalCommit, isTrustedUser } from './utils'
import { octokit } from './octokit'
import { analyzePR } from './analyze-pr'
import { startCron } from './cron'
const app = express()
const port = process.env.PORT || 3000
const webhooks = new Webhooks({
secret: 'summary',
})
app.use(express.json())
startCron()
app.post('/webhook', (req, res) => {
webhooks
.verifyAndReceive({
id: req.headers['x-github-delivery'] as string,
name: req.headers['x-github-event'] as any,
payload: JSON.stringify(req.body),
signature: req.headers['x-hub-signature-256'] as string,
})
.catch(console.error)
res.status(200).send('OK')
})
webhooks.on('pull_request.opened', async ({ payload }) => {
console.log('Received pull request:', payload)
const sender = payload.sender.login
const isMember = await isTrustedUser(
payload.repository.owner.login,
payload.repository.name,
sender,
)
analyzePR(
payload.repository.owner.login,
payload.repository.name,
payload.pull_request.number,
)
if (isMember) return
if (!isMember) {
await octokit.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number,
body: 'Thank you for your contribution. We will review it promptly.',
})
}
// Check pr title is Conventional Commits
const prTitle = payload.pull_request.title
const isConventionalCommit = conventionalCommit(prTitle)
if (!isConventionalCommit) {
await octokit.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number,
body: `@${sender}, please use Conventional Commits format for your PR title.
Your PR title should follow this format:
\`<type>(<scope>): <description>\`
Common types include:
- feat: A new feature
- fix: A bug fix
- docs: Documentation changes
- style: Code style changes (formatting, missing semi colons, etc)
- refactor: Code refactoring
- test: Adding or updating tests
- chore: Changes to build process or auxiliary tools
Examples:
- feat(user): add user login function
- fix(api): correct HTTP response status code
- docs(readme): update installation guide
For more details, please visit: https://www.conventionalcommits.org/
`,
})
}
})
webhooks.on('issues.opened', async ({ payload }) => {
console.log('Received issue opened:', payload)
if (payload.issue.performed_via_github_app?.owner?.login === 'linear') {
// Linear issue, created by Linear app, ignore
console.log(
`Ignore issue created by Linear app, [#${payload.issue.number} - ${payload.issue.title}]`,
)
return
}
const comment = payload.issue.body
if (
await isTrustedUser(
payload.repository.owner.login,
payload.repository.name,
payload.sender.login,
)
) {
return
}
const needIncludedTexts = ['- [x] This issue is valid', '### Environment']
const isIncluded = needIncludedTexts.some((text) => comment?.includes(text))
if (!isIncluded) {
console.log('Invalid issue ' + payload.issue.number, ' closed. ')
await octokit.issues.update({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
state: 'closed',
state_reason: 'not_planned',
})
await octokit.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
body: 'This issue is invalid. Please provide more information, or update the issue description and re-open the issue.',
})
}
})
webhooks.on('issue_comment.created', async ({ payload }) => {
if (!payload.issue.pull_request) {
return
}
const owner = payload.repository.owner.login
const repo = payload.repository.name
const prNumber = payload.issue.number
const commentBody = payload.comment.body
const commentId = payload.comment.id
console.log('Received comment:', commentBody)
const isPRComment = payload.issue.pull_request
if (isPRComment) {
await handlePRComment(owner, repo, prNumber, commentId, commentBody)
} else {
await handleIssueComment(
owner,
repo,
prNumber,
commentId,
commentBody,
payload.issue,
)
}
})
webhooks.on('pull_request.closed', async ({ payload }) => {
// Check if the PR was merged (closed without merging won't trigger this)
if (!payload.pull_request.merged) {
return
}
const sender = payload.pull_request.user.login
const isMember = await isTrustedUser(
payload.repository.owner.login,
payload.repository.name,
sender,
)
// Only send thank you message to external contributors
if (!isMember) {
await octokit.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number,
body: `Thank you @${sender} for your contribution! 🎉
Your pull request has been merged and we really appreciate your help in making this project better. We hope to see more contributions from you in the future! 💪`,
})
}
})
app.listen(port, () => {
console.log(`Server is running on port ${port}`)
})