Skip to content

Commit cf02bbc

Browse files
bjohansebasljharb
authored andcommitted
feat: add sha option for testing and update schema to include branch
1 parent d1f563e commit cf02bbc

25 files changed

+267
-84
lines changed

USAGE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ against the patch branch wiby had created.
7474
Options:
7575
7676
--dependent URL of a dependent [string]
77+
--sha Commit or branch that was chosen for testing [string]
7778
--config Path to the configuration file. By default it will try to load
7879
the configuration from the first file it finds in the current
7980
working directory: `.wiby.json`, `.wiby.js` [string]
@@ -95,6 +96,7 @@ Options:
9596
--dependent URL of a dependent [string]
9697
--pull-request, --pr Raise a draft PR in addition to creating a branch
9798
[boolean]
99+
--sha Test against a specific commit or branch [string]
98100
--config Path to the configuration file. By default it will try
99101
to load the configuration from the first file it finds
100102
in the current working directory: `.wiby.json`,

bin/commands/result.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ exports.builder = (yargs) => yargs
1010
type: 'string',
1111
conflicts: 'config'
1212
})
13+
.option('sha', {
14+
desc: 'Commit or branch that was chosen for testing',
15+
type: 'string'
16+
})
1317
.option('config', {
1418
desc: 'Path to the configuration file. By default it will try to load the configuration from the first file it finds in the current working directory: `.wiby.json`, `.wiby.js`',
1519
type: 'string'
@@ -18,7 +22,7 @@ exports.builder = (yargs) => yargs
1822
exports.handler = async (params) => {
1923
const config = params.dependent
2024
? {
21-
dependents: [{ repository: params.dependent }]
25+
dependents: [{ repository: params.dependent, sha: params.sha || 'HEAD' }]
2226
}
2327
: wiby.validate({ config: params.config })
2428

bin/commands/test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ exports.builder = (yargs) => yargs
1616
type: 'boolean',
1717
conflicts: 'config'
1818
})
19+
.option('sha', {
20+
desc: 'Test against a specific commit or branch',
21+
type: 'string'
22+
})
1923
.option('config', {
2024
desc: 'Path to the configuration file. By default it will try to load the configuration from the first file it finds in the current working directory: `.wiby.json`, `.wiby.js`',
2125
type: 'string'
@@ -24,7 +28,7 @@ exports.builder = (yargs) => yargs
2428
exports.handler = (params) => {
2529
const config = params.dependent
2630
? {
27-
dependents: [{ repository: params.dependent, pullRequest: !!params['pull-request'] }]
31+
dependents: [{ repository: params.dependent, pullRequest: !!params['pull-request'], sha: params.sha || 'HEAD' }]
2832
}
2933
: wiby.validate({ config: params.config })
3034

docs/DRAFT-FLOW.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- `wiby` would also be able to run on a specific commit.
99
- [ ] Missing branch commit handling: [issue #46](https://github.com/pkgjs/wiby/issues/46)
1010
1. Indicate that you want to start your dependent tests by running `wiby test` while on the commit/branch/tag you wish to test.
11-
- [ ] It's also possible to specify this using `--commit=sha`: [issue #49](https://github.com/pkgjs/wiby/issues/49)
11+
- [x] It's also possible to specify this using `--sha=hash`: [issue #49](https://github.com/pkgjs/wiby/issues/49)
1212
- [ ] `wiby` will look for an existing log file for this HEAD, and error if it finds one. This will stop `wiby test` being run multiple times. You can override this check with `wiby test --overwrite`: [issue #50](https://github.com/pkgjs/wiby/issues/50)
1313
- [x] You can also run `--dependent` to specify a single dependent to test
1414
1. By default, `wiby` will look for your defined list of dependents to test against in `wiby.json`.

lib/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const dependentSchema = joi.object({
1414
'git+https'
1515
]
1616
}),
17-
pullRequest: joi.boolean().optional()
17+
pullRequest: joi.boolean().optional(),
18+
sha: joi.string().optional().default('HEAD')
1819
}).unknown(false)
1920

2021
exports.schema = joi.object({

lib/github.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,27 @@ const graphqlWithAuth = graphql.defaults({
1515
}
1616
})
1717

18-
module.exports.getPackageJson = async function getPackageJson (owner, repo) {
18+
module.exports.getPackageJson = async function getPackageJson (owner, repo, sha) {
1919
try {
2020
const resp = await graphqlWithAuth({
21-
query: queries.getPackageJson,
22-
owner: owner,
23-
repo: repo
21+
query: queries.getFiles,
22+
owner,
23+
repo,
24+
sha
2425
})
25-
return (JSON.parse(resp.repository.object.text))
26+
27+
const files = resp.repository.object.tree.entries
28+
29+
const packageJson = files != null ? files.find(file => file.name === 'package.json') : undefined
30+
31+
if (packageJson == null) {
32+
throw new Error('Could not find the package.json in the repository')
33+
}
34+
35+
return (JSON.parse(packageJson.object.text))
2636
} catch (err) {
2737
if (err.status === 404) {
28-
throw Error(`Could not find GitHub repository at https://www.github.com/${owner}/${repo}`)
38+
throw new Error(`Could not find GitHub repository at https://www.github.com/${owner}/${repo}`)
2939
} else {
3040
throw err
3141
}
@@ -51,10 +61,11 @@ module.exports.getDefaultBranch = async function (owner, repo) {
5161
return resp.repository.defaultBranchRef.name
5262
}
5363

54-
module.exports.getShas = async function getShas (owner, repo) {
64+
module.exports.getShas = async function getShas (owner, repo, sha) {
5565
const resp = await octokit.repos.listCommits({
5666
owner,
5767
repo,
68+
sha,
5869
per_page: 1
5970
})
6071
const headSha = resp.data[0].sha

lib/graphql/getFiles.graphql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
query($owner: String!, $repo: String!, $sha: String!) {
2+
repository(name: $repo, owner: $owner) {
3+
object(expression: $sha) {
4+
... on Commit {
5+
tree {
6+
entries {
7+
name
8+
object {
9+
... on Blob {
10+
text
11+
}
12+
}
13+
}
14+
}
15+
}
16+
}
17+
}
18+
}

lib/graphql/getPackageJson.graphql

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/graphql/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const fs = require('fs')
44
const path = require('path')
55

6-
exports.getPackageJson = fs.readFileSync(path.join(__dirname, 'getPackageJson.graphql')).toString()
6+
exports.getFiles = `${fs.readFileSync(path.join(__dirname, 'getFiles.graphql'))}`
77

8-
exports.getWibyBranches = fs.readFileSync(path.join(__dirname, 'getWibyBranches.graphql')).toString()
8+
exports.getWibyBranches = `${fs.readFileSync(path.join(__dirname, 'getWibyBranches.graphql'))}`
99

10-
exports.getDefaultBranch = fs.readFileSync(path.join(__dirname, 'getDefaultBranch.graphql')).toString()
10+
exports.getDefaultBranch = `${fs.readFileSync(path.join(__dirname, 'getDefaultBranch.graphql'))}`

lib/result.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ module.exports = async function ({ dependents }) {
2828
const allDependentsChecks = []
2929

3030
debug(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`)
31-
32-
for (const { repository: url } of dependents) {
31+
for (const { repository: url, sha } of dependents) {
3332
const dependentPkgInfo = gitURLParse(url)
34-
const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name)
33+
const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name, sha)
3534
debug(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
3635

3736
if (!context.checkDependentUsesParent(parentPkgJSON.name, dependentPkgJSON)) {

0 commit comments

Comments
 (0)