-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
54 lines (46 loc) · 1.96 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
const yaml = require('js-yaml')
const { createProbot } = require('probot')
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Probot} app
*/
module.exports = (app) => {
app.log.info('Yay, the app was loaded!')
console.log(`APP_ID is ${process.env.APP_ID}`)
app.on('repository.created', async (context) => {
const owner = context.payload.repository.owner.login
const repo = context.payload.repository.name
context.log.debug(`Repository ${repo} created in org ${owner}`)
// Loading the setting file in <org>/.github-private/permissions.app.yml
try {
const settingFile = await context.octokit.repos.getContent({
'owner': owner,
'repo': '.github-private',
'path': 'permissions.app.yml'
})
app.log.info('Got the settings')
const buff = Buffer.from(settingFile.data.content, settingFile.data.encoding) // encoding should be 'base64'
const settings = yaml.load(buff.toString())
context.log.debug(`Settings retrieved: ${JSON.stringify(settings)}`)
context.log.debug(`Received content ${settingFile.data.content} with encoding ${settingFile.data.encoding}`)
for (const [permission, teamArray] of Object.entries(settings)) {
for(const team of teamArray) {
context.log.debug(`Setting ${permission} permission to team ${team} on repo ${repo} in org ${owner}`)
try {
await context.octokit.teams.addOrUpdateRepoPermissionsInOrg({
'org': owner,
'team_slug': team,
'owner': owner,
'repo': repo,
'permission': permission
})
} catch(error) {
context.log.error(`Failed to set permission ${permission} for team ${team} on ${owner}/${repo} with message: ${error.message}`)
}
}
}
} catch(error) {
context.log.error(`Failed to retrieve settings: ${error.message}`)
}
})
}