-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from bearyinnovative/npm
refactor for release
- Loading branch information
Showing
4 changed files
with
115 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters