Skip to content

Commit

Permalink
Slack notifications automation (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspergrom authored May 9, 2023
1 parent a33a0a4 commit 3d953d3
Show file tree
Hide file tree
Showing 67 changed files with 2,261 additions and 1,199 deletions.
4 changes: 4 additions & 0 deletions backend/.env.dist.local
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ CROWD_SLACK_CLIENT_SECRET=
CROWD_SLACK_REPORTER_TOKEN=
CROWD_SLACK_REPORTER_CHANNEL=

# Slack notifier settings
CROWD_SLACK_NOTIFIER_CLIENT_ID=
CROWD_SLACK_NOTIFIER_CLIENT_SECRET=

# Google settings
CROWD_GOOGLE_CLIENT_ID=
CROWD_GOOGLE_CLIENT_SECRET=
Expand Down
4 changes: 4 additions & 0 deletions backend/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@
"appId": "CROWD_SLACK_APP_ID",
"appToken": "CROWD_SLACK_APP_TOKEN"
},
"slackNotifier": {
"clientId": "CROWD_SLACK_NOTIFIER_CLIENT_ID",
"clientSecret": "CROWD_SLACK_NOTIFIER_CLIENT_SECRET"
},
"google": {
"clientId": "CROWD_GOOGLE_CLIENT_ID",
"clientSecret": "CROWD_GOOGLE_CLIENT_SECRET",
Expand Down
1 change: 1 addition & 0 deletions backend/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"slack": {
"maxRetrospectInSeconds": 3600
},
"slackNotifier": {},
"google": {},
"discord": {
"maxRetrospectInSeconds": 3600
Expand Down
215 changes: 215 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"formidable-serverless": "1.1.1",
"he": "^1.2.0",
"helmet": "4.1.1",
"html-to-mrkdwn-ts": "^1.1.0",
"html-to-text": "^8.2.1",
"json2csv": "^5.0.7",
"jsonwebtoken": "8.5.1",
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/apiResponseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class ApiResponseHandler extends LoggingBase {
method: req.method,
query: error.sql,
body: req.body,
errorMessage: error.original.message,
errorMessage: error.original?.message,
},
'Database error while processing REST API request!',
)
Expand Down
1 change: 1 addition & 0 deletions backend/src/api/auth/authMe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default async (req, res) => {
activityTypes: await SettingsRepository.buildActivityTypes(
tenantUser.tenant.settings[0].dataValues,
),
slackWebHook: !!tenantUser.tenant.settings[0].dataValues.slackWebHook,
}

return tenantUser
Expand Down
18 changes: 18 additions & 0 deletions backend/src/api/automation/automationSlackCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Axios from 'axios'
import Permissions from '../../security/permissions'
import PermissionChecker from '../../services/user/permissionChecker'
import SettingsService from '../../services/settingsService'

export default async (req, res) => {
new PermissionChecker(req).validateHas(Permissions.values.automationCreate)
const { redirectUrl } = JSON.parse(Buffer.from(req.query.state, 'base64').toString())

const { url } = req.account

await SettingsService.save({ slackWebHook: url }, req)
await Axios.post(url, {
text: 'Crowd.dev Notifier has been successfully connected.',
})

res.redirect(redirectUrl)
}
20 changes: 20 additions & 0 deletions backend/src/api/automation/automationSlackConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import passport from 'passport'
import Permissions from '../../security/permissions'
import PermissionChecker from '../../services/user/permissionChecker'
import { getSlackNotifierStrategy } from '../../services/auth/passportStrategies/slackStrategy'

export default async (req, res, next) => {
new PermissionChecker(req).validateHas(Permissions.values.automationCreate)
const state = {
tenantId: req.params.tenantId,
redirectUrl: req.query.redirectUrl,
crowdToken: req.query.crowdToken,
}

const authenticator = passport.authenticate(getSlackNotifierStrategy(), {
scope: ['incoming-webhook'],
state: Buffer.from(JSON.stringify(state)).toString('base64'),
})

authenticator(req, res, next)
}
Loading

0 comments on commit 3d953d3

Please sign in to comment.