From 6db6698a98f8dba3a82627a0b21e096945148bb6 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Mon, 19 Mar 2018 15:19:51 +0530 Subject: [PATCH 1/9] added enchancement sync settings after install --- encode.js | 0 index.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 encode.js diff --git a/encode.js b/encode.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/index.js b/index.js index f66d52f7f7..f730b6d667 100644 --- a/index.js +++ b/index.js @@ -14,4 +14,90 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { return Settings.sync(context.github, context.repo()) } } + + robot.on('installation_repositories.added', installSync) + + async function installSync (context) { + const payload = context.payload + // getting all the repos that are adding + const repoAddedId = await payload.repositories_added + const repoAddedIds = [] + repoAddedId.forEach(async function (value) { + await repoAddedIds.push(value.id) + }) + repoAddedIds.forEach(async function (value) { + const result = await context.github.repos.getById({id: value}) + const owner = result.data.owner.login + const repoName = result.data.name + // 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(async file => { + if (file.name === 'settings.yml') { + return file.name + } else { + try { + const reference = await context.github.gitdata.getReference({owner: owner, repo: repoName, ref: 'heads/master'}) + const refData = reference.data + const sha = refData.object.sha + try { + await context.github.gitdata.createReference({owner: owner, repo: repoName, ref: 'refs/heads/probot', sha: sha}) + // setting the template of file + const string = 'repository: \n' + + ' name: repo-name \n' + + ' description: description of repo \n' + + ' homepage: www.google.com \n' + + ' topics: github, probot \n' + + ' private: true \n' + + ' has_issues: true \n' + + ' has_projects: true \n' + + ' has_wiki: true \n' + + ' has_downloads: true \n' + + ' default_branch: master \n' + + ' allow_squash_merge: true \n' + + ' allow_merge_commit: true \n' + + ' allow_rebase_merge: true \n\n' + + 'labels: \n' + + ' - name: bug \n' + + ' color: CC0000 \n' + + ' - name: feature \n' + + ' color: 336699 \n' + + ' - name: first-timers-only \n' + + ' oldname: bug \n\n' + + 'collaborators: \n' + + ' - username: your username \n' + + ' permission: push' + + const base64 = require('./encode') + const encodedString = base64.encode(string) + // 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) + } + } + }) + // 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) { + context.log('Wrong path create a .github folder') + } + }) + } } From 82ee56d47dd36c0d61e9ac4ff911909d3a0aea43 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Mon, 19 Mar 2018 15:23:07 +0530 Subject: [PATCH 2/9] added enchancement sync settings after install --- encode.js | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/encode.js b/encode.js index e69de29bb2..2962cb9c14 100644 --- a/encode.js +++ b/encode.js @@ -0,0 +1,118 @@ +const Base64 = { + + _keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', + + encode: function (input) { + var output = '' + var chr1, chr2, chr3, enc1, enc2, enc3, enc4 + var i = 0 + + input = Base64._utf8_encode(input) + + while (i < input.length) { + chr1 = input.charCodeAt(i++) + chr2 = input.charCodeAt(i++) + chr3 = input.charCodeAt(i++) + + enc1 = chr1 >> 2 + enc2 = ((chr1 & 3) << 4) | (chr2 >> 4) + enc3 = ((chr2 & 15) << 2) | (chr3 >> 6) + enc4 = chr3 & 63 + + if (isNaN(chr2)) { + enc3 = enc4 = 64 + } else if (isNaN(chr3)) { + enc4 = 64 + } + + output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4) + } + + return output + }, + + decode: function (input) { + var output = '' + var chr1, chr2, chr3 + var enc1, enc2, enc3, enc4 + var i = 0 + + input = input.replace(/[^A-Za-z0-9]/g, '') + + while (i < input.length) { + enc1 = this._keyStr.indexOf(input.charAt(i++)) + enc2 = this._keyStr.indexOf(input.charAt(i++)) + enc3 = this._keyStr.indexOf(input.charAt(i++)) + enc4 = this._keyStr.indexOf(input.charAt(i++)) + + chr1 = (enc1 << 2) | (enc2 >> 4) + chr2 = ((enc2 & 15) << 4) | (enc3 >> 2) + chr3 = ((enc3 & 3) << 6) | enc4 + + output = output + String.fromCharCode(chr1) + + if (enc3 !== 64) { + output = output + String.fromCharCode(chr2) + } + if (enc4 !== 64) { + output = output + String.fromCharCode(chr3) + } + } + + output = Base64._utf8_decode(output) + + return output + }, + + _utf8_encode: function (string) { + string = string.replace(/\r\n/g, '\n') + var utftext = '' + + for (var n = 0; n < string.length; n++) { + var c = string.charCodeAt(n) + + if (c < 128) { + utftext += String.fromCharCode(c) + } else if ((c > 127) && (c < 2048)) { + utftext += String.fromCharCode((c >> 6) | 192) + utftext += String.fromCharCode((c & 63) | 128) + } else { + utftext += String.fromCharCode((c >> 12) | 224) + utftext += String.fromCharCode(((c >> 6) & 63) | 128) + utftext += String.fromCharCode((c & 63) | 128) + } + } + + return utftext + }, + + _utf8_decode: function (utftext) { + var string = '' + var i = 0 + var c, c2, c3 + c = c2 = 0 + + while (i < utftext.length) { + c = utftext.charCodeAt(i) + + if (c < 128) { + string += String.fromCharCode(c) + i++ + } else if ((c > 191) && (c < 224)) { + c2 = utftext.charCodeAt(i + 1) + string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)) + i += 2 + } else { + c2 = utftext.charCodeAt(i + 1) + c3 = utftext.charCodeAt(i + 2) + string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)) + i += 3 + } + } + + return string + } + +} + +module.exports = Base64 From 5edc089721bc8423cc2cf52452d7790cbcf2df81 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Wed, 21 Mar 2018 23:21:01 +0530 Subject: [PATCH 3/9] create repo according to present repo settings --- index.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index f730b6d667..4391e39b0d 100644 --- a/index.js +++ b/index.js @@ -48,22 +48,23 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { const refData = reference.data const sha = refData.object.sha try { - await context.github.gitdata.createReference({owner: owner, repo: repoName, ref: 'refs/heads/probot', sha: sha}) + const getRepo = await context.github.repos.get({owner: owner, repo: repoName}) + await context.github.gitdata.createReference({owner: owner, repo: repoName, ref: 'refs/heads/test2', sha: sha}) // setting the template of file const string = 'repository: \n' + - ' name: repo-name \n' + - ' description: description of repo \n' + - ' homepage: www.google.com \n' + + ' name: ' + getRepo.data.name + ' \n' + + ' description: ' + getRepo.data.description + ' \n' + + ' homepage: ' + getRepo.data.homepage + ' \n' + ' topics: github, probot \n' + - ' private: true \n' + - ' has_issues: true \n' + - ' has_projects: true \n' + - ' has_wiki: true \n' + - ' has_downloads: true \n' + - ' default_branch: master \n' + - ' allow_squash_merge: true \n' + - ' allow_merge_commit: true \n' + - ' allow_rebase_merge: true \n\n' + + ' private: ' + getRepo.data.private + ' \n' + + ' has_issues: ' + getRepo.data.has_issues + ' \n' + + ' has_projects: ' + getRepo.data.has_projects + ' \n' + + ' has_wiki: ' + getRepo.data.has_wiki + ' \n' + + ' has_downloads: ' + getRepo.data.has_downloads + ' \n' + + ' default_branch: ' + getRepo.data.default_branch + ' \n' + + ' allow_squash_merge: ' + getRepo.data.allow_squash_merge + ' \n' + + ' allow_merge_commit: ' + getRepo.data.allow_merge_commit + ' \n' + + ' allow_rebase_merge: ' + getRepo.data.allow_rebase_merge + ' \n\n' + 'labels: \n' + ' - name: bug \n' + ' color: CC0000 \n' + @@ -72,15 +73,15 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { ' - name: first-timers-only \n' + ' oldname: bug \n\n' + 'collaborators: \n' + - ' - username: your username \n' + + ' - username: enter any username \n' + ' permission: push' const base64 = require('./encode') const encodedString = base64.encode(string) // 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'}) + await context.github.repos.createFile({owner: owner, repo: repoName, path: '.github/settings.yml', message: 'adding settings.yml file', content: encodedString, branch: 'test2'}) // 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'}) + await context.github.pullRequests.create({owner: owner, repo: repoName, head: 'test2', base: 'master', title: 'Settings Bot adding config file', body: 'Merge it to configure the bot'}) } catch (error) { context.log(error) } From 61d3d7667c81338fca9bd69cccee3075a1df75d8 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Mon, 26 Mar 2018 13:15:48 +0530 Subject: [PATCH 4/9] corrected the reference --- encode.js | 118 ---------------------------------------------------- index.js | 35 +++------------- template.js | 28 +++++++++++++ 3 files changed, 34 insertions(+), 147 deletions(-) delete mode 100644 encode.js create mode 100644 template.js diff --git a/encode.js b/encode.js deleted file mode 100644 index 2962cb9c14..0000000000 --- a/encode.js +++ /dev/null @@ -1,118 +0,0 @@ -const Base64 = { - - _keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', - - encode: function (input) { - var output = '' - var chr1, chr2, chr3, enc1, enc2, enc3, enc4 - var i = 0 - - input = Base64._utf8_encode(input) - - while (i < input.length) { - chr1 = input.charCodeAt(i++) - chr2 = input.charCodeAt(i++) - chr3 = input.charCodeAt(i++) - - enc1 = chr1 >> 2 - enc2 = ((chr1 & 3) << 4) | (chr2 >> 4) - enc3 = ((chr2 & 15) << 2) | (chr3 >> 6) - enc4 = chr3 & 63 - - if (isNaN(chr2)) { - enc3 = enc4 = 64 - } else if (isNaN(chr3)) { - enc4 = 64 - } - - output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4) - } - - return output - }, - - decode: function (input) { - var output = '' - var chr1, chr2, chr3 - var enc1, enc2, enc3, enc4 - var i = 0 - - input = input.replace(/[^A-Za-z0-9]/g, '') - - while (i < input.length) { - enc1 = this._keyStr.indexOf(input.charAt(i++)) - enc2 = this._keyStr.indexOf(input.charAt(i++)) - enc3 = this._keyStr.indexOf(input.charAt(i++)) - enc4 = this._keyStr.indexOf(input.charAt(i++)) - - chr1 = (enc1 << 2) | (enc2 >> 4) - chr2 = ((enc2 & 15) << 4) | (enc3 >> 2) - chr3 = ((enc3 & 3) << 6) | enc4 - - output = output + String.fromCharCode(chr1) - - if (enc3 !== 64) { - output = output + String.fromCharCode(chr2) - } - if (enc4 !== 64) { - output = output + String.fromCharCode(chr3) - } - } - - output = Base64._utf8_decode(output) - - return output - }, - - _utf8_encode: function (string) { - string = string.replace(/\r\n/g, '\n') - var utftext = '' - - for (var n = 0; n < string.length; n++) { - var c = string.charCodeAt(n) - - if (c < 128) { - utftext += String.fromCharCode(c) - } else if ((c > 127) && (c < 2048)) { - utftext += String.fromCharCode((c >> 6) | 192) - utftext += String.fromCharCode((c & 63) | 128) - } else { - utftext += String.fromCharCode((c >> 12) | 224) - utftext += String.fromCharCode(((c >> 6) & 63) | 128) - utftext += String.fromCharCode((c & 63) | 128) - } - } - - return utftext - }, - - _utf8_decode: function (utftext) { - var string = '' - var i = 0 - var c, c2, c3 - c = c2 = 0 - - while (i < utftext.length) { - c = utftext.charCodeAt(i) - - if (c < 128) { - string += String.fromCharCode(c) - i++ - } else if ((c > 191) && (c < 224)) { - c2 = utftext.charCodeAt(i + 1) - string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)) - i += 2 - } else { - c2 = utftext.charCodeAt(i + 1) - c3 = utftext.charCodeAt(i + 2) - string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)) - i += 3 - } - } - - return string - } - -} - -module.exports = Base64 diff --git a/index.js b/index.js index 4391e39b0d..ad364ec1eb 100644 --- a/index.js +++ b/index.js @@ -49,39 +49,16 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { const sha = 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/test2', sha: sha}) + await context.github.gitdata.createReference({owner: owner, repo: repoName, ref: 'refs/heads/probot', sha: sha}) // setting the template of file - const string = 'repository: \n' + - ' name: ' + getRepo.data.name + ' \n' + - ' description: ' + getRepo.data.description + ' \n' + - ' homepage: ' + getRepo.data.homepage + ' \n' + - ' topics: github, probot \n' + - ' private: ' + getRepo.data.private + ' \n' + - ' has_issues: ' + getRepo.data.has_issues + ' \n' + - ' has_projects: ' + getRepo.data.has_projects + ' \n' + - ' has_wiki: ' + getRepo.data.has_wiki + ' \n' + - ' has_downloads: ' + getRepo.data.has_downloads + ' \n' + - ' default_branch: ' + getRepo.data.default_branch + ' \n' + - ' allow_squash_merge: ' + getRepo.data.allow_squash_merge + ' \n' + - ' allow_merge_commit: ' + getRepo.data.allow_merge_commit + ' \n' + - ' allow_rebase_merge: ' + getRepo.data.allow_rebase_merge + ' \n\n' + - 'labels: \n' + - ' - name: bug \n' + - ' color: CC0000 \n' + - ' - name: feature \n' + - ' color: 336699 \n' + - ' - name: first-timers-only \n' + - ' oldname: bug \n\n' + - 'collaborators: \n' + - ' - username: enter any username \n' + - ' permission: push' + const template = require('./template') + const string = template(getRepo) - const base64 = require('./encode') - const encodedString = base64.encode(string) + 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: 'test2'}) + 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: 'test2', base: 'master', title: 'Settings Bot adding config file', body: 'Merge it to configure the bot'}) + 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) } diff --git a/template.js b/template.js new file mode 100644 index 0000000000..721ebb1e32 --- /dev/null +++ b/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 +` From 9c75dd94342d9cf450df937bc561906c80a86750 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Fri, 13 Apr 2018 17:12:56 +0530 Subject: [PATCH 5/9] delete branch when PR is merged --- index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.js b/index.js index ad364ec1eb..46adc5da3c 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,7 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { const sha = 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') @@ -78,4 +79,13 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { } }) } + + // deleting the reference when pull request is merged + robot.on('pull_request.closed', deleteRef) + + async function deleteRef (context) { + if (context.payload.pull_request.head.ref === 'probot') { + await context.github.gitdata.deleteReference(context.repo({ref: 'heads/probot'})) + } + } } From cf68cfd47502dd29eec41e7b5a0604a91e52ec95 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Wed, 18 Apr 2018 20:47:01 +0530 Subject: [PATCH 6/9] Create a folder automatically --- index.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 46adc5da3c..3080cdfbd4 100644 --- a/index.js +++ b/index.js @@ -75,7 +75,29 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { context.log('file not found so creating a pull request') } } catch (error) { - context.log('Wrong path create a .github folder') + try { + const reference = await context.github.gitdata.getReference({owner: owner, repo: repoName, ref: 'heads/master'}) + const refData = reference.data + const sha = 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) + } } }) } From e90cc27dfb9e52a44fe2da60cbb34d4f509f3362 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Thu, 19 Apr 2018 11:15:00 +0530 Subject: [PATCH 7/9] Fixed Bugs --- index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 3080cdfbd4..0e1d95ea45 100644 --- a/index.js +++ b/index.js @@ -15,12 +15,12 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { } } - robot.on('installation_repositories.added', installSync) + robot.on(['installation.created', 'installation_repositories.added'], installSync) async function installSync (context) { const payload = context.payload // getting all the repos that are adding - const repoAddedId = await payload.repositories_added + const repoAddedId = await (payload.repositories_added || payload.repositories) const repoAddedIds = [] repoAddedId.forEach(async function (value) { await repoAddedIds.push(value.id) @@ -38,28 +38,27 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { const path = '.github' try { const repoInfo = await context.github.repos.getContent({owner: owner, repo: repoName, path: path}) - - const FILE_NAME = repoInfo.data.find(async file => { + const FILE_NAME = repoInfo.data.find(file => { if (file.name === 'settings.yml') { return file.name } else { try { - const reference = await context.github.gitdata.getReference({owner: owner, repo: repoName, ref: 'heads/master'}) + 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 = await context.github.repos.get({owner: owner, repo: repoName}) + const getRepo = context.github.repos.get({owner: owner, repo: repoName}) - await context.github.gitdata.createReference({owner: owner, repo: repoName, ref: 'refs/heads/probot', sha: sha}) + 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'}) + 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'}) + 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) } @@ -68,6 +67,7 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { } } }) + // syncying the file if present if (FILE_NAME.name === 'settings.yml') { return Settings.sync(context.github, repo) From 818df335ce97022307ca3814908d8b22cf61a690 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Sat, 21 Apr 2018 08:09:40 +0530 Subject: [PATCH 8/9] Reducing index.js --- index.js | 99 +++------------------------------- src/deleteRef.js | 7 +++ src/installSync.js | 86 +++++++++++++++++++++++++++++ template.js => src/template.js | 0 4 files changed, 99 insertions(+), 93 deletions(-) create mode 100644 src/deleteRef.js create mode 100644 src/installSync.js rename template.js => src/template.js (100%) diff --git a/index.js b/index.js index 0e1d95ea45..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) @@ -15,99 +18,9 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => { } } - robot.on(['installation.created', 'installation_repositories.added'], installSync) - - async function installSync (context) { - const payload = context.payload - // getting all the repos that are adding - const repoAddedId = await (payload.repositories_added || payload.repositories) - const repoAddedIds = [] - repoAddedId.forEach(async function (value) { - await repoAddedIds.push(value.id) - }) - repoAddedIds.forEach(async function (value) { - const result = await context.github.repos.getById({id: value}) - const owner = result.data.owner.login - const repoName = result.data.name - // 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 = reference.data - const sha = refData.object.sha - try { - const getRepo = await context.github.repos.get({owner: owner, repo: repoName}) + // Bot Syncing when new app is installed or new repo added to bot configuration + robot.on(['installation.created', 'installation_repositories.added'], installSync, Settings) - 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) - } - } - }) - } - - // deleting the reference when pull request is merged + // deleting the reference when pull request is merged or closed robot.on('pull_request.closed', deleteRef) - - async function deleteRef (context) { - if (context.payload.pull_request.head.ref === 'probot') { - await context.github.gitdata.deleteReference(context.repo({ref: 'heads/probot'})) - } - } } 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..54c72549c0 --- /dev/null +++ b/src/installSync.js @@ -0,0 +1,86 @@ +async function installSync (context, Settings) { + const payload = context.payload + // getting all the repos that are adding + const repoAddedId = await (payload.repositories_added || payload.repositories) + const repoAddedIds = [] + repoAddedId.forEach(async function (value) { + await repoAddedIds.push(value.id) + }) + repoAddedIds.forEach(async function (value) { + const result = await context.github.repos.getById({id: value}) + const owner = result.data.owner.login + const repoName = result.data.name + // 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/template.js b/src/template.js similarity index 100% rename from template.js rename to src/template.js From 674e60708437cf73aa00210059d6280c8fa52b50 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Sun, 22 Apr 2018 10:01:40 +0530 Subject: [PATCH 9/9] adding module --- package.json | 4 +- src/installSync.js | 128 ++++++++++++++++++++++----------------------- 2 files changed, 65 insertions(+), 67 deletions(-) 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/installSync.js b/src/installSync.js index 54c72549c0..c108577433 100644 --- a/src/installSync.js +++ b/src/installSync.js @@ -1,85 +1,81 @@ +const repos = require('probot-repos') + async function installSync (context, Settings) { - const payload = context.payload - // getting all the repos that are adding - const repoAddedId = await (payload.repositories_added || payload.repositories) - const repoAddedIds = [] - repoAddedId.forEach(async function (value) { - await repoAddedIds.push(value.id) - }) - repoAddedIds.forEach(async function (value) { - const result = await context.github.repos.getById({id: value}) - const owner = result.data.owner.login - const repoName = result.data.name - // 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 + 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 getRepo = context.github.repos.get({owner: owner, repo: repoName}) + 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) + 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'}) + 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) } - } 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 + // 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 getRepo = await context.github.repos.get({owner: owner, repo: repoName}) + 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) + 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'}) + 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) } - } catch (error) { - context.log(error) } - } + }) }) }