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

Added enhancement Sync settings after installing the integration #74

Closed
wants to merge 9 commits into from
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
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
7 changes: 7 additions & 0 deletions src/deleteRef.js
Original file line number Diff line number Diff line change
@@ -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
82 changes: 82 additions & 0 deletions src/installSync.js
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions src/template.js
Original file line number Diff line number Diff line change
@@ -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
`