|
| 1 | +const { App } = require('@slack/bolt'); |
| 2 | +const mongoose = require('mongoose'); |
| 3 | + |
| 4 | +// commented, approved, changes_requested |
| 5 | + |
| 6 | +const getUserModel = () => { |
| 7 | + const userSchema = new mongoose.Schema({ |
| 8 | + slackId: { type: String, required: true }, // Slack user ID |
| 9 | + slackName: { type: String, required: true }, // Slack user name; do we want to keep track of this |
| 10 | + role: { type: String, required: true }, // "ADMIN" | "TECHLEAD" | "MEMBER" - defined in config/perms.js |
| 11 | + repos: { type: Array, required: true }, // List of assigned projects |
| 12 | + github: { type: String, required: true }, |
| 13 | + rep: { type: Number, required: true }, // Reputation count |
| 14 | + matchyEnabled: { type: Boolean, required: true }, // Opted into Matchy or not |
| 15 | + }); |
| 16 | + return mongoose.model('User', userSchema); |
| 17 | +}; |
| 18 | + |
| 19 | +const messageAssignee = async ({ context }) => { |
| 20 | + // Initializes your app with your bot token and signing secret |
| 21 | + const Bot = new App({ |
| 22 | + token: process.env.SLACK_TOKEN, |
| 23 | + signingSecret: process.env.SIGNING_SECRET, |
| 24 | + socketMode: true, |
| 25 | + appToken: process.env.APP_LEVEL_TOKEN, |
| 26 | + }); |
| 27 | + |
| 28 | + const reviewer = context.payload.sender.login; |
| 29 | + const githubAssignees = context.payload.pull_request.assignees; |
| 30 | + const url = context.payload.review.html_url; |
| 31 | + |
| 32 | + // Connect to mongo |
| 33 | + mongoose.connect(process.env.MONGO_URI, { |
| 34 | + useNewUrlParser: true, |
| 35 | + useUnifiedTopology: true, |
| 36 | + }); |
| 37 | + const mongoConnection = mongoose.connection; |
| 38 | + // After connection is established, do the things |
| 39 | + mongoConnection.once('open', async () => { |
| 40 | + try { |
| 41 | + const UserModel = getUserModel(); |
| 42 | + const slackAssignees = await Promise.allSettled( |
| 43 | + githubAssignees.map(assignee => UserModel.findOne({ github: assignee.login })), |
| 44 | + ); |
| 45 | + if (context.payload.review.state === 'approved') { |
| 46 | + await Promise.all( |
| 47 | + slackAssignees.map(assignee => |
| 48 | + Bot.client.chat.postMessage({ |
| 49 | + channel: assignee.value?.slackId, |
| 50 | + text: `One of your pull requests has been APPROVED by ${reviewer}! <${url}|View Review> :shrek::thumbsup:`, |
| 51 | + }), |
| 52 | + ), |
| 53 | + ); |
| 54 | + } else { |
| 55 | + await Promise.all( |
| 56 | + slackAssignees.map(assignee => |
| 57 | + Bot.client.chat.postMessage({ |
| 58 | + channel: assignee.value?.slackId, |
| 59 | + text: `One of your pull requests has been REVIEWED by ${reviewer}! <${url}|View Review> :shrek:`, |
| 60 | + }), |
| 61 | + ), |
| 62 | + ); |
| 63 | + } |
| 64 | + } catch (e) { |
| 65 | + console.log(e); |
| 66 | + } |
| 67 | + mongoConnection.close(); |
| 68 | + }); |
| 69 | +}; |
| 70 | + |
| 71 | +module.exports = messageAssignee; |
0 commit comments