Skip to content

Commit

Permalink
feat(create-initial-subgroup-branch): add job
Browse files Browse the repository at this point in the history
  • Loading branch information
Realtin authored and hulkoba committed Mar 26, 2018
1 parent f427ee5 commit 16a4714
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 95 deletions.
105 changes: 31 additions & 74 deletions jobs/create-initial-subgroup-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ const getConfig = require('../lib/get-config')
const createBranch = require('../lib/create-branch')
const { updateRepoDoc } = require('../lib/repository-docs')
const githubQueue = require('../lib/github-queue')
const { maybeUpdatePaymentsJob } = require('../lib/payments')
const upsert = require('../lib/upsert')

const registryUrl = env.NPM_REGISTRY

// if we update dependencies find open PRs for that dependency and close the PRs by commit message

module.exports = async function ({ repositoryId, groupname }) {
module.exports = async function ({ repositoryId, groupName }) {
const { installations, repositories, logs } = await dbs()
const repoDoc = await repositories.get(repositoryId)
const accountId = repoDoc.accountId
Expand All @@ -28,42 +27,36 @@ module.exports = async function ({ repositoryId, groupname }) {

log.info('started')

if (repoDoc.fork && !repoDoc.hasIssues) { // we should allways check if issues are disabled and exit
log.warn('exited: Issues disabled on fork')
return
}

await updateRepoDoc(installationId, repoDoc)

// Object.keys(repoDoc.packages).length > 0
if (!_.get(repoDoc, ['packages', 'package.json'])) {
log.warn('exited: No packages and package.json found')
const config = getConfig(repoDoc)
const pathsForGroup = config.groups[groupName].packages
if (_.isEmpty(pathsForGroup)) {
log.warn(`exited: No packages and package.json found for group: ${groupName}`)
return
}
await upsert(repositories, repoDoc._id, repoDoc)

const config = getConfig(repoDoc)
if (config.disabled) {
log.warn('exited: Greenkeeper is disabled for this repo in package.json')
const packageJsonFiles = _.get(repoDoc, ['packages'])
if (_.isEmpty(packageJsonFiles)) {
log.warn(`exited: No package.json files found`)
return
}
const pkg = _.get(repoDoc, ['packages', 'package.json']) // this is duplicated code (merge with L44)
if (!pkg) return
await upsert(repositories, repoDoc._id, repoDoc)

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

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

const registry = RegClient()
const registryGet = promisify(registry.get.bind(registry))
// get for all package.jsons in a group
// every package should be updated to the newest version
const dependencyMeta = _.flatten(
['dependencies', 'devDependencies', 'optionalDependencies'].map(type => {
return _.map(pkg[type], (version, name) => ({ name, version, type }))
})
)
log.info('dependencies found', {parsedDependencies: dependencyMeta, packageJson: pkg})
const dependencyMeta = _.uniqWith(_.flatten(pathsForGroup.map(path => {
return _.flatten(
['dependencies', 'devDependencies', 'optionalDependencies'].map(type => {
return _.map(packageJsonFiles[path][type], (version, name) => ({ name, version, type }))
})
)
})), _.isEqual)

log.info('dependencies found', {parsedDependencies: dependencyMeta, packageJsonFiles: packageJsonFiles})
let dependencies = await Promise.mapSeries(dependencyMeta, async dep => {
try {
dep.data = await registryGet(registryUrl + dep.name.replace('/', '%2F'), {
Expand All @@ -78,7 +71,10 @@ module.exports = async function ({ repositoryId, groupname }) {
.filter(Boolean)
.map(dependency => {
let latest = _.get(dependency, 'data.dist-tags.latest')
if (_.includes(config.ignore, dependency.name)) {
if (
_.includes(config.ignore, dependency.name) ||
_.includes(config.groups[groupName].ignore, dependency.name)
) {
dependencyActionsLog[dependency.name] = 'ignored in config'
return
}
Expand Down Expand Up @@ -124,21 +120,19 @@ module.exports = async function ({ repositoryId, groupname }) {
})
.filter(Boolean)
.value()

log.info('parsed dependency actions', {dependencyActionsLog})

const ghRepo = await githubQueue(installationId).read(github => github.repos.get({ owner, repo })) // wrap in try/catch
log.info('github: repository info', {repositoryInfo: ghRepo})

const branch = ghRepo.default_branch

const newBranch = config.branchPrefix + 'initial' + `-${groupname}`
const newBranch = config.branchPrefix + 'initial' + `-${groupName}`

let badgeAlreadyAdded = false
// create a transform loop for all the package.json paths and push into the transforms array below
const transforms = [
{
path: 'package.json',
const transforms = pathsForGroup.map(path => {
return {
path,
message: 'chore(package): update dependencies',
transform: oldPkg => {
const oldPkgParsed = JSON.parse(oldPkg)
Expand All @@ -152,7 +146,7 @@ module.exports = async function ({ repositoryId, groupname }) {
return inplace.toString()
}
}
]
})

const sha = await createBranch({ // try/catch
installationId,
Expand All @@ -163,55 +157,18 @@ module.exports = async function ({ repositoryId, groupname }) {
transforms
})

if (!sha) {
// When there are no changes and the badge already exists we can enable right away
if (badgeAlreadyAdded) {
await upsert(repositories, repoDoc._id, { enabled: true })
log.info('Repository silently enabled')
return maybeUpdatePaymentsJob(accountId, repoDoc.private)
} else {
log.error('Could not create initial branch')
throw new Error('Could not create initial branch')
}
}

const depsUpdated = transforms[0].created
const travisModified = false
const badgeAdded = false
const depsUpdated = _.some(transforms, 'created')
if (!depsUpdated) return

await upsert(repositories, `${repositoryId}:branch:${sha}`, {
type: 'branch',
initial: true,
initial: false, // other flag?
sha,
base: branch,
head: newBranch,
processed: false,
depsUpdated,
travisModified,
badgeAdded
depsUpdated
})

log.success('success')

return {
delay: 30 * 60 * 1000,
data: {
name: 'initial-timeout-pr',
repositoryId,
accountId
}
}
}

async function createDefaultLabel ({ installationId, name, owner, repo }) {
if (name !== false) {
try {
await githubQueue(installationId).write(github => github.issues.createLabel({
owner,
repo,
name,
color: '00c775'
}))
} catch (e) {}
}
}
11 changes: 0 additions & 11 deletions jobs/github-event/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ module.exports = async function (data) {

await updateDoc(repositories, repository, repoDoc)

console.log('oldPkg', oldPkg)
console.log('pkg', pkg)

if (!oldPkg) {
return {
data: {
Expand All @@ -85,14 +82,10 @@ module.exports = async function (data) {
// TODO: for Tuesday -> deleting a package.json needs to be detected!!
const branches = await getDependencyBranchesForAllGroups({pkg, oldPkg, config, repositories, repositoryId})
const configChanges = diffGreenkeeperJson(config, repoDoc.greenkeeper)
console.log('configChanges', configChanges)
console.log('dependencyChanges', branches)

const groupBranchesToDelete = await getGroupBranchesToDelete({configChanges, repositories, repositoryId})
console.log('branches in push', branches)
const allBranchesToDelete = branches.concat(groupBranchesToDelete)
const _branches = _.uniqWith(_.flattenDeep(allBranchesToDelete), _.isEqual)
console.log('allBranchesToDelete flattend&uniq', _branches)

await Promise.mapSeries(
_branches,
Expand All @@ -109,9 +102,7 @@ module.exports = async function (data) {
return true
}
})
console.log('relevantModifiedGroups', relevantModifiedGroups)
const groupsToRecvieveInitialBranch = configChanges.added.concat(relevantModifiedGroups)
console.log('groupsToRecvieveInitialBranch', groupsToRecvieveInitialBranch)
if (_.isEmpty(groupsToRecvieveInitialBranch)) return
// create subgroup initial pr
return _(groupsToRecvieveInitialBranch)
Expand Down Expand Up @@ -160,7 +151,6 @@ function hasRelevantChanges (commits, files) {
}

async function disableRepo ({ repositories, repoDoc, repository }) {
// console.log('disableRepo')
repoDoc.enabled = false
await updateDoc(repositories, repository, repoDoc)
if (!env.IS_ENTERPRISE) {
Expand All @@ -180,7 +170,6 @@ async function getDependencyBranchesForAllGroups ({pkg, oldPkg, config, reposito
}
// this can only happen if a package.json was modified
const dependencyDiff = diff(oldPkg[path], pkg[path], groupName)
console.log('dependencyDiff', dependencyDiff)
if (!_.isEmpty(dependencyDiff)) {
return getDependencyBranchesToDelete({changes: dependencyDiff, repositories, repositoryId})
}
Expand Down
6 changes: 0 additions & 6 deletions lib/branches-to-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = {
}

function getDependencyChanges (changes) {
console.log('changes in getDependencyChanges', changes)
let dependencyChanges = []

_.each(changes, (type, dependencyType) => {
Expand Down Expand Up @@ -37,7 +36,6 @@ async function getDependencyBranchesToDelete ({changes, repositories, repository
}

async function getSingleDependencyBranchesToDelete ({changes, repositories, repositoryId}) {
console.log('changes in getSingleDependencyBranchesToDelete', changes)
const { change, after, dependency, dependencyType, groupName } = changes

let branches = []
Expand All @@ -52,7 +50,6 @@ async function getSingleDependencyBranchesToDelete ({changes, repositories, repo
})).rows,
'doc'
)
console.log('branches in getSingleDependencyBranchesToDelete', branches)
return _(branches)
.filter(
branch =>
Expand All @@ -68,7 +65,6 @@ async function getSingleDependencyBranchesToDelete ({changes, repositories, repo
// if groupName is passed in, only include branches of that group
// branch.head = 'greenkeeper/${groupName}/${dependency}'
if (groupName) {
console.log('has groupName', groupName)
return branch.head.includes(`greenkeeper/${groupName}/`)
} else {
// If there's no groupName, only return branches that don’t belong to groups
Expand All @@ -82,9 +78,7 @@ async function getGroupBranchesToDelete ({configChanges, repositories, repositor
if (configChanges.removed.length || configChanges.modified.length) {
const groups = _.uniq(configChanges.removed.concat(configChanges.modified))
// delete all branches for those groups
console.log('groups', groups)
return Promise.all(_.map(groups, async (group) => {
console.log('map über group', group)
return Promise.all(_.map(
(await repositories.query('branch_by_group', {
key: [repositoryId, group],
Expand Down
1 change: 0 additions & 1 deletion lib/delete-branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = async function (
) {
const { repositories } = await dbs()
const [owner, repo] = fullName.split('/')
console.log('branch to be deleted', branch)
if (!branch) return
let referenceDeleted = false
try {
Expand Down
Loading

0 comments on commit 16a4714

Please sign in to comment.