Skip to content

Commit

Permalink
Merge pull request #82 from dominykas/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas committed Mar 9, 2021
2 parents 075c33e + ea46480 commit 5b031ee
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 276 deletions.
5 changes: 3 additions & 2 deletions lib/clean.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const context = require('./context')
const github = require('./github')
const gitURLParse = require('git-url-parse')
const logger = require('./logger')
Expand All @@ -12,7 +13,7 @@ module.exports = async ({ dependents }, { all, dryRun }) => {
// enable log output for clean command without DEBUG env
logger.enableLogs(cleanCommandNamespace)

const parentPkgJSON = await require('./test').getLocalPackageJSON()
const parentPkgJSON = await context.getLocalPackageJSON()
const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url)
debug(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`)

Expand All @@ -23,7 +24,7 @@ module.exports = async ({ dependents }, { all, dryRun }) => {

let branches

const branch = await require('./result').getBranchName(parentPkgJSON.name)
const branch = await context.getTestingBranchName(parentPkgJSON.name)

if (all) {
branches = await github.getWibyBranches(dependentPkgInfo.owner, dependentPkgInfo.name)
Expand Down
4 changes: 3 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const joi = require('joi')
const path = require('path')
const debug = require('./logger')('wiby:config')
const logger = require('./logger')

const debug = logger('wiby:config')

const dependentSchema = joi.object({
repository: joi.string().uri({
Expand Down
16 changes: 16 additions & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const fsPromises = require('fs').promises

exports.getTestingBranchName = async function getTestingBranchName (parentPackageName) {
return `wiby-${parentPackageName}`
}

exports.getLocalPackageJSON = async function getLocalPackageJSON () {
const pkg = await fsPromises.readFile('package.json')
return JSON.parse(pkg)
}

exports.checkPackageInPackageJSON = function checkPackageInPackageJSON (dep, packageJSON) {
return Object.prototype.hasOwnProperty.call(packageJSON.dependencies, dep)
}
16 changes: 7 additions & 9 deletions lib/result.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict'

const test = require('./test')
const context = require('./context')
const github = require('./github')
const gitURLParse = require('git-url-parse')
const debug = require('./logger')('wiby:result')
const logger = require('./logger')

const debug = logger('wiby:result')

// enum containing possible pipeline checks statuses
const pipelineStatusesEnum = module.exports.pipelineStatusesEnum = Object.freeze({
Expand All @@ -16,7 +18,7 @@ const pipelineStatusesEnum = module.exports.pipelineStatusesEnum = Object.freeze
const PENDING_RESULT_EXIT_CODE = 64

module.exports = async function ({ dependents }) {
const parentPkgJSON = await test.getLocalPackageJSON()
const parentPkgJSON = await context.getLocalPackageJSON()
const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url)
const output = { status: pipelineStatusesEnum.PENDING, results: [] }
const allDependentsChecks = []
Expand All @@ -28,11 +30,11 @@ module.exports = async function ({ dependents }) {
const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name)
debug(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)

if (!test.checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) {
if (!context.checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) {
throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
}

const branch = await getBranchName(parentPkgJSON.name)
const branch = await context.getTestingBranchName(parentPkgJSON.name)
let resp = await github.getChecks(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
if (resp.data.check_runs.length === 0) {
resp = await github.getCommitStatusesForRef(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
Expand Down Expand Up @@ -62,10 +64,6 @@ module.exports = async function ({ dependents }) {
return output
}

const getBranchName = module.exports.getBranchName = async function getBranchName (dep) {
return `wiby-${dep}`
}

const getResultForEachRun = module.exports.getResultForEachRun = function getResultForEachRun (runs) {
return runs.map((check) => {
if (check.status === pipelineStatusesEnum.QUEUED) {
Expand Down
21 changes: 5 additions & 16 deletions lib/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const fsPromises = require('fs').promises
const context = require('./context')
const github = require('./github')
const gitURLParse = require('git-url-parse')
const logger = require('./logger')
Expand All @@ -13,7 +13,7 @@ module.exports = async function ({ dependents }) {
// enable log output for test command without DEBUG env
logger.enableLogs(testCommandNamespace)

const parentPkgJSON = await getLocalPackageJSON()
const parentPkgJSON = await context.getLocalPackageJSON()
const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url)
debug(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`)

Expand All @@ -25,7 +25,7 @@ module.exports = async function ({ dependents }) {
const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name)
debug(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)

if (!checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) {
if (!context.checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) {
throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
}

Expand All @@ -39,27 +39,16 @@ const getCommitHash = async function getCommitHash (owner, repo) {
return headSha
}

const checkPackageInPackageJSON = module.exports.checkPackageInPackageJSON = function checkPackageInPackageJSON (dep, packageJSON) {
return Object.prototype.hasOwnProperty.call(packageJSON.dependencies, dep)
}

const getCommitURL = async function getCommitURL (owner, repo, hash) {
hash = hash || await getCommitHash(owner, repo)

const patch = `${owner}/${repo}#${hash}`
return patch
}

const getLocalPackageJSON = module.exports.getLocalPackageJSON =
async function getLocalPackageJSON (pkgPath) {
pkgPath = pkgPath || 'package.json'
const pkg = await fsPromises.readFile(pkgPath)
return JSON.parse(pkg)
}

const applyPatch = module.exports.applyPatch =
function applyPatch (patch, module, packageJSON) {
if (!checkPackageInPackageJSON(module, packageJSON)) {
if (!context.checkPackageInPackageJSON(module, packageJSON)) {
throw new Error('Dependency not found in package.json')
}
packageJSON.dependencies[module] = patch
Expand All @@ -70,7 +59,7 @@ async function pushPatch (packageJSON, owner, repo, dep) {
const file = JSON.stringify(packageJSON, null, 2) + '\n' // assumes package.json is using two spaces
const encodedFile = Buffer.from(file).toString('base64')
const message = `wiby: update ${dep}`
const branch = await require('./result').getBranchName(dep)
const branch = await context.getTestingBranchName(dep)

const [headSha, treeSha] = await github.getShas(owner, repo)
const blobSha = await github.createBlob(owner, repo, encodedFile)
Expand Down
210 changes: 0 additions & 210 deletions test/cli.js

This file was deleted.

Loading

0 comments on commit 5b031ee

Please sign in to comment.