diff --git a/index.js b/index.js index f66d52f7f7..6808515d56 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,6 @@ +const installSync = require('./src/installSync') +const deleteRef = require('./src/deleteRef') + module.exports = (robot, _, Settings = require('./lib/settings')) => { robot.on('push', receive) @@ -14,4 +17,10 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { return Settings.sync(context.github, context.repo()) } } + + // Bot Syncing when new app is installed or new repo added to bot configuration + robot.on(['installation.created', 'installation_repositories.added'], installSync, Settings) + + // deleting the reference when pull request is merged or closed + robot.on('pull_request.closed', deleteRef) } diff --git a/package.json b/package.json index babce55ed5..238729ba5b 100644 --- a/package.json +++ b/package.json @@ -12,10 +12,12 @@ "license": "ISC", "dependencies": { "js-yaml": "^3.7.0", - "probot": "^6.0.0" + "probot": "^6.0.0", + "probot-repos": "^1.0.5" }, "devDependencies": { "jest": "^22.0.0", + "nock": "^9.0.22", "smee-client": "^1.0.1", "standard": "^11.0.0" }, diff --git a/src/deleteRef.js b/src/deleteRef.js new file mode 100644 index 0000000000..bf901a0f01 --- /dev/null +++ b/src/deleteRef.js @@ -0,0 +1,7 @@ +async function deleteRef (context) { + if (context.payload.pull_request.head.ref === 'probot') { + await context.github.gitdata.deleteReference(context.repo({ref: 'heads/probot'})) + } +} + +module.exports = deleteRef diff --git a/src/installSync.js b/src/installSync.js new file mode 100644 index 0000000000..c108577433 --- /dev/null +++ b/src/installSync.js @@ -0,0 +1,82 @@ +const repos = require('probot-repos') + +async function installSync (context, Settings) { + repos(context).then((val) => { + val.forEach(async function (value) { + const owner = value.owner + const repoName = value.repo + // As context.repo() was undefined so had to convert it into object + const repo = { + owner: owner, + repo: repoName + } + // repo should have a .github folder + const path = '.github' + try { + const repoInfo = await context.github.repos.getContent({owner: owner, repo: repoName, path: path}) + const FILE_NAME = repoInfo.data.find(file => { + if (file.name === 'settings.yml') { + return file.name + } else { + try { + const reference = context.github.gitdata.getReference({owner: owner, repo: repoName, ref: 'heads/master'}) + const refData = reference.data + const sha = refData.object.sha + try { + const getRepo = context.github.repos.get({owner: owner, repo: repoName}) + + context.github.gitdata.createReference({owner: owner, repo: repoName, ref: 'refs/heads/probot', sha: sha}) + // setting the template of file + const template = require('./template') + const string = template(getRepo) + + const encodedString = Buffer.from(string).toString('base64') + // creating a file + context.github.repos.createFile({owner: owner, repo: repoName, path: '.github/settings.yml', message: 'adding settings.yml file', content: encodedString, branch: 'probot'}) + // creating pull request + context.github.pullRequests.create({owner: owner, repo: repoName, head: 'probot', base: 'master', title: 'Settings Bot adding config file', body: 'Merge it to configure the bot'}) + } catch (error) { + context.log(error) + } + } catch (error) { + context.log(error) + } + } + }) + + // syncying the file if present + if (FILE_NAME.name === 'settings.yml') { + return Settings.sync(context.github, repo) + } else { + context.log('file not found so creating a pull request') + } + } catch (error) { + try { + const reference = await context.github.gitdata.getReference({owner: owner, repo: repoName, ref: 'heads/master'}) + const refData = await reference.data + const sha = await refData.object.sha + try { + const getRepo = await context.github.repos.get({owner: owner, repo: repoName}) + + await context.github.gitdata.createReference({owner: owner, repo: repoName, ref: 'refs/heads/probot', sha: sha}) + // setting the template of file + const template = require('./template') + const string = template(getRepo) + + const encodedString = Buffer.from(string).toString('base64') + // creating a file + await context.github.repos.createFile({owner: owner, repo: repoName, path: '.github/settings.yml', message: 'adding settings.yml file', content: encodedString, branch: 'probot'}) + // creating pull request + await context.github.pullRequests.create({owner: owner, repo: repoName, head: 'probot', base: 'master', title: 'Settings Bot adding config file', body: 'Merge it to configure the bot'}) + } catch (error) { + context.log(error) + } + } catch (error) { + context.log(error) + } + } + }) + }) +} + +module.exports = installSync diff --git a/src/template.js b/src/template.js new file mode 100644 index 0000000000..721ebb1e32 --- /dev/null +++ b/src/template.js @@ -0,0 +1,28 @@ +module.exports = getRepo => ` + repository: + name: ${getRepo.data.name} + description: ${getRepo.data.description} + homepage: ${getRepo.data.homepage} + topics: github, probot + private: ${getRepo.data.private} + has_issues: ${getRepo.data.has_issues} + has_projects: ${getRepo.data.has_projects} + has_wiki: ${getRepo.data.has_wiki} + has_downloads: ${getRepo.data.has_downloads} + default_branch: ${getRepo.data.default_branch} + allow_squash_merge: ${getRepo.data.allow_squash_merge} + allow_merge_commit: ${getRepo.data.allow_merge_commit} + allow_rebase_merge: ${getRepo.data.allow_rebase_merge} + + labels: + - name: bug + color: CC0000 + - name: feature + color: 336699 + - name: first-timers-only + oldname: bug + + collaborators: + - username: enter any username + permission: push +`