-
Notifications
You must be signed in to change notification settings - Fork 182
/
index.js
50 lines (39 loc) · 1.62 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
import mergeArrayByName from './lib/mergeArrayByName.js'
import SettingsApp from './lib/settings.js'
/**
* @param {import('probot').Probot} robot
*/
export default (robot, _, Settings = SettingsApp) => {
async function syncSettings (context, repo = context.repo()) {
const config = await context.config('settings.yml', {}, { arrayMerge: mergeArrayByName })
return Settings.sync(context.octokit, repo, config)
}
robot.on('push', async context => {
const { payload } = context
const { repository } = payload
const defaultBranch = payload.ref === 'refs/heads/' + repository.default_branch
if (!defaultBranch) {
robot.log.debug('Not working on the default branch, returning...')
return
}
const settingsModified = payload.commits.find(commit => {
return commit.added.includes(Settings.FILE_NAME) || commit.modified.includes(Settings.FILE_NAME)
})
if (!settingsModified) {
robot.log.debug(`No changes in '${Settings.FILE_NAME}' detected, returning...`)
return
}
return syncSettings(context)
})
robot.on('repository.edited', async context => {
const { payload } = context
const { changes, repository } = payload
if (!Object.prototype.hasOwnProperty.call(changes, 'default_branch')) {
robot.log.debug('Repository configuration was edited but the default branch was not affected, returning...')
return
}
robot.log.debug(`Default branch changed from '${changes.default_branch.from}' to '${repository.default_branch}'`)
return syncSettings(context)
})
robot.on('repository.created', async context => syncSettings(context))
}