-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (134 loc) · 4.57 KB
/
index.js
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
const assert = require('assert')
const http = require('http')
const fetch = require('node-fetch')
const debug = require('debug')
const webhooks = require('@octokit/webhooks')({
secret: process.env.GITHUB_TOKEN
})
const rest = require('@octokit/rest')()
rest.authenticate({
type: 'token',
token: process.env.GITHUB_TOKEN
})
const { Chess } = require('chess.js')
const parseBoard = board => {
const lines = board.replace(/^\s+|\s+$/g, '').split('\n')
assert(lines.length === 10, 'Lines must be 10')
lines.shift()
lines.shift()
const chess = new Chess()
chess.clear()
lines.forEach((line, index) => {
const lineIndex = 8 - index
const squares = line.split('|')
assert(squares.length === 10, `Line ${lineIndex} must have 9 lines`)
squares.shift()
squares.pop()
squares.forEach((square, index) => {
if (square === ' ' || square === ' ') return
const squareIndex = String.fromCharCode('a'.charCodeAt(0) + index) + lineIndex
const piece = {
'♜': { type: chess.ROOK, color: chess.BLACK },
'♞': { type: chess.KNIGHT, color: chess.BLACK },
'♝': { type: chess.BISHOP, color: chess.BLACK },
'♚': { type: chess.KING, color: chess.BLACK },
'♛': { type: chess.QUEEN, color: chess.BLACK },
'♟': { type: chess.PAWN, color: chess.BLACK },
'♖': { type: chess.ROOK, color: chess.WHITE },
'♘': { type: chess.KNIGHT, color: chess.WHITE },
'♗': { type: chess.BISHOP, color: chess.WHITE },
'♔': { type: chess.KING, color: chess.WHITE },
'♕': { type: chess.QUEEN, color: chess.WHITE },
'♙': { type: chess.PAWN, color: chess.WHITE }
}[square]
assert(piece !== undefined, `Unknown piece in ${squareIndex}: ${square}`)
assert(chess.put(piece, squareIndex), `Put ${JSON.stringify(piece)} into ${squareIndex} failed.`)
})
})
return chess.ascii()
}
webhooks.on(['pull_request.opened', 'pull_request.reopened'], async ({ payload }) => {
const log = debug(`chessbot:#${payload.number}`)
const { 'pull_request': pullRequest } = payload
try {
if (pullRequest.base.ref !== pullRequest.base.repo.default_branch) {
return log('Ignored', 'base is not default branch')
}
const repoMeta = {
owner: pullRequest.base.repo.owner.login,
repo: pullRequest.base.repo.name
}
const pullRequestMeta = {
...repoMeta,
number: pullRequest.number
}
try {
const { data: files } = await rest.pullRequests.getFiles({
...pullRequestMeta,
per_page: 2
})
log(files)
assert(files.length === 1, 'Only `README.md` should be changed.')
const file = files[0]
assert(file.filename === 'README.md' && file.status === 'modified',
'Only `README.md` should be changed.')
const board = await fetch(file.raw_url).then(response => {
assert(response.ok, 'Request content failed.')
return response.text()
})
log('Target Board', board)
const asciiBoard = parseBoard(board)
const { data: commits } = await rest.repos.getCommits({
...repoMeta,
per_page: 1
})
assert(commits.length > 0, 'There must be an initial commit.')
const message = commits[0].commit.message
log('Current history', message)
const chess = new Chess()
if (message !== 'Initial commit') {
const moves = message.split('\n').slice(2)
for (const move of moves) {
chess.move(move)
}
}
let currentMove = null
for (const move of chess.moves()) {
chess.move(move)
if (chess.ascii() === asciiBoard) {
currentMove = move
break
}
chess.undo()
}
assert(currentMove !== null, 'Invalid move')
log('Validate move, merging', currentMove)
return rest.pullRequests.merge({
...pullRequestMeta,
commit_title: `Moved by @${pullRequest.user.login}`,
commit_message: chess.history().join('\n'),
sha: pullRequest.head.sha,
merge_method: 'squash'
})
} catch (e) {
if (!(e instanceof assert.AssertionError)) {
throw e
}
await rest.issues.createComment({
...pullRequestMeta,
body: e.message
})
await rest.pullRequests.update({
...pullRequestMeta,
state: 'closed'
})
return log('Closed', e.message)
}
} catch (e) {
log('Uncaught error', e)
}
})
const server = module.exports = http.createServer(webhooks.middleware)
if (require.main === module) {
server.listen(process.env.PORT)
}