Skip to content

Commit

Permalink
Refactor indexjs, add triggerwords, update yml
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktu12 committed Jan 7, 2019
1 parent 67af3b4 commit e30c000
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 46 deletions.
2 changes: 1 addition & 1 deletion app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 37 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
@@ -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`)
Expand All @@ -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);
};
});

Expand Down

0 comments on commit e30c000

Please sign in to comment.