From 5b4510f6d952ac33c8a7698285769b776a53fe9f Mon Sep 17 00:00:00 2001 From: dessant Date: Tue, 12 Jun 2018 22:47:56 +0300 Subject: [PATCH] fix: remove database items for deleted installation --- src/index.js | 31 ++++++++++++++++++------------- src/reaction.js | 8 ++++++-- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index d54148b..db90ffe 100644 --- a/src/index.js +++ b/src/index.js @@ -36,25 +36,30 @@ module.exports = robot => { }); robot.on('installation.deleted', async context => { - await removeRepositories(context.payload.repositories); + await removeInstallation(context.payload.installation.id); }); robot.on('installation_repositories.removed', async context => { - await removeRepositories(context.payload.repositories_removed); + await removeRepositories( + context.payload.installation.id, + context.payload.repositories_removed + ); }); - async function removeRepositories(repos) { - if (repos.length) { - repos.forEach(item => scheduler.stop(item)); + async function removeInstallation(installationId) { + await App.getStorageStatic(db, `comments/${installationId}`).remove(); + } - await App.getStorageStatic(db, 'comments').update( - Object.assign( - ...repos.map(function(item) { - return {[item.id]: null}; - }) - ) - ); - } + async function removeRepositories(installationId, repos) { + repos.forEach(item => scheduler.stop(item)); + + await App.getStorageStatic(db, `comments/${installationId}`).update( + Object.assign( + ...repos.map(function(item) { + return {[item.id]: null}; + }) + ) + ); } async function getApp(context) { diff --git a/src/reaction.js b/src/reaction.js index e1ad89d..587c862 100644 --- a/src/reaction.js +++ b/src/reaction.js @@ -85,7 +85,9 @@ ${commentBody} GhResource.editComment({...commentParams, body: editedComment}) ); - await this.getStorage(`comments/${payload.repository.id}`).push({ + await this.getStorage( + `comments/${payload.installation.id}/${payload.repository.id}` + ).push({ dt: Date.now(), isReviewComment, commentId, @@ -97,7 +99,9 @@ ${commentBody} const {only: type} = this.config; const {payload, github} = this.context; - const commentsRef = this.getStorage(`comments/${payload.repository.id}`); + const commentsRef = this.getStorage( + `comments/${payload.installation.id}/${payload.repository.id}` + ); const comments = await commentsRef.limitToFirst(100).once('value'); const outdatedComments = [];