2018-07-12 23:07:16 +02:00
"use strict" ;
2018-09-27 18:11:26 +02:00
// P A C K A G E S
const async = require ( "async" ) ;
const color = require ( "colorette" ) ;
const local = require ( "app-root-path" ) . require ;
const octokit = require ( "@octokit/rest" ) ( ) ;
const redis = require ( "redis" ) ;
// V A R I A B L E S
const logSlackError = local ( "app/helpers/slack" ) ;
const relativeDate = local ( "app/modules/relative-date" ) ;
let client ;
// R E D I S
if ( typeof process . env . GITHUB _OAUTH _TOKEN !== "undefined" ) {
octokit . authenticate ( {
type : "oauth" ,
token : process . env . GITHUB _OAUTH _TOKEN
} ) ;
} else process . stdout . write ( ` ${ color . red ( "[missing]" ) } GitHub token ` ) ;
if ( typeof process . env . REDISCLOUD _URL !== "undefined" ) {
client = redis . createClient ( process . env . REDISCLOUD _URL ) ;
client . on ( "error" , redisError => {
process . env . NODE _ENV === "development" ?
process . stdout . write ( ` \n ${ color . yellow ( "Unable to connect to Redis client." ) } \n You may be missing an .env file or your connection was reset. ` ) :
logSlackError (
"\n" +
"> *REDIS ERROR:* ```" + JSON . parse ( JSON . stringify ( redisError ) ) + "```" + "\n" +
"> _Cause: Someone is trying to run LBRY.tech locally without environment variables OR Heroku is busted_\n"
)
;
} ) ;
} else process . stdout . write ( ` ${ color . red ( "[missing]" ) } Redis client URL ` ) ;
2018-07-12 23:07:16 +02:00
// P R O G R A M
function generateEvent ( event ) {
switch ( event . type ) {
case "CommitCommentEvent" :
return `
< strong > $ { event . actor . display _login } < / s t r o n g > c o m m e n t e d o n
< a href = "${generateUrl(" comment ", event)}" title = "View this comment on GitHub" > commit < / a > i n
` ;
case "CreateEvent" :
return `
< strong > $ { event . actor . display _login } < / s t r o n g > c r e a t e d
$ { event . payload . ref _type } < code > $ { event . payload . ref } < / c o d e > i n
` ;
case "DeleteEvent" :
return `
< strong > $ { event . actor . display _login } < / s t r o n g > d e l e t e d
$ { event . payload . ref _type } < code > $ { event . payload . ref } < / c o d e > i n
` ;
case "ForkEvent" :
return `
< strong > $ { event . actor . display _login } < / s t r o n g > f o r k e d
< strong > < a href = "${generateUrl(" repo ", event)}" title = "View this repo on GitHub" target = "_blank" rel = "noopener noreferrer" > $ { event . repo . name } < / a > < / s t r o n g > t o
< strong > < a href = "${generateUrl(" forkee ", event)}" title = "View this repo fork on GitHub" target = "_blank" rel = "noopener noreferrer" > $ { event . payload . forkee . name } < / a > < / s t r o n g > i n
` ;
case "IssueCommentEvent" :
if ( event . payload . issue . pull _request ) {
return `
< strong > $ { event . actor . display _login } < / s t r o n g > c o m m e n t e d o n p u l l r e q u e s t
< em > < a href = "${generateUrl(" issue ", event)}" title = "View this comment on GitHub" target = "_blank" rel = "noopener noreferrer" > $ { event . payload . issue . title } < / a > < / e m > i n
` ;
} else {
return `
< strong > $ { event . actor . display _login } < / s t r o n g > c o m m e n t e d o n i s s u e
< em > < a href = "${generateUrl(" issue ", event)}" title = "View this comment on GitHub" target = "_blank" rel = "noopener noreferrer" > $ { event . payload . issue . title } < / a > < / e m > i n
` ;
}
case "IssuesEvent" :
return `
< strong > $ { event . actor . display _login } < / s t r o n g > $ { e v e n t . p a y l o a d . a c t i o n } i s s u e
< em > < a href = "${generateUrl(" issue ", event)}" title = "View this issue on GitHub" target = "_blank" rel = "noopener noreferrer" > $ { event . payload . issue . title } < / a > < / e m > i n
` ;
case "PullRequestEvent" :
return `
< strong > $ { event . actor . display _login } < / s t r o n g > $ { e v e n t . p a y l o a d . a c t i o n } p u l l r e q u e s t
< em > < a href = "${generateUrl(" pull _request ", event)}" title = "View this pull request on GitHub" target = "_blank" rel = "noopener noreferrer" > $ { event . payload . pull _request . title } < / a > < / e m > i n
` ;
case "PullRequestReviewCommentEvent" :
return `
< strong > $ { event . actor . display _login } < / s t r o n g > c o m m e n t e d o n p u l l r e q u e s t
< em > < a href = "${generateUrl(" pull _request ", event)}" title = "View this comment on GitHub" target = "_blank" rel = "noopener noreferrer" > $ { event . payload . pull _request . title } < / a > < / e m > i n
` ;
case "PushEvent" :
return `
< strong > $ { event . actor . display _login } < / s t r o n g > p u s h e d t o
< code > < a href = "${generateUrl(" push ", event)}" title = "View this branch on GitHub" target = "_blank" rel = "noopener noreferrer" > $ { refToBranch ( event . payload . ref ) } < / a > < / c o d e > i n
` ;
case "ReleaseEvent" :
return `
< strong > $ { event . actor . display _login } < / s t r o n g > r e l e a s e d
< em > < a href = "${generateUrl(" release ", event)}" title = "View this release on GitHub" target = "_blank" rel = "noopener noreferrer" > $ { event . payload . release . tag _name } < / a > < / e m > i n
` ;
case "WatchEvent" :
return `
< strong > $ { event . actor . display _login } < / s t r o n g > s t a r r e d t h e r e p o
` ;
default :
break ;
}
}
2018-09-27 18:11:26 +02:00
function generateGitHubFeed ( displayGitHubFeed ) {
if ( typeof process . env . REDISCLOUD _URL !== "undefined" ) {
client . zrevrange ( "events" , 0 , 9 , ( err , reply ) => {
if ( err ) return ; // TODO: Render a div with nice error message
const events = [ ] ;
const renderedEvents = [ ] ;
reply . forEach ( item => events . push ( JSON . parse ( item ) ) ) ;
for ( const event of events ) {
renderedEvents . push ( `
< div class = 'github-feed__event' >
< a href = "${generateUrl(" actor ", event)}" target = "_blank" rel = "noopener noreferrer" >
< img src = "${event.actor.avatar_url}" class = "github-feed__event__avatar" alt = "" / >
< / a >
< p >
$ { generateEvent ( event ) }
< a href = "${generateUrl(" repo ", event)}" title = "View this repo on GitHub" target = "_blank" rel = "noopener noreferrer" > < strong > $ { event . repo . name } < / s t r o n g > < / a >
< em class = "github-feed__event__time" > $ { relativeDate ( new Date ( event . created _at ) ) } < / e m >
< / p >
< / d i v >
` );
}
updateGithubFeed ( ) ; // TODO: Update `.last-updated` every minute
displayGitHubFeed ( `
< h3 > GitHub < / h 3 >
< h5 class = "last-updated" > Last updated : $ { new Date ( ) . format ( "YYYY-MM-DD" ) . replace ( /-/g , "·" ) } at $ { new Date ( ) . add ( - 4 , "hours" ) . format ( "UTC:H:mm:ss A" ) . toLowerCase ( ) } EST < / h 5 >
$ { renderedEvents . join ( "" ) }
` );
} ) ;
}
}
2018-07-12 23:07:16 +02:00
function generateUrl ( type , event ) {
switch ( type ) {
case "actor" :
return ` https://github.com/ ${ event . actor . display _login } ` ;
case "comment" :
return event . payload . comment . html _url ;
case "repo" :
return ` https://github.com/ ${ event . repo . name } ` ;
case "forkee" :
return ` https://github.com/ ${ event . payload . forkee . full _name } ` ;
case "issue" :
return event . payload . issue . html _url ;
case "pull_request" :
return event . payload . pull _request . html _url ;
case "release" :
return event . payload . release . html _url ;
case "push" :
return ` https://github.com/ ${ event . repo . name } /tree/ ${ event . payload . ref . replace ( "refs/heads/" , "" ) } ` ;
default :
break ;
}
}
2018-09-27 18:11:26 +02:00
function updateGithubFeed ( ) {
octokit . activity . getEventsForOrg ( {
org : "lbryio" ,
per _page : 20 ,
page : 1
} ) . then ( ( { data } ) => {
async . eachSeries ( data , ( item , callback ) => {
const eventString = JSON . stringify ( item ) ;
client . zrank ( "events" , eventString , ( err , reply ) => {
if ( reply === null ) client . zadd ( "events" , item . id , eventString , callback ) ;
else callback ( ) ;
} ) ;
} , ( ) => client . zremrangebyrank ( "events" , 0 , - 51 ) ) ; // Keep the latest 50 events
} ) . catch ( err => {
logSlackError (
"\n" +
"> *GITHUB FEED ERROR:* ```" + JSON . parse ( JSON . stringify ( err ) ) + "```" + "\n" +
"> _Cause: GitHub feed refresh_\n"
) ;
} ) ;
}
2018-07-12 23:07:16 +02:00
// H E L P E R
function refToBranch ( ref ) {
return ref . replace ( "refs/heads/" , "" ) ;
}
2018-08-14 22:15:27 +02:00
// E X P O R T S
2018-07-12 23:07:16 +02:00
module . exports = exports = {
generateEvent ,
2018-09-27 18:11:26 +02:00
generateGitHubFeed ,
generateUrl ,
updateGithubFeed
2018-07-12 23:07:16 +02:00
} ;