Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: If account-level config is changed trigger update all #179

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 86 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,91 @@
const yaml = require('js-yaml')
const merge = require('deepmerge')
const mergeArrayByName = require('./lib/mergeArrayByName')

module.exports = (robot, _, Settings = require('./lib/settings')) => {
async function onPush (context) {
const config = await context.config('settings.yml', {}, { arrayMerge: mergeArrayByName })
Settings.sync(context.github, context.repo(), config)
}

async function listRepos (github, { login, type }) {
var listForOrg
if (type.toLowerCase() === 'organization') {
listForOrg = github.paginate(
github.repos.listForOrg.endpoint.merge({
org: login
})
)
} else {
listForOrg = github.paginate(
github.repos.listForUser.endpoint.merge({
username: login
})
)
}

return listForOrg
}

async function loadYaml (github, params) {
try {
const response = await github.repos.getContents(params)

// Ignore in case path is a folder
// - https://developer.github.com/v3/repos/contents/#response-if-content-is-a-directory
if (Array.isArray(response.data)) {
return null
}

// we don't handle symlinks or submodule
// - https://developer.github.com/v3/repos/contents/#response-if-content-is-a-symlink
// - https://developer.github.com/v3/repos/contents/#response-if-content-is-a-submodule
if (typeof response.data.content !== 'string') {
return
}

return yaml.safeLoad(Buffer.from(response.data.content, 'base64').toString()) || {}
} catch (e) {
if (e.status === 404) {
return null
}

throw e
}
}

async function triggerRepositoryUpdate (context, baseConfig, { owner, repo }) {
const { github } = context

const config = await loadYaml(github, {
owner,
repo,
path: Settings.FILE_NAME
})

if (config === null) {
robot.log.debug(`File '${Settings.FILE_NAME}' not found in '${owner}/${repo}', returning...`)
return
}

return Settings.sync(context.github, { owner, repo }, merge(baseConfig, config))
}

async function onPushTemplate (context) {
const { github, payload } = context
const { login, type } = payload.repository.owner

const baseConfig = await context.config('settings.yml', {}, { arrayMerge: mergeArrayByName })
const repositories = await listRepos(github, { login, type })
await repositories.filter(repo => repo.name !== '.github').map(async (repo) => {
triggerRepositoryUpdate(context, baseConfig, { owner: login, repo: repo.name })
})
}

robot.on('push', async context => {
const { payload } = context
const { repository } = payload
const { name: repositoryName } = repository

const defaultBranch = payload.ref === 'refs/heads/' + repository.default_branch
if (!defaultBranch) {
Expand All @@ -21,7 +103,9 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => {
return
}

const config = await context.config('settings.yml', {}, { arrayMerge: mergeArrayByName })
return Settings.sync(context.github, context.repo(), config)
await onPush(context)
if (repositoryName === '.github') {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could make this configurable in case I don't want to name my .github repository ".github"? The main reason for that is that I have a master repo which has submodules with all the repos in the org. I currently named my "base" repo template, but I'm not married to that. However, I can't name it ".github", because then I can't have a submodule for it in the main stack repo, at least not have it named the same way as the .github repo itself. I could have a submodule "template" that points at the repo ".github", but it would be anomalous compared to everything else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are the types of details that make this implementation complex. thank you for raising this so that we can consider it. i wasnt personally aware that probot-config supported extending config from other repos than .github, but if that is supported there, it probably makes sense to find a way to enable when this goes out.

would you mind capturing those thoughts in #95 in case this PR ends up not being the way we move this effort forward?

await onPushTemplate(context)
}
})
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"author": "Brandon Keepers",
"license": "ISC",
"dependencies": {
"deepmerge": "^4.1.0",
"js-yaml": "^3.12.0",
"deepmerge": "^4.2.2",
"js-yaml": "^3.13.1",
"probot": "^9.5.0"
},
"devDependencies": {
Expand Down