From d9979adfebe6e48caffc479ecd514b3dd4082b2a Mon Sep 17 00:00:00 2001 From: loddit Date: Sun, 16 Aug 2015 15:49:23 +0800 Subject: [PATCH] use obejct params for callGitHubAPI --- legilimens.coffee | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/legilimens.coffee b/legilimens.coffee index e415c95..96da14d 100644 --- a/legilimens.coffee +++ b/legilimens.coffee @@ -11,7 +11,7 @@ repoName = 'reponame' repoUrl = "#{GITHUB_REPO_API_ROOT}#{repoOwner}/#{repoName}" -callGithubAPI = (url, callback = (error, response, body) -> {}) -> +callGithubAPI = ({url, callback}) -> request url: url headers: @@ -21,26 +21,31 @@ callGithubAPI = (url, callback = (error, response, body) -> {}) -> password: password , callback -getClosedPullRequestsAfter = (time) -> - callGithubAPI (repoUrl + RECENT_CLOSED_PR_PATH), (error, response, body) -> - if (!error and response.statusCode isnt 200) - console.log error, body - else - pullRequests = JSON.parse(body).filter (pullRequest) -> - new Date(pullRequest.merged_at) > time - if pullRequests.length - printPullRequestsReport(pullRequests) +getLastedReleaseTime = new Promise (resolve, reject) -> + callGithubAPI + url: repoUrl + LATEST_RELEASE_PATH + callback: (error, response, body) -> + if (!error and response.statusCode isnt 200) + console.log error, body + reject error else - console.log 'No new pull requests be merged.' + lastedReleaseTime = new Date(JSON.parse(body).created_at) + console.log "Last release time is #{lastedReleaseTime.toLocaleTimeString()} #{lastedReleaseTime.toLocaleDateString()}" + resolve(lastedReleaseTime) -getLastedReleaseTime = new Promise (resolve, reject) -> - callGithubAPI (repoUrl + LATEST_RELEASE_PATH), (error, response, body) -> - if (!error and response.statusCode isnt 200) - reject error - else - lastedReleaseTime = new Date(JSON.parse(body).created_at) - console.log "Last release time is #{lastedReleaseTime.toLocaleTimeString()} #{lastedReleaseTime.toLocaleDateString()}" - resolve(lastedReleaseTime) +getClosedPullRequestsAfter = (time) -> + callGithubAPI + url: repoUrl + RECENT_CLOSED_PR_PATH + callback: (error, response, body) -> + if (!error and response.statusCode isnt 200) + console.log error, body + else + pullRequests = JSON.parse(body).filter (pullRequest) -> + new Date(pullRequest.merged_at) > time + if pullRequests.length + printPullRequestsReport(pullRequests) + else + console.log 'No new pull requests be merged.' printPullRequestsReport = (pullRequests) -> index = 1 @@ -48,4 +53,5 @@ printPullRequestsReport = (pullRequests) -> console.log "#{index}. #{pullRequest.title} by @#{pullRequest.user.login}" index++ -getLastedReleaseTime.then getClosedPullRequestsAfter +getLastedReleaseTime.then (lastedReleaseTime) -> + getClosedPullRequestsAfter(lastedReleaseTime)