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

Random refactors #228

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions ci-services/circleci.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const _ = require('lodash')
const gitHelpers = require('../lib/git-helpers')

const env = process.env
Expand All @@ -18,7 +17,7 @@ module.exports = {
repoSlug: `${env.CIRCLE_PROJECT_USERNAME}/${env.CIRCLE_PROJECT_REPONAME}`,
branchName: env.CIRCLE_BRANCH,
// update, CIRCLE_PREVIOUS_BUILD_NUM is only null on the first job of the first workflow on the branch
correctBuild: _.isEmpty(env.CI_PULL_REQUEST) && !env.CIRCLE_PREVIOUS_BUILD_NUM,
correctBuild: !env.CI_PULL_REQUEST && !env.CIRCLE_PREVIOUS_BUILD_NUM,
// upload when last commit is lockfile update
uploadBuild: env.CIRCLE_NODE_INDEX === `${env.BUILD_LEADER_ID || 0}` && isLockfileUpdate()
}
11 changes: 5 additions & 6 deletions ci-services/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict'

const _ = require('lodash')

const tests = require('./tests')

module.exports = () => {
const service = _.findKey(tests, test => test())

if (!service) throw new Error('Could not detect a CI Service.')
for (const service in tests) {
const test = tests[service]
if (test()) return require(`./${service}`)
}

return require(`./${service}`)
throw new Error('Could not detect a CI Service.')
}
4 changes: 1 addition & 3 deletions ci-services/semaphoreci.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

const _ = require('lodash')

const env = process.env

module.exports = {
repoSlug: env.SEMAPHORE_REPO_SLUG,
branchName: env.BRANCH_NAME,
correctBuild: _.isEmpty(env.PULL_REQUEST_NUMBER),
correctBuild: !env.PULL_REQUEST_NUMBER,
uploadBuild: env.SEMAPHORE_CURRENT_JOB === '1'
}
9 changes: 4 additions & 5 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict'

const _ = require('lodash')
const relative = require('require-relative')

const pkg = relative('./package.json')
const { greenkeeper = {} } = relative('./package.json')

module.exports = _.defaults(pkg.greenkeeper, {
branchPrefix: 'greenkeeper/'
})
if (typeof greenkeeper.branchPrefix === 'undefined') greenkeeper.branchPrefix = 'greenkeeper/'

module.exports = greenkeeper
23 changes: 12 additions & 11 deletions lib/git-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
const url = require('url')
const exec = require('child_process').execSync

const _ = require('lodash')
const DEBUG = process.env.GK_LOCK_DEBUG || false
const env = process.env

const DEBUG = env.GK_LOCK_DEBUG || false

function setupRemote (info) {
let remote = `[email protected]:${info.repoSlug}`
if (info.gitUrl) remote = info.gitUrl

if (process.env.GH_TOKEN) {
if (env.GH_TOKEN) {
if (remote.slice(0, 5) !== 'https') remote = `https://github.com/${info.repoSlug}`
const urlParsed = url.parse(remote)
urlParsed.auth = process.env.GH_TOKEN
urlParsed.auth = env.GH_TOKEN
remote = url.format(urlParsed)
}
exec(`git remote add gk-origin ${remote} || git remote set-url gk-origin ${remote}`)
Expand All @@ -23,11 +24,11 @@ module.exports = {
getNumberOfCommitsOnBranch: function getNumberOfCommitsOnBranch (branch) {
const refArgument = `$(git for-each-ref '--format=%(refname)' refs/ | grep /${branch} | head -1)`
const notArgument = `$(git for-each-ref '--format=%(refname)' refs/ | grep -v /${branch})`
return _.toNumber(
return parseInt(
exec(
`git log ${refArgument} --oneline --not ${notArgument} | wc -l`
).toString()
)
).toString(),
10)
},
getLastCommitMessage: function getLastCommitMessage () {
return exec('git log --format=%B -1').toString()
Expand All @@ -36,15 +37,15 @@ module.exports = {
return exec('git config remote.origin.url').toString().trim()
},
getRepoSlug: function getRepoSlug (githubUrl) {
var ghRegex = /\S+[:|/](\w+(?:[-]\w+)*)\/(\w+(?:[-]\w+)*)/g
var parsed = ghRegex.exec(githubUrl)
const ghRegex = /\S+[:|/](\w+(?:[-]\w+)*)\/(\w+(?:[-]\w+)*)/g
const parsed = ghRegex.exec(githubUrl)
return (
`${parsed[1]}/${parsed[2]}`
)
},
hasLockfileCommit: function hasLockfileCommit (info) {
setupRemote(info)
const defaultBranch = process.env.GK_LOCK_DEFAULT_BRANCH || 'master'
const defaultBranch = env.GK_LOCK_DEFAULT_BRANCH || 'master'
// CI clones are often shallow clones. Let’s make sure we have enough to work with
// https://stackoverflow.com/a/44036486/242298
// console.log(`git config --replace-all remote.origin.fetch +refs/heads/*:refs/remotes/origin/*`)
Expand Down Expand Up @@ -90,7 +91,7 @@ module.exports = {
return true
},
getBranch: function getBranch () {
var branch = exec('git show -s --pretty=%d HEAD')
const branch = exec('git show -s --pretty=%d HEAD')
.toString()
.match(/\(?([^)]+)\)?/)[1]
.split(', ')
Expand Down
13 changes: 7 additions & 6 deletions lib/update-lockfile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const exec = require('child_process').execSync
const _ = require('lodash')
const semver = require('semver')

const env = process.env

const flags = {
'dependencies': ' -S',
'devDependencies': ' -D',
Expand All @@ -22,7 +23,7 @@ module.exports.updateLockfile = function updateLockfile (dependency, options) {
} else {
if (options.yarn) {
const flag = yarnFlags[dependency.type]
const envArgs = process.env.GK_LOCK_YARN_OPTS ? ` ${process.env.GK_LOCK_YARN_OPTS.trim()}` : ''
const envArgs = env.GK_LOCK_YARN_OPTS ? ` ${env.GK_LOCK_YARN_OPTS.trim()}` : ''
const args = `${flag}${envArgs} '${dependency.name}@${dependency.range}'`
exec(`yarn add${args}`)
}
Expand All @@ -31,7 +32,7 @@ module.exports.updateLockfile = function updateLockfile (dependency, options) {
const flag = flags[dependency.type]
const prefix = dependency.prefix ? ` --save-prefix="${dependency.prefix}"` : ''
const args = `${flag}${prefix} ${dependency.name}@${dependency.version}`
var npmBin = 'npm'
let npmBin = 'npm'
try {
exec('npm5 -v')
npmBin = 'npm5'
Expand All @@ -53,9 +54,9 @@ module.exports.stageLockfile = function stageLockfile (options) {
}

module.exports.commitLockfiles = function commitLockfiles () {
const commitEmail = process.env.GK_LOCK_COMMIT_EMAIL ? process.env.GK_LOCK_COMMIT_EMAIL.trim() : '[email protected]'
const commitName = process.env.GK_LOCK_COMMIT_NAME ? process.env.GK_LOCK_COMMIT_NAME.trim() : 'greenkeeperio-bot'
const shouldAmend = !_.includes([undefined, `0`, 'false', 'null', 'undefined'], process.env.GK_LOCK_COMMIT_AMEND)
const commitEmail = env.GK_LOCK_COMMIT_EMAIL ? env.GK_LOCK_COMMIT_EMAIL.trim() : '[email protected]'
const commitName = env.GK_LOCK_COMMIT_NAME ? env.GK_LOCK_COMMIT_NAME.trim() : 'greenkeeperio-bot'
const shouldAmend = ![undefined, `0`, 'false', 'null', 'undefined'].includes(env.GK_LOCK_COMMIT_AMEND)

exec(`git config user.email "${commitEmail}"`)
exec(`git config user.name "${commitName}"`)
Expand Down
48 changes: 35 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"dependencies": {
"fast-glob": "^2.2.0",
"greenkeeper-monorepo-definitions": "^1.0.0",
"lodash": "^4.17.10",
"require-relative": "^0.8.7",
"semver": "^5.3.0"
},
Expand Down
Loading