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 6 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
96 changes: 96 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,100 @@ 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 {
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)
}
}
})
// 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})

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
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'}))
}
}
}
28 changes: 28 additions & 0 deletions 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
`