-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from blamattina/issue-comment-once
Add support for issue-comment --once
- Loading branch information
Showing
13 changed files
with
414 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules/ | ||
npm-debug.log | ||
.env |
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 @@ | ||
4.1.1 |
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
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,3 @@ | ||
machine: | ||
node: | ||
version: 4.1.1 |
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 |
---|---|---|
@@ -1,16 +1,26 @@ | ||
'use strict'; | ||
var Promise = require('bluebird'); | ||
var GithubApi = require('../github/api'); | ||
var Issue = require('../github/issue'); | ||
|
||
module.exports = function createIssueComment(params) { | ||
const ALREADY_EXISTS = 'Not created. A comment with this message already exists.'; | ||
|
||
var api = new GithubApi(); | ||
|
||
var issue = new Issue({ | ||
module.exports = (params) => { | ||
const api = new GithubApi(); | ||
const issue = new Issue({ | ||
api: api, | ||
user: params.user, | ||
repo: params.repo, | ||
number: params.number | ||
}); | ||
|
||
return issue.createComment(params.body); | ||
const createComment = () => issue.createComment(params.body); | ||
const alreadyExists = () => Promise.resolve(ALREADY_EXISTS); | ||
|
||
if(params.once) { | ||
return issue.findComment({body: params.body}) | ||
.then(alreadyExists, createComment); | ||
} else { | ||
return createComment(); | ||
} | ||
}; |
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,55 +1,92 @@ | ||
var _ = require('lodash'); | ||
var debug = require('debug')('pull-request-hotline:githup-api'); | ||
'use strict'; | ||
const _ = require('lodash'); | ||
const debug = require('debug')('github-hotline:github:api'); | ||
const Promise = require('bluebird'); | ||
|
||
// Rewire-able | ||
var Github = require('github'); | ||
var Promise = require('bluebird'); | ||
|
||
var wrappedFunctions = { | ||
createIssueComment: { module: 'issues', fn: 'createComment' } | ||
const wrappedFunctions = { | ||
getIssue: { module: 'issues', fn: 'getRepoIssue' }, | ||
createIssueComment: { module: 'issues', fn: 'createComment' }, | ||
getIssueCommentsAsync: { module: 'issues', fn: 'getComments' } | ||
} | ||
|
||
var wrapLibraryFunctionsInPromises = function(wrapped, api) { | ||
Object.keys(wrappedFunctions).forEach(function(wrappedFn) { | ||
wrapped[wrappedFn] = function(params) { | ||
const wrapLibraryFunctionsInPromises = (wrapped, api) => { | ||
Object.keys(wrappedFunctions).forEach((wrappedFn) => { | ||
wrapped[wrappedFn] = (params) => { | ||
|
||
debug('Calling ' + wrappedFn); | ||
debug(params); | ||
var wrapping = wrappedFunctions[wrappedFn]; | ||
var fnAsync = Promise.promisify(api[wrapping.module][wrapping.fn]); | ||
return fnAsync(params); | ||
const wrapping = wrappedFunctions[wrappedFn]; | ||
const fnAsync = Promise.promisify(api[wrapping.module][wrapping.fn]); | ||
const promise = fnAsync(params); | ||
|
||
promise.then( | ||
(data) => debug('Resolving with: ', data), | ||
(err) => debug('Failing with: ', err) | ||
); | ||
return promise; | ||
} | ||
}); | ||
} | ||
|
||
function ensureEnvVariables() { | ||
const ensureEnvVariables = () => { | ||
if (!process.env.GITHUB_USERNAME && !process.env.GITHUB_ACCESS_TOKEN) { | ||
throw new Error( | ||
'Missing environment variables: GITHUB_USERNAME, GITHUB_ACCESS_TOKEN' | ||
); | ||
} | ||
} | ||
|
||
function GithubApi() { | ||
debug('Creating Github API wrapper.'); | ||
ensureEnvVariables(); | ||
class GithubApi { | ||
constructor() { | ||
debug('Creating Github API wrapper.'); | ||
ensureEnvVariables(); | ||
|
||
var options = { | ||
version: "3.0.0" | ||
}; | ||
|
||
if (process.env.GITHUB_ENTERPRISE_HOSTNAME) { | ||
options.host = process.env.GITHUB_ENTERPRISE_HOSTNAME; | ||
options.pathPrefix = '/api/v3'; | ||
debug('Using Github Host: ' + options.host); | ||
} | ||
|
||
var options = { | ||
version: "3.0.0" | ||
}; | ||
this.api = new Github(options); | ||
debug('Authenticating as: ' + process.env.GITHUB_USERNAME); | ||
this.api.authenticate({ | ||
type: 'basic', | ||
username: process.env.GITHUB_USERNAME, | ||
password: process.env.GITHUB_ACCESS_TOKEN | ||
}); | ||
|
||
if (process.env.GITHUB_ENTERPRISE_HOSTNAME) { | ||
options.host = process.env.GITHUB_ENTERPRISE_HOSTNAME; | ||
options.pathPrefix = '/api/v3'; | ||
debug('Using Github Host: ' + options.host); | ||
wrapLibraryFunctionsInPromises(this, this.api); | ||
} | ||
|
||
this.api = new Github(options); | ||
debug('Authenticating as: ' + process.env.GITHUB_USERNAME); | ||
this.api.authenticate({ | ||
type: 'basic', | ||
username: process.env.GITHUB_USERNAME, | ||
password: process.env.GITHUB_ACCESS_TOKEN | ||
}); | ||
getIssueComments(params) { | ||
debug('Calling getIssueComments'); | ||
debug(params); | ||
return this.getIssue(params).then((issue) => { | ||
const perPage = 30; | ||
var page = 0; | ||
|
||
wrapLibraryFunctionsInPromises(this, this.api); | ||
} | ||
debug('Creating comment generator'); | ||
return (function* generator(issue) { | ||
const pages = Math.ceil(issue.comments / perPage); | ||
|
||
debug(pages + ' comment pages available'); | ||
while(page < pages) { | ||
page++; | ||
let pageParams = { page: page, per_page: perPage }; | ||
|
||
debug('Yeilding ' + page + ' of ' + pages); | ||
yield this.getIssueCommentsAsync(_.assign(pageParams, params)) | ||
} | ||
}.bind(this))(issue); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = GithubApi; |
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,19 +1,51 @@ | ||
function Issue(params) { | ||
this.api = params.api; | ||
this.user = params.user; | ||
this.repo = params.repo; | ||
this.number = params.number; | ||
} | ||
'use strict'; | ||
var _ = require('lodash'); | ||
var debug = require('debug')('github-hotline:github:issue'); | ||
|
||
class Issue { | ||
constructor(params) { | ||
this.api = params.api; | ||
this.user = params.user; | ||
this.repo = params.repo; | ||
this.number = params.number; | ||
} | ||
|
||
Issue.prototype = { | ||
createComment: function (body) { | ||
createComment(body) { | ||
return this.api.createIssueComment({ | ||
user: this.user, | ||
repo: this.repo, | ||
number: this.number, | ||
body: body | ||
}); | ||
} | ||
|
||
findComment(source) { | ||
var commentGenerator; | ||
|
||
const setGenerator = (generator) => commentGenerator = generator; | ||
const hasFoundComment = (comments) => _(comments).findWhere(source); | ||
|
||
function findCommentRecursively() { | ||
debug('Recursively searching for comments'); | ||
const commentPromise = commentGenerator.next().value; | ||
debug(commentPromise); | ||
if (commentPromise) { | ||
return commentPromise.then((comments) => { | ||
return hasFoundComment(comments) || findCommentRecursively(); | ||
}); | ||
} | ||
return Promise.reject('Comment not found.'); | ||
}; | ||
|
||
const issueParams = { | ||
user: this.user, | ||
repo: this.repo, | ||
number: this.number | ||
}; | ||
return this.api.getIssueComments(issueParams) | ||
.then(setGenerator) | ||
.then(findCommentRecursively); | ||
} | ||
} | ||
|
||
module.exports = Issue; |
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
Oops, something went wrong.