-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-plan.js
43 lines (36 loc) · 1.21 KB
/
get-plan.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
module.exports = getPlan;
// Find out if user as pro plan or not. The request to check the installation
// for the current account (user account_id or organization) needs to be
// authenticated as the app, not installation. If the app has no plan it means
// that it wasn’t installed from the marketplace but from github.com/app/wip.
// We treat it these as "FREE".
//
// The plan can be overwritten to "Pro" by adding an account name to pro-plan-for-free.js
const PRO_PLAN_FOR_FREE = require("../pro-plan-for-free");
async function getPlan(robot, owner) {
if (PRO_PLAN_FOR_FREE.includes(owner.login)) {
return "pro";
}
// For GitHub Enterprise Server (GHES), always return 'pro'
// This is because /marketplace_listing API routes
// are not available on GHES
if (process.env.GHE_HOST) {
return "pro";
}
const authenticatedAsApp = await robot.auth();
try {
const {
data: {
marketplace_purchase: { plan },
},
} = await authenticatedAsApp.apps.checkAccountIsAssociatedWithAny({
account_id: owner.id,
});
return plan.price_model === "FREE" ? "free" : "pro";
} catch (error) {
if (error.status === 404) {
return "free";
}
throw error;
}
}