Skip to content
This repository has been archived by the owner on Jul 3, 2021. It is now read-only.

Commit

Permalink
feat: log additional data and add DRY_RUN env var
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Jun 26, 2018
1 parent b2d4e20 commit f311d57
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 39 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and deletes *new* reaction comments, such as +1.

## Supporting the Project

The ongoing development of Reaction Comments is made possible
The continued development of Reaction Comments is made possible
thanks to the support of awesome backers. If you'd like to join them,
please consider contributing with [Patreon](https://goo.gl/qRhKSW),
[PayPal](https://goo.gl/5FnBaw) or [Bitcoin](https://goo.gl/uJUAaU).
Expand All @@ -23,6 +23,7 @@ or it is deleted immediately, if `reactionComment` is set to `false`.
## Usage

1. **[Install the GitHub App](https://github.com/apps/reaction)**
for the required repositories
2. Create `.github/reaction.yml` based on the template below

#### Configuration
Expand Down
4 changes: 2 additions & 2 deletions assets/app-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The app detects if a new comment consists solely of emojis and shortcodes used i

## Usage

1. **[Install the GitHub App](https://github.com/apps/reaction)**
1. **[Install the GitHub App](https://github.com/apps/reaction)** for the required repositories
2. Create `.github/reaction.yml` based on the template below

#### Configuration
Expand Down Expand Up @@ -44,4 +44,4 @@ reactionComment: >

## Supporting the Project

The ongoing development of Reaction Comments is made possible thanks to the support of awesome backers. If you'd like to join them, please consider contributing with [Patreon](https://goo.gl/qRhKSW), [PayPal](https://goo.gl/5FnBaw) or [Bitcoin](https://goo.gl/uJUAaU).
The continued development of Reaction Comments is made possible thanks to the support of awesome backers. If you'd like to join them, please consider contributing with [Patreon](https://goo.gl/qRhKSW), [PayPal](https://goo.gl/5FnBaw) or [Bitcoin](https://goo.gl/uJUAaU).
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"joi": "^13.4.0",
"probot": "^6.2.0",
"probot-config": "^0.1.0",
"probot-scheduler": "^1.2.0"
"probot-scheduler": "^1.2.0",
"uuid": "^3.3.0"
},
"devDependencies": {
"nodemon": "1.17.5",
Expand Down
38 changes: 22 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const uuidV4 = require('uuid/v4');
const firebase = require('firebase-admin');
const createScheduler = require('probot-scheduler');
const getMergedConfig = require('probot-config');

const App = require('./reaction');
const schema = require('./schema');

module.exports = robot => {
firebase.initializeApp({
module.exports = async robot => {
const firebaseApp = firebase.initializeApp({
credential: firebase.credential.cert(process.env.FIREBASE_KEY_PATH),
databaseURL: process.env.FIREBASE_DB_URL
});
const db = firebase.database();
const db = firebaseApp.database();

const github = await robot.auth();
const appName = (await github.apps.get({})).data.name;

const scheduler = createScheduler(robot);

Expand Down Expand Up @@ -63,26 +67,28 @@ module.exports = robot => {
}

async function getApp(context) {
const config = await getConfig(context);
if (config) {
return new App(context, config, robot.log, db);
const logger = context.log.child({appName, session: uuidV4()});
const config = await getConfig(context, logger);
if (config && config.perform) {
return new App(context, config, logger, db);
}
}

async function getConfig(context) {
const {owner, repo} = context.repo();
async function getConfig(context, log, file = 'reaction.yml') {
let config;
const repo = context.repo();
try {
const repoConfig = await getMergedConfig(context, 'reaction.yml');
if (repoConfig) {
const {error, value} = schema.validate(repoConfig);
if (error) {
throw error;
}
config = value;
let repoConfig = await getMergedConfig(context, file);
if (!repoConfig) {
repoConfig = {perform: false};
}
const {error, value} = schema.validate(repoConfig);
if (error) {
throw error;
}
config = value;
} catch (err) {
robot.log.warn({err: new Error(err), owner, repo}, 'Invalid config');
log.warn({err: new Error(err), repo, file}, 'Invalid config');
}

return config;
Expand Down
35 changes: 17 additions & 18 deletions src/reaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ module.exports = class Reaction {
constructor(context, config, logger, db) {
this.context = context;
this.config = config;
this.logger = logger;
this.log = logger;
this.db = db;
}

async comment() {
const {only: type} = this.config;
const {isBot, payload, github} = this.context;
const {owner, repo, number: issue} = this.context.issue();
const issue = this.context.issue();

if (isBot || (type === 'issues' && payload.issue.pull_request)) {
return;
Expand All @@ -28,8 +28,8 @@ module.exports = class Reaction {

const commentId = payload.comment.id;
const commentParams = {
owner,
repo,
owner: issue.owner,
repo: issue.repo,
comment_id: commentId
};
const commentBody = payload.comment.body;
Expand All @@ -38,9 +38,7 @@ module.exports = class Reaction {
}

const {data: issueData} = await github.issues.get({
owner,
repo,
number: issue,
...issue,
headers: {
Accept: 'application/vnd.github.sailor-v-preview+json'
}
Expand All @@ -55,8 +53,8 @@ module.exports = class Reaction {
const GhResource = isReviewComment ? github.pullRequests : github.issues;

if (!reactionComment) {
this.logger.info({issue, ...commentParams}, 'Deleting comment');
await this.ensureUnlock({owner, repo, number: issue}, lock, () =>
this.log.info({issue, commentId}, 'Deleting comment');
await this.ensureUnlock(issue, lock, () =>
GhResource.deleteComment(commentParams)
);
return;
Expand All @@ -83,8 +81,8 @@ module.exports = class Reaction {
</h6>
`;

this.logger.info({issue, ...commentParams}, 'Editing comment');
await this.ensureUnlock({owner, repo, number: issue}, lock, () =>
this.log.info({issue, commentId}, 'Editing comment');
await this.ensureUnlock(issue, lock, () =>
GhResource.editComment({...commentParams, body: editedComment})
);

Expand All @@ -94,7 +92,7 @@ module.exports = class Reaction {
dt: Date.now(),
isReviewComment,
commentId,
issue
issue: issue.number
});
}

Expand Down Expand Up @@ -141,11 +139,9 @@ module.exports = class Reaction {
}
const commentBody = commentData.body;
if (/<!--notice-->/.test(commentBody) || reactionRx.test(commentBody)) {
const issue = comment.issue;
const issue = {owner, repo, number: comment.issue};
const {data: issueData} = await github.issues.get({
owner,
repo,
number: issue,
...issue,
headers: {
Accept: 'application/vnd.github.sailor-v-preview+json'
}
Expand All @@ -155,8 +151,11 @@ module.exports = class Reaction {
reason: issueData.active_lock_reason
};

this.logger.info({issue, ...commentParams}, 'Deleting comment');
await this.ensureUnlock({owner, repo, number: issue}, lock, () =>
this.log.info(
{issue, commentId: comment.commentId},
'Deleting comment'
);
await this.ensureUnlock(issue, lock, () =>
GhResource.deleteComment(commentParams)
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const schema = Joi.object().keys({
.description('Limit to only `issues` or `pulls`'),
pulls: Joi.object().keys(fields),
issues: Joi.object().keys(fields),
_extends: Joi.string().description('Repository to extend settings from')
_extends: Joi.string().description('Repository to extend settings from'),
perform: Joi.boolean().default(!process.env.DRY_RUN)
});

module.exports = schema;
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5088,6 +5088,10 @@ uuid@^3.1.0:
version "3.2.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14"

uuid@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.0.tgz#b237147804881d7b86f40a7ff8f590f15c37de32"

validate-npm-package-license@^3.0.1:
version "3.0.3"
resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.3.tgz#81643bcbef1bdfecd4623793dc4648948ba98338"
Expand Down

0 comments on commit f311d57

Please sign in to comment.