Skip to content

Commit

Permalink
Add prettier (#7)
Browse files Browse the repository at this point in the history
* Add prettier (and run pre-commit)

* There seems to be a race condition with pre-commit prettier
  • Loading branch information
SteveShaffer committed Jun 1, 2019
1 parent 91a69c6 commit 8959f8c
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 41 deletions.
4 changes: 2 additions & 2 deletions commands/commit.js
Expand Up @@ -6,8 +6,8 @@ exports.builder = yargs => {
yargs.positional('message', {
type: 'string',
describe: 'the commit message',
default: '.' // TODO: Have a better default commit message
})
default: '.', // TODO: Have a better default commit message
});
};
exports.handler = async argv => {
const message = argv.message;
Expand Down
10 changes: 7 additions & 3 deletions commands/merge.js
Expand Up @@ -6,14 +6,18 @@ exports.desc = 'Squash and merge a GitHub pull request.';
exports.builder = yargs => {
yargs.positional('number', {
type: 'string',
describe: 'the pull request number'
})
describe: 'the pull request number',
});
};
exports.handler = async argv => {
const pullRequestNumber = argv.number;
console.log('merging the PR');
const repoInfo = await git.getCurrentRepoGithubInfo();
await github.mergePullRequest({repoOwner: repoInfo.owner, repoName: repoInfo.name, pullRequestNumber});
await github.mergePullRequest({
repoOwner: repoInfo.owner,
repoName: repoInfo.name,
pullRequestNumber,
});
console.log('merged');
// TODO: Delete the branch after merge (and maybe delete local branch too, and maybe switch to or detached head on master?)
};
18 changes: 13 additions & 5 deletions commands/pr.js
Expand Up @@ -2,21 +2,29 @@ const git = require('../git');
const github = require('../github');

exports.comomand = 'pr [title]';
exports.desc = 'Create a GitHub pull request from the current branch to master.';
exports.desc =
'Create a GitHub pull request from the current branch to master.';
exports.builder = yargs => {
yargs.positional('title', {
type: 'string',
describe: 'the pull request title',
default: 'Hey look it\'s a PR!'
})
default: "Hey look it's a PR!",
});
};
exports.handler = async argv => {
const title = argv.title;
console.log('creating PR');
const currentBranch = await git.getCurrentBranchName();
// TODO: commit & push if necessary
const repoInfo = await git.getCurrentRepoGithubInfo();
const repositoryId = await github.getRepositoryId({owner: repoInfo.owner, name: repoInfo.name});
const pullRequest = await github.createPullRequest({repositoryId, headBranch: currentBranch, title});
const repositoryId = await github.getRepositoryId({
owner: repoInfo.owner,
name: repoInfo.name,
});
const pullRequest = await github.createPullRequest({
repositoryId,
headBranch: currentBranch,
title,
});
console.log(pullRequest.url);
};
5 changes: 3 additions & 2 deletions commands/switch.js
Expand Up @@ -5,7 +5,8 @@ exports.desc = 'Switch to a branch.';
exports.builder = yargs => {
yargs.positional('branch', {
type: 'string',
describe: 'the branch name (in origin) that you want to switch to (i.e. checkout)'
describe:
'the branch name (in origin) that you want to switch to (i.e. checkout)',
// TODO: Make optional with default to master?
});
};
Expand All @@ -18,5 +19,5 @@ exports.handler = async argv => {
await git.fetch();
await git.detachHead();
await git.deleteLocalBranch(branchName);
await git.checkoutBranchFromOrigin(branchName)
await git.checkoutBranchFromOrigin(branchName);
};
8 changes: 4 additions & 4 deletions git.js
Expand Up @@ -9,7 +9,7 @@ module.exports = {
fetch: () => Git().fetch,
getCurrentBranchName,
getCurrentRepoGithubInfo,
pushCurrentBranchToOrigin
pushCurrentBranchToOrigin,
};

/**
Expand All @@ -20,7 +20,7 @@ module.exports = {
*/
async function checkoutBranchFromOrigin(branchName) {
const git = Git(); // TODO: Consolidate all this init stuff
let branchSummary = await git.branch({'-r': null});
let branchSummary = await git.branch({ '-r': null });
const remoteBranchName = `origin/${branchName}`;
if (branchSummary.branches[remoteBranchName]) {
console.log(`checking out branch from ${remoteBranchName}`);
Expand Down Expand Up @@ -106,7 +106,7 @@ async function getCurrentRepoGithubInfo() {
return {
owner,
name,
url
url,
};
}

Expand All @@ -117,5 +117,5 @@ async function getCurrentRepoGithubInfo() {
async function pushCurrentBranchToOrigin() {
const git = Git();
const currentBranch = await getCurrentBranchName();
await git.push('origin', currentBranch, {'-u': null});
await git.push('origin', currentBranch, { '-u': null });
}
50 changes: 30 additions & 20 deletions github.js
Expand Up @@ -8,7 +8,7 @@ const GITHUB_USERNAME = 'steveshaffer';
module.exports = {
createPullRequest,
getRepositoryId,
mergePullRequest
mergePullRequest,
};

/**
Expand All @@ -17,15 +17,17 @@ module.exports = {
* @param variables {object} The GraphQL variables
* @return {PromiseLike<never> | Promise<never>} Response data
*/
async function callGithubGraphql({query, variables}) {
async function callGithubGraphql({ query, variables }) {
const resp = await callGithubBase({
method: 'post',
uri: 'https://api.github.com/graphql',
data: {query, variables},
bearerAuth: true
data: { query, variables },
bearerAuth: true,
});
if (resp.errors) {
throw new Error(`Error calling GitHub GraphQL API. ${JSON.stringify(resp.errors)}`);
throw new Error(
`Error calling GitHub GraphQL API. ${JSON.stringify(resp.errors)}`,
);
}
return resp.data;
}
Expand All @@ -37,11 +39,11 @@ async function callGithubGraphql({query, variables}) {
* @param data {object} Data to put in the request body
* @return {*|PromiseLike<T|never>|Promise<T|never>} Response data
*/
async function callGithubRest({method, uri, data}) {
async function callGithubRest({ method, uri, data }) {
return await callGithubBase({
method,
uri: `https://api.github.com/${uri}`,
data
data,
});
}

Expand All @@ -53,17 +55,21 @@ async function callGithubRest({method, uri, data}) {
* @param bearerAuth {boolean=false} Use bearer auth. Otherwise use Basic auth
* @return {*|PromiseLike<T | never>|Promise<T | never>} Response data
*/
async function callGithubBase({method, uri, data, bearerAuth = false}) {
async function callGithubBase({ method, uri, data, bearerAuth = false }) {
const githubAccessToken = getGitHubAccessToken();
let res = await request({
uri,
method,
headers: {
Authorization: bearerAuth ? `bearer ${githubAccessToken}` : `Basic ${new Buffer(`${GITHUB_USERNAME}:${githubAccessToken}`).toString('base64')}`,
Authorization: bearerAuth
? `bearer ${githubAccessToken}`
: `Basic ${new Buffer(
`${GITHUB_USERNAME}:${githubAccessToken}`,
).toString('base64')}`,
'Content-Type': 'application/json',
'User-Agent': 'gish'
'User-Agent': 'gish',
},
body: JSON.stringify(data)
body: JSON.stringify(data),
});
return JSON.parse(res);
}
Expand All @@ -75,7 +81,7 @@ async function callGithubBase({method, uri, data, bearerAuth = false}) {
* @param title
* @return {*|PromiseLike<T | never>|Promise<T | never>} The number of the created PR
*/
async function createPullRequest({repositoryId, headBranch, title}) {
async function createPullRequest({ repositoryId, headBranch, title }) {
// TODO: Use variables
let resp = await callGithubGraphql({
query: `mutation {
Expand All @@ -90,7 +96,7 @@ async function createPullRequest({repositoryId, headBranch, title}) {
url
}
}
}`
}`,
});
return resp.createPullRequest.pullRequest;
}
Expand All @@ -102,7 +108,10 @@ async function createPullRequest({repositoryId, headBranch, title}) {
function getGitHubAccessToken() {
// TODO: Handle DNE
// TODO: Traverse the directory hierarchy looking for the first folder that contains this
return fs.readFileSync('.github/credentials').toString().trim();
return fs
.readFileSync('.github/credentials')
.toString()
.trim();
}

/**
Expand All @@ -111,14 +120,14 @@ function getGitHubAccessToken() {
* @param name
* @return {*|PromiseLike<T | never>|Promise<T | never>} The repository ID
*/
async function getRepositoryId({owner, name}) {
async function getRepositoryId({ owner, name }) {
// TODO: Use variables
let resp = await callGithubGraphql({
query: `query {
repository(owner: "${owner}", name: "${name}") {
id
}
}`
}`,
});
return resp.repository.id;
}
Expand All @@ -130,12 +139,13 @@ async function getRepositoryId({owner, name}) {
* @param pullRequestNumber {number}
* @return {Promise<void>}
*/
async function mergePullRequest({repoOwner, repoName, pullRequestNumber}) {
await callGithubRest({ // Have to use GitHub REST API for now because GraphQL doesn't support squash-and-merge
async function mergePullRequest({ repoOwner, repoName, pullRequestNumber }) {
await callGithubRest({
// Have to use GitHub REST API for now because GraphQL doesn't support squash-and-merge
method: 'put',
uri: `repos/${repoOwner}/${repoName}/pulls/${pullRequestNumber}/merge`,
data: {
merge_method: 'squash'
}
merge_method: 'squash',
},
});
}
5 changes: 2 additions & 3 deletions index.js
Expand Up @@ -3,8 +3,7 @@ const yargs = require('yargs');

// noinspection BadExpressionStatementJS
yargs
.scriptName('gish') // TODO: Better name
.scriptName('gish') // TODO: Better name
.commandDir('commands')
.demandCommand()
.help()
.argv;
.help().argv;
9 changes: 9 additions & 0 deletions package.json
Expand Up @@ -9,10 +9,19 @@
"scripts": {
"test": "./test/happy-path.sh"
},
"husky": {
"hooks": {
"pre-commit": "prettier --single-quote --trailing-comma all --write \"**/*.js\""
}
},
"dependencies": {
"request": "^2.88.0",
"request-promise": "^4.2.4",
"simple-git": "^1.110.0",
"yargs": "^13.2.2"
},
"devDependencies": {
"husky": "^2.3.0",
"prettier": "1.17.1"
}
}

0 comments on commit 8959f8c

Please sign in to comment.