From e4980a1c4d496c043fb818691b5724b5c1ee63d4 Mon Sep 17 00:00:00 2001 From: Martijn Vegter Date: Sat, 16 Nov 2019 13:33:09 +0100 Subject: [PATCH] If base is changed trigger update all --- index.js | 88 +++++++++++++++++++++++++++++++++++++++++++++-- package-lock.json | 6 ++-- package.json | 4 +-- 3 files changed, 91 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index b30cb4eff7..8115c96179 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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') { + await onPushTemplate(context) + } }) } diff --git a/package-lock.json b/package-lock.json index 15ee99c61f..74d2469d63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1619,9 +1619,9 @@ "dev": true }, "deepmerge": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.1.0.tgz", - "integrity": "sha512-tJlmKE535XRVBrzMRMqlm4V2S/l3yO/qr9Ad0MUqgR5jh9epGrALs8Q6+pROMa6ntDieAlnmYM/cDFCtNjp4+Q==" + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz", + "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==" }, "define-properties": { "version": "1.1.3", diff --git a/package.json b/package.json index 3df7de9ed2..f8b092e292 100644 --- a/package.json +++ b/package.json @@ -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": {