Skip to content

Commit

Permalink
feat: queue all github requests, get token before request
Browse files Browse the repository at this point in the history
  • Loading branch information
finnp authored and christophwitzko committed Jun 9, 2017
1 parent 5982795 commit 8dd3fc5
Show file tree
Hide file tree
Showing 19 changed files with 166 additions and 163 deletions.
20 changes: 8 additions & 12 deletions jobs/create-initial-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ const escapeRegex = require('escape-string-regexp')

const RegClient = require('../lib/npm-registry-client')
const env = require('../lib/env')
const Github = require('../lib/github')
const getRangedVersion = require('../lib/get-ranged-version')
const dbs = require('../lib/dbs')
const getToken = require('../lib/get-token')
const getConfig = require('../lib/get-config')
const createBranch = require('../lib/create-branch')
const statsd = require('../lib/statsd')
const { updateRepoDoc } = require('../lib/repository-docs')
const githubQueue = require('../lib/github-write-queue')
const githubQueue = require('../lib/github-queue')
const { maybeUpdatePaymentsJob } = require('../lib/payments')
const upsert = require('../lib/upsert')

Expand All @@ -31,13 +29,11 @@ module.exports = async function ({ repositoryId }) {
const repoDoc = await repositories.get(repositoryId)
const accountId = repoDoc.accountId
const installation = await installations.get(accountId)
const { token } = await getToken(installation.installation)
const github = Github()
github.authenticate({ type: 'token', token })
const installationId = installation.installation

if (repoDoc.fork && !repoDoc.hasIssues) return

await updateRepoDoc(github, repoDoc)
await updateRepoDoc(installationId, repoDoc)
if (!_.get(repoDoc, ['packages', 'package.json'])) return
await upsert(repositories, repoDoc._id, repoDoc)

Expand All @@ -48,7 +44,7 @@ module.exports = async function ({ repositoryId }) {

const [owner, repo] = repoDoc.fullName.split('/')

await createDefaultLabel({ github, owner, repo, name: config.label })
await createDefaultLabel({ installationId, owner, repo, name: config.label })

const registry = RegClient()
const registryGet = promisify(registry.get.bind(registry))
Expand Down Expand Up @@ -103,7 +99,7 @@ module.exports = async function ({ repositoryId }) {
.filter(Boolean)
.value()

const ghRepo = await github.repos.get({ owner, repo })
const ghRepo = await githubQueue(installationId).read(github => github.repos.get({ owner, repo }))

const branch = ghRepo.default_branch

Expand Down Expand Up @@ -174,7 +170,7 @@ module.exports = async function ({ repositoryId }) {
]

const sha = await createBranch({
github,
installationId,
owner,
repo,
branch,
Expand Down Expand Up @@ -221,10 +217,10 @@ module.exports = async function ({ repositoryId }) {
}
}

async function createDefaultLabel ({ github, name, owner, repo }) {
async function createDefaultLabel ({ installationId, name, owner, repo }) {
if (name !== false) {
try {
await githubQueue(() => github.issues.createLabel({
await githubQueue(installationId).write(github => github.issues.createLabel({
owner,
repo,
name,
Expand Down
23 changes: 10 additions & 13 deletions jobs/create-initial-pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ const statsd = require('../lib/statsd')
const getConfig = require('../lib/get-config')
const dbs = require('../lib/dbs')
const env = require('../lib/env')
const githubQueue = require('../lib/github-write-queue')
const GitHub = require('../lib/github')
const githubQueue = require('../lib/github-queue')
const { hasBilling } = require('../lib/payments')
const upsert = require('../lib/upsert')
const getToken = require('../lib/get-token')

const prContent = require('../content/initial-pr')

Expand All @@ -21,9 +19,6 @@ module.exports = async function (
const repositoryId = String(repository.id)
let repodoc = await repositories.get(repositoryId)
const config = getConfig(repodoc)
const { token } = await getToken(installationId)
const github = GitHub()
github.authenticate({ type: 'token', token })

const [owner, repo] = repodoc.fullName.split('/')
const {
Expand All @@ -50,7 +45,9 @@ module.exports = async function (
state: combined.state
})

await githubQueue(() => github.repos.createStatus({
const ghqueue = githubQueue(installationId)

await ghqueue.write(github => github.repos.createStatus({
owner,
repo,
sha,
Expand All @@ -62,7 +59,7 @@ module.exports = async function (

const accountHasBilling = await hasBilling(accountId)
if (repodoc.private && !accountHasBilling) {
await githubQueue(() => github.repos.createStatus({
await ghqueue.write(github => github.repos.createStatus({
owner,
repo,
sha,
Expand All @@ -73,7 +70,7 @@ module.exports = async function (
}))
}

const ghRepo = await github.repos.get({ owner, repo })
const ghRepo = await ghqueue.read(github => github.repos.get({ owner, repo }))

const secret = repodoc.private &&
crypto
Expand All @@ -90,7 +87,7 @@ module.exports = async function (
var {
id,
number
} = await githubQueue(() => github.pullRequests.create({
} = await ghqueue.write(github => github.pullRequests.create({
owner,
repo,
title: enabled
Expand All @@ -116,7 +113,7 @@ module.exports = async function (
statsd.increment('initial_pullrequests')

if (config.label !== false) {
await githubQueue(() => github.issues.addLabels({
await ghqueue.write(github => github.issues.addLabels({
owner,
repo,
number,
Expand All @@ -128,12 +125,12 @@ module.exports = async function (

// in case the pull request was already created
// we just store that PRs info
const pr = (await github.pullRequests.getAll({
const pr = (await ghqueue.read(github => github.pullRequests.getAll({
owner,
repo,
base,
head: `${owner}:${head}`
}))[0]
})))[0]

id = pr.id
number = pr.number
Expand Down
31 changes: 14 additions & 17 deletions jobs/create-version-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ const _ = require('lodash')
const jsonInPlace = require('json-in-place')
const semver = require('semver')

const GitHub = require('../lib/github')
const dbs = require('../lib/dbs')
const getToken = require('../lib/get-token')
const getConfig = require('../lib/get-config')
const getInfos = require('../lib/get-infos')
const getRangedVersion = require('../lib/get-ranged-version')
const createBranch = require('../lib/create-branch')
const statsd = require('../lib/statsd')
const env = require('../lib/env')
const githubQueue = require('../lib/github-write-queue')
const githubQueue = require('../lib/github-queue')
const upsert = require('../lib/upsert')
const { getActiveBilling } = require('../lib/payments')

Expand Down Expand Up @@ -52,10 +50,9 @@ module.exports = async function (
const [owner, repo] = repository.fullName.split('/')
const config = getConfig(repository)
if (_.includes(config.ignore, dependency)) return
const { token } = await getToken(installation.installation)
const github = GitHub()
github.authenticate({ type: 'token', token })
const { default_branch: base } = await github.repos.get({ owner, repo })
const installationId = installation.installation
const ghqueue = githubQueue(installationId)
const { default_branch: base } = await ghqueue.read(github => github.repos.get({ owner, repo }))

const newBranch = `${config.branchPrefix}${dependency}-${version}`

Expand Down Expand Up @@ -98,7 +95,7 @@ module.exports = async function (
}

const sha = await createBranch({
github,
installationId,
owner,
repo,
branch: base,
Expand Down Expand Up @@ -146,7 +143,7 @@ module.exports = async function (
: oldVersionResolved

const { dependencyLink, release, diffCommits } = await getInfos({
github,
installationId,
dependency,
version,
diffBase,
Expand All @@ -156,7 +153,7 @@ module.exports = async function (
const bodyDetails = _.compact(['\n', release, diffCommits]).join('\n')

if (openPR) {
await githubQueue(() => github.issues.createComment({
await ghqueue.write(github => github.issues.createComment({
owner,
repo,
number: openPR.number,
Expand Down Expand Up @@ -185,7 +182,7 @@ module.exports = async function (
})

// verify pull requests commit
await githubQueue(() => github.repos.createStatus({
await ghqueue.write(github => github.repos.createStatus({
sha,
owner,
repo,
Expand All @@ -196,7 +193,7 @@ module.exports = async function (
}))

const createdPr = await createPr({
github,
ghqueue,
title,
body,
base,
Expand All @@ -223,7 +220,7 @@ module.exports = async function (
})

if (config.label !== false) {
await githubQueue(() => github.issues.addLabels({
await ghqueue.write(github => github.issues.addLabels({
number: createdPr.number,
labels: [config.label],
owner,
Expand All @@ -232,9 +229,9 @@ module.exports = async function (
}
}

async function createPr ({ github, title, body, base, head, owner, repo }) {
async function createPr ({ ghqueue, title, body, base, head, owner, repo }) {
try {
return await githubQueue(() => github.pullRequests.create({
return await ghqueue.write(github => github.pullRequests.create({
title,
body,
base,
Expand All @@ -244,12 +241,12 @@ async function createPr ({ github, title, body, base, head, owner, repo }) {
}))
} catch (err) {
if (err.code !== 422) throw err
const allPrs = await github.pullRequests.getAll({
const allPrs = await ghqueue.read(github => github.pullRequests.getAll({
base,
head: owner + ':' + head,
owner,
repo
})
}))

if (allPrs.length > 0) return allPrs.shift()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
const _ = require('lodash')

const dbs = require('../../../lib/dbs')
const getToken = require('../../../lib/get-token')
const GitHub = require('../../../lib/github')
const GithubQueue = require('../../../lib/github-queue')
const statsd = require('../../../lib/statsd')

const { createDocs } = require('../../../lib/repository-docs')

module.exports = async function ({ installation, repositories_added }) {
const { repositories: reposDb } = await dbs()
if (!repositories_added.length) return
const { token } = await getToken(installation.id)

const github = GitHub()
github.authenticate({ type: 'token', token })

const repositories = await Promise.mapSeries(repositories_added, doc => {
const [owner, repo] = doc.full_name.split('/')
return github.repos.get({ owner, repo })
return GithubQueue(installation.id).read(github => github.repos.get({ owner, repo }))
})

statsd.increment('repositories', repositories.length)
Expand Down
9 changes: 2 additions & 7 deletions jobs/github-event/pull_request/closed.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const _ = require('lodash')

const GitHub = require('../../../lib/github')
const getToken = require('../../../lib/get-token')
const githubQueue = require('../../../lib/github-queue')
const dbs = require('../../../lib/dbs')
const upsert = require('../../../lib/upsert')
const githubQueue = require('../../../lib/github-write-queue')

module.exports = async function (data) {
const { repositories } = await dbs()
Expand All @@ -30,15 +28,12 @@ module.exports = async function (data) {
const accountId = repodoc.accountId

const [owner, repo] = repository.full_name.split('/')
const { token } = await getToken(installation.id)
const github = GitHub()
github.authenticate({ type: 'token', token })

repodoc = await upsert(repositories, String(repository.id), {
enabled: true
})
try {
await githubQueue(() => github.gitdata.deleteReference({
await githubQueue(installation.id).write(github => github.gitdata.deleteReference({
owner,
repo,
ref: `heads/${prdoc.head}`
Expand Down
11 changes: 2 additions & 9 deletions jobs/github-event/push.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const _ = require('lodash')

const getToken = require('../../lib/get-token')
const GitHub = require('../../lib/github')
const dbs = require('../../lib/dbs')
const { updateRepoDoc } = require('../../lib/repository-docs')
const updatedAt = require('../../lib/updated-at')
Expand Down Expand Up @@ -31,13 +29,8 @@ module.exports = async function (data) {
if (after === repodoc.headSha) return
repodoc.headSha = after

const { token } = await getToken(installation.id)

const github = GitHub()
github.authenticate({ type: 'token', token })

const oldPkg = _.get(repodoc, ['packages', 'package.json'])
await updateRepoDoc(github, repodoc)
await updateRepoDoc(installation.id, repodoc)
const pkg = _.get(repodoc, ['packages', 'package.json'])

if (!pkg) return disableRepo({ repositories, repository, repodoc })
Expand Down Expand Up @@ -80,7 +73,7 @@ module.exports = async function (data) {
await Promise.mapSeries(
branches,
deleteBranches.bind(null, {
github,
installationId: installation.id,
fullName: repository.full_name,
repositoryId
})
Expand Down
14 changes: 5 additions & 9 deletions jobs/github-event/status.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const _ = require('lodash')

const dbs = require('../../lib/dbs')
const GitHub = require('../../lib/github')
const getToken = require('../../lib/get-token')
const GithubQueue = require('../../lib/github-queue')
const handleBranchStatus = require('../../lib/handle-branch-status')

module.exports = async function ({ state, sha, repository, installation }) {
Expand All @@ -13,16 +12,13 @@ module.exports = async function ({ state, sha, repository, installation }) {

const [owner, repo] = repository.full_name.split('/')
const accountId = String(repository.owner.id)
const { token } = await getToken(installation.id)
const github = GitHub()
github.authenticate({ type: 'token', token })

const installationId = installation.id
// not a success or failure state
const combined = await github.repos.getCombinedStatus({
const combined = await GithubQueue(installationId).read(github => github.repos.getCombinedStatus({
owner,
repo,
ref: sha
})
}))
if (!_.includes(['success', 'failure'], combined.state)) return

const branchDoc = _.get(
Expand Down Expand Up @@ -54,7 +50,7 @@ module.exports = async function ({ state, sha, repository, installation }) {
}

await handleBranchStatus({
github,
installationId,
branchDoc,
accountId,
repository,
Expand Down
Loading

0 comments on commit 8dd3fc5

Please sign in to comment.