Skip to content

Commit

Permalink
fix: minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
hfreire committed May 22, 2019
1 parent 102cba2 commit b2ab950
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ provider:
memorySize: 128
environment:
GH_TOKEN: ${env:GH_TOKEN}
GH_OWNERS: ${env:GH_OWNERS}
GH_ORGS: ${env:GH_ORGS}

plugins:
- serverless-plugin-include-dependencies
Expand Down
19 changes: 11 additions & 8 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@
*/

const GH_TOKEN = process.env.GH_TOKEN
const GH_OWNERS = process.env.GH_OWNERS
const GH_ORGS = process.env.GH_ORGS

const _ = require('lodash')

const GitHubWrapper = require('@dog-ai/github-wrapper')

const owners = _.words(GH_OWNERS, /[^, ]+/g) || []
const github = new GitHubWrapper({ github: { type: 'token', token: GH_TOKEN } })
const github = new GitHubWrapper({ octokit: { auth: GH_TOKEN } })

module.exports = async () => {
const mergedGreenkeeperPullRequests = []
const orgs = _.words(GH_ORGS, /[^, ]+/g) || []
const owners = [ undefined ].concat(orgs)

github.on('error', (error, owner, repo, { url }) => console.warn(`Failed to merge Greenkeeper pull request ${url} because of ${error.message}`))
github.on('pulls:create', (owner, repo, title) => console.info(`Created pull request ${owner}/${repo} ${title}`))
github.on('pulls:merge', (owner, repo, number) => console.info(`Merged pull request ${owner}/${repo} ${number}`))
github.on('pulls:close', (owner, repo, number) => console.info(`Closed pull request ${owner}/${repo} ${number}`))

module.exports = async () => {
for (const owner of owners) {
try {
mergedGreenkeeperPullRequests.concat(await github.mergeGreenkeeperPullRequests(owner))
await github.mergeGreenkeeperPullRequests(owner, { repoConcurrency: 10, pullConcurrency: 5 })
} catch (error) {
console.error(error)
} finally {
console.log(`Merged ${mergedGreenkeeperPullRequests.length} Greenkeeper Pull Requests: ${mergedGreenkeeperPullRequests}`)
}
}
}
20 changes: 15 additions & 5 deletions test/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,36 @@ describe('Handler', () => {

beforeAll(() => {
GitHubWrapper = require('@dog-ai/github-wrapper')
jest.mock('@dog-ai/github-wrapper')
//jest.mock('@dog-ai/github-wrapper')
})

describe('when handling a schedule event', () => {
const token = 'my-token'
const owners = 'my-owner'
const orgs = 'my-owner'
let mergeGreenkeeperPullRequests

beforeAll(() => {
process.env.GH_TOKEN = token
process.env.GH_OWNERS = owners
//process.env.GH_TOKEN = token
//process.env.GH_ORGS = orgs

mergeGreenkeeperPullRequests = jest.fn().mockImplementation(() => {
return {}
})
})

beforeEach(() => {
//GitHubWrapper.mockImplementation(() => {
// return { mergeGreenkeeperPullRequests }
//})

subject = require('../src/handler')
})

it('should call merge greenkeeper pull requests', async () => {
jest.setTimeout(400000)
await subject()

expect(GitHubWrapper.mock.instances[ 0 ].mergeGreenkeeperPullRequests).toHaveBeenCalledTimes(1)
expect(mergeGreenkeeperPullRequests).toHaveBeenCalledTimes(2)
})
})
})

0 comments on commit b2ab950

Please sign in to comment.