-
Notifications
You must be signed in to change notification settings - Fork 183
/
get-github-configuration.js
56 lines (51 loc) · 2.1 KB
/
get-github-configuration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const JWT = require('atlassian-jwt')
const { Installation } = require('../models')
module.exports = async (req, res, next) => {
if (!req.session.githubToken || !req.session.jiraHost) {
return next(new Error('GitHub token or jiraHost missing'))
}
const { github, client, isAdmin } = res.locals
async function getInstallationsWithAdmin ({installations, login}) {
const installationsWithAdmin = []
for (const installation of installations) {
// See if we can get the membership for this user
const admin = await isAdmin({
org: installation.account.login,
username: login,
type: installation.target_type
})
const hasMemberPermission = installation.permissions.members === 'read'
installationsWithAdmin.push({...installation, admin, hasMemberPermission})
}
return installationsWithAdmin
}
if (req.query.jwt && req.query.xdm_e) {
const { jwt: token, xdm_e: jiraHost } = req.query
const { data: { login } } = await github.users.get()
try {
// we can get the jira client Key from the JWT's `iss` property
// so we'll decode the JWT here and verify it's the right key before continuing
const installation = await Installation.getForHost(jiraHost)
const { iss: clientKey } = JWT.decode(token, installation.sharedSecret)
const { data: { installations } } = (await github.users.getInstallations({}))
const installationsWithAdmin = await getInstallationsWithAdmin({installations, login})
const { data: info } = (await client.apps.get({}))
return res.render('github-configuration.hbs', {
csrfToken: req.csrfToken(),
installations: installationsWithAdmin,
info,
jiraHost,
clientKey
})
} catch (err) {
// If we get here, there was either a problem decoding the JWT
// or getting the data we need from GitHub, so we'll show the user an error.
req.log.error(err)
return next(err)
}
} else {
return res.redirect(
`${req.session.jiraHost}/plugins/servlet/upm/marketplace/plugins/com.github.integration.production`
)
}
}