Skip to content

Commit

Permalink
Merge pull request #5 from bearyinnovative/npm
Browse files Browse the repository at this point in the history
refactor for release
  • Loading branch information
loddit authored Aug 18, 2017
2 parents 3f8e82a + 9e264fe commit 5a187e9
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 62 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ It benefits us quite a lot in these case:
Legilimens help you generate release report with pull requests info


## Usage
## Command Usage

1. Edit a config file in legilimens`s repo folder to setup GitHub account and repo
1. Install packages
`npm install`

2. Edit a config file in legilimens`s repo folder to setup GitHub account and repo

`.config.json`

```
{
"username": // your github login name
"password": // your github password
"token": // your github access token, see https://github.com/settings/tokens not needed for public repo
"repo_path": // as github/hubut in https://github.com/github/hubot
"repo_branch": // prs' base branch, eg: `master`
}
```

2. Install packages
`npm install`
(Optinal, otherwise you need answer some interactive command questions later)

3. Run Script
`npm start`
Expand All @@ -64,6 +64,18 @@ Legilimens help you generate release report with pull requests info
4. For Hofix
`npm start [hotfix-branch-name]`

## Library Usage

```js
const legilimens = require('legilimens');

legilimens(token, repoPath, baseBranch, function(output) {
...
your code here(use output)
...
});
```

# Legilimens

In Harry Potter`s Magic World, Legilimens is the spell used for the practice of Legilimency (OP24). Legilimens is presumably used non-verbally whenever someone uses the arts of Legilimency to obtain information. However, there may be subtler ways to do this.
Expand Down
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs');
const legilimens = require('./legilimens.js');
const inquirer = require('inquirer');

if (fs.existsSync(".config.json")) {
const config = JSON.parse(fs.readFileSync('.config.json','utf8'));
const token = config.token;
const repoPath = config.repo_path;
const baseBranch = process.argv[2] || config.repo_branch || 'master';
legilimens(token, repoPath, baseBranch, (output) => {
console.log(output);
})
} else {
const questions = [{
type: "input",
name: "repo",
message: "what is your repo path? (as hubotio/hubot in https://github.com/hubotio/hubot)\n"
}, {
type: "input",
name: "branch",
message: "what is your repo release branch?\n",
default: "master"
}, {
type: "input",
name: "branch",
message: "give me a access token for API, you can get one via https://github.com/settings/tokens\n(optional, not need for public repo)",
default: null
}]
inquirer.prompt(questions).then(function (answers) {
legilimens(answers.token, answers.repo, answers.branch, (output) => {
console.log(output);
})
});
}


109 changes: 56 additions & 53 deletions legilimens.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,83 @@
const request = require('request');
const fs = require('fs');

const config = JSON.parse(fs.readFileSync('.config.json','utf8'));

const GITHUB_REPO_API_ROOT = "https://api.github.com/repos/";
const RELEASES_PATH = "/releases";
const RECENT_CLOSED_PR_PATH = "/pulls?state=closed&sort=updated&direction=desc";

const { username, password } = config;
const repoPath = config.repo_path;
const baseBranch = process.argv[2] || config.repo_branch || 'master';
const isHotfix = !!process.argv[2];

const repoUrl = `${GITHUB_REPO_API_ROOT}${repoPath}`;

function callGithubAPI({url, callback}) {
function callGithubAPI({url, token=null, callback}) {
const headers = {
'User-Agent': 'legilimens',
};
if (token) {
headers['Authorization'] = `token ${token}`;
}
request({
url,
headers: {
'User-Agent': 'request'
},
auth: {
user: username,
password
}
headers,
}, callback);
}

const getLastedReleaseTime = new Promise((resolve, reject) => {
callGithubAPI({
url: repoUrl + RELEASES_PATH,
callback(error, response, body) {
switch (response.statusCode) {
case 200:
const lastedRelease = JSON.parse(body).filter(release => {
return ((release.target_commitish === 'master') || isHotfix) && !release.prerelease;
})[0];
const lastedReleaseTime = lastedRelease ? new Date(lastedRelease.created_at) : new Date(1970,1,1);
return resolve(lastedReleaseTime);
case 404:
console.log("No releases before");
return resolve(new Date(1970,1,1));
default:
console.log(error, body, response.statusCode);
return reject(error);
function getLastedReleaseTime(token, repoPath, baseBranch="master") {
const repoUrl = `${GITHUB_REPO_API_ROOT}${repoPath}`;
return new Promise((resolve, reject) => {
callGithubAPI({
url: repoUrl + RELEASES_PATH,
token: token,
callback(error, response, body) {
switch (response.statusCode) {
case 200:
const lastedRelease = JSON.parse(body).filter(release => {
return release.target_commitish === baseBranch && !release.prerelease;
})[0];
const lastedReleaseTime = lastedRelease ? new Date(lastedRelease.created_at) : new Date(1970,1,1);
return resolve(lastedReleaseTime);
case 404:
console.log("No releases before");
return resolve(new Date(1970,1,1));
default:
console.log(error, body, response.statusCode);
return reject(error);
}
}
}
})
});
})
});
}

function getClosedPullRequestsAfter(time) {
function getClosedPullRequestsAfter(token, repoPath, time, baseBranch="master", callback) {
const repoUrl = `${GITHUB_REPO_API_ROOT}${repoPath}`;
callGithubAPI({
url: repoUrl + RECENT_CLOSED_PR_PATH,
token: token,
callback(error, response, body) {
if (!error && (response.statusCode !== 200)) {
return console.log(error, body);
} else {
const pullRequests = JSON.parse(body)
.filter(pullRequest => new Date(pullRequest.merged_at) > time).filter(pullRequest => pullRequest.base.ref === baseBranch);
if (pullRequests.length) {
return printPullRequestsReport(pullRequests);
} else {
return console.log('No new pull requests be merged.');
}
.filter(pullRequest => new Date(pullRequest.merged_at) > time)
.filter(pullRequest => pullRequest.base.ref === baseBranch);
callback(renderPullRequestsReport(pullRequests));
}
}
});
}

function printPullRequestsReport(pullRequests) {
console.log("New merged pull requests:");
let index = 1;
return pullRequests.forEach(function(pullRequest) {
console.log(`- [ ] ${index}. #${pullRequest.number} ${pullRequest.title} by @${pullRequest.user.login}`);
return index++;
});
function renderPullRequestsReport(pullRequests) {
let output = '';
if (pullRequests.length) {
output += "New merged pull requests:";
let index = 1;
pullRequests.forEach(function(pullRequest) {
output += `\n- [ ] ${index}. #${pullRequest.number} ${pullRequest.title} by @${pullRequest.user.login}`;
});
} else {
output += 'No new pull requests be merged.';
}
return output;
};

getLastedReleaseTime.then(lastedReleaseTime => getClosedPullRequestsAfter(lastedReleaseTime));

module.exports = (token, repoPath, baseBranch, callback) => {
getLastedReleaseTime(token, repoPath, baseBranch).then((lastedReleaseTime) => {
getClosedPullRequestsAfter(token, repoPath, lastedReleaseTime, baseBranch, callback);
});
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"url": "https://github.com/bearyinnovative/legilimens.git"
},
"scripts": {
"start": "node legilimens.js"
"start": "node index.js"
},
"dependencies": {
"coffee-script": "^1.8.0",
"inquirer": "^3.2.2",
"request": "^2.60.0"
}
},
"main": "./legilimens.js"
}

0 comments on commit 5a187e9

Please sign in to comment.