Skip to content
This repository was archived by the owner on May 20, 2020. It is now read-only.

Commit 9691eb6

Browse files
author
Adam Braimbridge
authored
Merge pull request #48 from Financial-Times/adambraimbridge/pull-requests
Commands for GitHub pull requests
2 parents b461c0c + e480aa0 commit 9691eb6

34 files changed

+1581
-813
lines changed

.huskyrc.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"hooks": {
3-
"pre-commit": "npm run lint"
4-
}
2+
"hooks": {
3+
"pre-commit": "npm run lint",
4+
"pre-push": "npm run test"
5+
}
56
}

README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ const github = require("@financial-times/github")({
4444
});
4545
```
4646

47-
See [`examples/examples.js`](https://github.com/Financial-Times/github/blob/master/examples/examples.js) for a full set of usage examples.
47+
# Developers
4848

49-
See [`src/lib/github.js`](https://github.com/Financial-Times/github/blob/master/src/lib/github.js) for all available methods.
49+
## Command hierarchy and directory structure
50+
51+
This project follows the example provided in the Yargs documentation for command hierarchy and directory structure.
52+
53+
- @see: https://github.com/yargs/yargs/blob/master/docs/advanced.md#commanddirdirectory-opts
54+
- @see: [`src/commands`](https://github.com/Financial-Times/github/blob/master/src/commands/) for all available commands.
55+
56+
## Conventions: CamelCase and under_scores
57+
58+
All commands and variables are consistent with the Ocktokit and GitHub APIs documentation, respectively:
59+
60+
- https://octokit.github.io/rest.js/#api-Pulls <-- Instead of camelCaseFormat, we use hyphen-format.
61+
- https://developer.github.com/v3/pulls <-- Variables are under_score_format.

__mocks__/@octokit/fixtures/create.json

-500
This file was deleted.

__mocks__/@octokit/fixtures/createCard.json

-32
This file was deleted.

__mocks__/@octokit/fixtures/createColumn.json

-10
This file was deleted.

__mocks__/@octokit/fixtures/createForOrg.json

-34
This file was deleted.

__mocks__/@octokit/rest.js

-20
This file was deleted.

jest.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ module.exports = {
1212
outputName: "results.xml"
1313
}
1414
]
15-
]
15+
],
16+
verbose: true,
1617
};

lib/common-yargs.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* These descriptions are exported so that they can be used to group global options in usage output.
3+
*/
4+
const descriptions = {
5+
token: "GitHub personal access token (uses `GITHUB_PERSONAL_ACCESS_TOKEN` environment variable by default). Generate one at https://github.com/settings/tokens.",
6+
json: "Format command output as JSON string",
7+
}
8+
9+
const withToken = yargs => yargs.option("token", {
10+
11+
// IMPORTANT: We use a function here so the token is not output on the command line.
12+
default: () => process.env.GITHUB_PERSONAL_ACCESS_TOKEN,
13+
describe: descriptions.token,
14+
type: "string",
15+
coerce: value => {
16+
if (!value) {
17+
throw new Error(
18+
"Error: `--token` option is required as a valid GITHUB_PERSONAL_ACCESS_TOKEN environment variable does not exist"
19+
)
20+
}
21+
return value
22+
}
23+
})
24+
25+
const withJson = yargs => yargs.option("json", {
26+
describe: descriptions.json,
27+
type: "boolean",
28+
})
29+
30+
const withBase = yargs => yargs.option("base", {
31+
describe: "Base branch",
32+
type: "string",
33+
default: "master",
34+
})
35+
36+
const withOwner = yargs => yargs.option("owner", {
37+
alias: "o",
38+
describe: "Owner",
39+
type: "string",
40+
demandOption: true,
41+
})
42+
43+
const withRepo = yargs => yargs.option("repo", {
44+
alias: "r",
45+
describe: "Repository",
46+
type: "string",
47+
demandOption: true,
48+
})
49+
50+
const withNumber = yargs => yargs.option("number", {
51+
alias: "n",
52+
describe: "Number",
53+
type: "integer",
54+
demandOption: true,
55+
})
56+
57+
const withReviewers = yargs => yargs.option("reviewers", {
58+
describe: "Reviewers",
59+
type: "string",
60+
})
61+
62+
const withTeamReviewers = yargs => yargs.option("team_reviewers", {
63+
describe: "Team reviewers",
64+
type: "string",
65+
})
66+
67+
const withBody = yargs => yargs.option("body", {
68+
describe: "Path to pull request body",
69+
type: "string",
70+
})
71+
72+
const withTitle = yargs => yargs.option("title", {
73+
alias: "t",
74+
describe: "Pull request title",
75+
type: "string",
76+
})
77+
78+
module.exports = {
79+
descriptions,
80+
withToken,
81+
withJson,
82+
withBase,
83+
withOwner,
84+
withRepo,
85+
withNumber,
86+
withReviewers,
87+
withTeamReviewers,
88+
withBody,
89+
withTitle,
90+
}

lib/octokit.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const Octokit = require('@octokit/rest')
2+
3+
module.exports = async ({ personalAccessToken }) => {
4+
if (!personalAccessToken) {
5+
throw new Error(
6+
'github tooling helper: Missing personalAccessToken option - https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/'
7+
)
8+
}
9+
10+
/**
11+
* Return an authenticated instance of Octokit
12+
*/
13+
try {
14+
return await new Octokit({
15+
previews: [
16+
/**
17+
* Access Projects API while it is under preview
18+
*
19+
* @see https://developer.github.com/v3/projects
20+
*/
21+
'inertia-preview',
22+
],
23+
/**
24+
* Authenticate GitHub API calls using GitHub personal access token
25+
*
26+
* @see https://github.com/octokit/rest.js#authentication
27+
*/
28+
auth: `token ${personalAccessToken}`,
29+
30+
// Todo: Allow emojis. @see https://developer.github.com/v3/pulls/review_requests/#create-a-review-request
31+
// header: 'application / vnd.github.symmetra - preview + json',
32+
})
33+
}
34+
catch (error) { throw new Error(error) }
35+
}

lib/print-output.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-disable no-console */
2+
3+
/**
4+
* Helper for formatting and printing GitHub resources.
5+
*
6+
* @param {object} options
7+
* @param {string} options.json
8+
* @param {object} options.resource
9+
*/
10+
module.exports = function printOutput({ json, resource, error }) {
11+
if (error) {
12+
console.log(`⚠️ Error response from GitHub. ${JSON.stringify(error.errors)}`)
13+
}
14+
else if (json === true) {
15+
const jsonOutput = JSON.stringify({
16+
type: "resource",
17+
resource
18+
});
19+
console.log(jsonOutput);
20+
} else {
21+
console.log(resource.html_url || "OK");
22+
}
23+
};

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"eslint-plugin-no-only-tests": "^2.1.0",
3131
"husky": "^1.3.1",
3232
"jest": "^24.1.0",
33-
"jest-junit": "^6.3.0"
33+
"jest-junit": "^6.3.0",
34+
"nock": "^10.0.6"
3435
},
3536
"engines": {
3637
"node": "^8.15.1 || ^10.15.3"

0 commit comments

Comments
 (0)