diff --git a/app.yml b/app.yml index ba70d49..62c5a5d 100644 --- a/app.yml +++ b/app.yml @@ -74,7 +74,7 @@ default_permissions: # Search repositories, list collaborators, and access repository metadata. # https://developer.github.com/v3/apps/permissions/#metadata-permissions - # metadata: read + metadata: read # Retrieve Pages statuses, configuration, and builds, as well as create new builds. # https://developer.github.com/v3/apps/permissions/#permission-on-pages diff --git a/index.js b/index.js index 4a47ea0..6d9bbf7 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,20 @@ module.exports = app => { - // Your code here const fetch = require('node-fetch'); - app.log('Yay, the app was loaded!'); + app.log('I got a hot sauce in my bag, swag'); - const triggerWords = ["Bey", "Beyonce", "Beyoncé", "Yonce", "Yoncé", "Mrs. Carter", "Mrs Carter", "JayZ", "Jay-Z", "Jay Z", "Sasha Fierce", "Destiny's Child", "Destinys Child", "Solange", "Knowles"]; + const triggerWords = ["Bey", "Beyonce", "Beyoncé", "Yonce", "Yoncé", "Mrs. Carter", "Mrs Carter", "JayZ", "Jay-Z", "Jay Z", "Sasha Fierce", "Destiny's Child", "Destinys Child", "Solange", "Knowles", "queen b", "qween"]; + + const triggered = (webhookType, context) => { + const webhookTitle = context.payload[webhookType].title; + const webhookBody = context.payload[webhookType].body; + const webhookText = webhookTitle + ' ' + webhookBody; + + const containsArray = triggerWords.map(word => + webhookText.toLowerCase().includes(word.toLowerCase()) + ); + + return containsArray.includes(true); + }; const getBeyGif = () => { return fetch(`https://api.giphy.com/v1/gifs/random?api_key=${process.env.GIPHY}&tag=beyonce&rating=PG-13`) @@ -12,56 +23,37 @@ module.exports = app => { .catch(error => { app.log(error) }); }; - const postBeyGif = () => { - + const postBeyGif = async (context) => { + const bey = await getBeyGif(); + const issueComment = context.issue({ body: `![](https://media.giphy.com/media/${bey}/giphy.gif)` }); + return context.github.issues.createComment(issueComment); }; - app.on('issues.opened', async context => { - const issueTitle = context.payload.issue.title; - const issueBody = context.payload.issue.body; - const issue = issueBody + ' ' + issueTitle; - - const containsArray = triggerWords.map(word => - issue.toLowerCase().includes(word.toLowerCase()) - ); - - if (containsArray.includes(true)) { - const bey = await getBeyGif(); - const issueComment = context.issue({ body: `![](https://media.giphy.com/media/${bey}/giphy.gif)` }); - return context.github.issues.createComment(issueComment); - }; - - }); + const isBeyBday = (webhookType, context) => { + const updatedAt = context.payload[webhookType].updated_at; + const beyBday = '-09-04T'; - app.on('pull_request.opened', async context => { - const prTitle = context.payload.pull_request.title; - const prBody = context.payload.pull_request.body; - const pr = prTitle + ' ' + prBody; + return updatedAt.includes(beyBday); + }; - const containsArray = triggerWords.map(word => - pr.toLowerCase().includes(word.toLowerCase()) - ); + const postBeyBdayGif = (context) => { + const issueComment = context.issue({ body: `![](https://media.giphy.com/media/xUA7baZERc49eHitgc/giphy.gif)` }); + return context.github.issues.createComment(issueComment); + }; - if (containsArray.includes(true)) { - const bey = await getBeyGif(); - const issueComment = context.issue({ body: `![](https://media.giphy.com/media/${bey}/giphy.gif)` }); - return context.github.issues.createComment(issueComment); + app.on('issues.opened', context => { + if (isBeyBday('issue', context)) { + postBeyBdayGif(context); + } else if (triggered('issue', context)) { + postBeyGif(context); }; }); - app.on('pull_request_review_comment.created', async context => { - const prComment = context.payload.comment.body; - - - const containsArray = triggerWords.map(word => - prComment.toLowerCase().includes(word.toLowerCase()) - ); - - if (containsArray.includes(true)) { - const bey = await getBeyGif(); - app.log(context); - const issueComment = context.issue({ body: `![](https://media.giphy.com/media/${bey}/giphy.gif)` }); - return context.github.issues.createComment(issueComment); + app.on('pull_request.opened', context => { + if (isBeyBday('pull_request', context)) { + postBeyBdayGif(context); + } else if (triggered('pull_request', context)) { + postBeyGif(context); }; });