-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathplans.ts
147 lines (139 loc) Β· 6.53 KB
/
plans.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {Context, Probot} from "probot";
import {getRepoOwnerId, getRepoOwnerLogin, isOrgRepo} from "./context";
import {showFreePlanWarning} from "./config";
import {addComment} from "./github";
import {Config} from "./entities/Config";
import {isRunningInGitHubActions, isRunningInTestEnvironment} from "./utils";
export async function hasValidSubscription(app: Probot, ctx: Context<any>, config: Config) {
if (isRunningInGitHubActions() || isRunningInTestEnvironment()) {
return true;
}
if (isFreePaidSubscription(app, ctx)) {
return true;
}
if (isOrgRepo(ctx)) {
const hasPaidPlan = await isCommercialOrganizationPlan(app, ctx)
if (hasPaidPlan) {
return true;
} else {
await buyCommercialOrganizationPlanComment(ctx, config);
app.log.info('Added comment to buy Commercial organization π plan');
return false;
}
}
if (await isPaidPlan(app, ctx)) {
return true;
} else {
await freePlanWarningComment(ctx, config);
return true;
}
}
export async function isCommercialOrganizationPlan(app: Probot, ctx: Context<any>) {
try {
const login = getRepoOwnerLogin(ctx);
app.log.info(`Checking Marketplace for organization: https://github.com/${login} ...`);
const id = getRepoOwnerId(ctx);
const res = await ctx.octokit.apps.getSubscriptionPlanForAccount({account_id: id});
const purchase = res.data.marketplace_purchase;
if (purchase.plan && purchase.plan.name === 'Commercial organization') {
app.log.info('Found Commercial organization π° plan');
return true;
} else {
return false;
}
} catch (error) {
app.log.info('Marketplace purchase not found');
return false;
}
}
export async function isPaidPlan(app: Probot, ctx: Context<any>) {
try {
const login = getRepoOwnerLogin(ctx);
app.log.info(`Checking Marketplace for organization: https://github.com/${login} ...`);
const id = getRepoOwnerId(ctx);
const res = await ctx.octokit.apps.getSubscriptionPlanForAccount({account_id: id});
const purchase = res.data.marketplace_purchase;
if (purchase.plan && purchase.plan.price_model === 'FREE') {
app.log.info('Found Free plan');
return false;
} else {
app.log.info('Found paid π° plan');
return true;
}
} catch (error) {
app.log.info('Marketplace purchase not found');
return false;
}
}
async function buyCommercialOrganizationPlanComment(ctx: Context<any>, config: Config) {
let buyComment = '';
buyComment += 'Hi there :wave:\n\n';
buyComment += 'Using this App for an organization repository requires a paid ';
buyComment += 'subscription that you can buy on the ';
buyComment += '[GitHub Marketplace](https://github.com/marketplace/create-issue-branch)\n\n';
buyComment += 'If you are a non-profit organization or otherwise can not pay for such a plan, contact me by ';
buyComment += '[creating an issue](https://github.com/robvanderleek/create-issue-branch/issues)';
config.silent = false;
await addComment(ctx, config, buyComment)
}
async function freePlanWarningComment(ctx: Context<any>, config: Config) {
if (showFreePlanWarning(config)) {
let freePlanWarning = '';
freePlanWarning += 'Hi there :wave:\n\n';
freePlanWarning += 'You are using the free plan of the Create Issue Branch App.\n';
freePlanWarning += 'Due to its popularity, offering the App for free is becoming too costly for me.\n';
freePlanWarning += 'The free plan is therefore going to be deprecated on March 1st, 2025.\n\n';
freePlanWarning += 'If you want to continue using this App, please upgrade to the Developer plan.\n';
freePlanWarning += 'The Developer plan costs only $1 per month (or $10 yearly).\nYou can upgrade on the ';
freePlanWarning += '[GitHub Marketplace](https://github.com/marketplace/create-issue-branch)\n\n';
freePlanWarning += 'If you have any questions reach out to me by ';
freePlanWarning += '[opening an issue](https://github.com/robvanderleek/create-issue-branch/issues).\n';
freePlanWarning += 'To disable this message, insert `freePlanWarning: false` in your configuration YAML.\n';
config.silent = false;
await addComment(ctx, config, freePlanWarning);
}
}
export function isFreePaidSubscription(app: Probot, ctx: Context<any>): boolean {
const login = getRepoOwnerLogin(ctx)
const logins = ['PWrInSpace', 'KPLRCDBS', 'codemeistre', 'RaspberryPiFoundation', 'astro-pi',
'LOG680-01-Equipe-09', 'New-AutoMotive', 'EpitechMscPro2020', 'snaphu-msu', 'SerenKodi', 'oyunprotocol',
'web-illinois', 'PathologyDataScience', 'miranhas-github', 'DHBW-FN', 'lecoindesdevs', 'getcodelimit'];
const match = logins.find(o => o.toLowerCase() === login.toLowerCase());
if (match !== undefined) {
app.log.info('Found free β€οΈ paid plan');
return true;
} else {
return false;
}
}
export async function listAppSubscriptions(app: any) {
let result = ''
const plans = (await app.state.octokit.apps.listPlans()).data
for (const plan of plans) {
if (plan.price_model === 'FLAT_RATE') {
const accounts = await app.state.octokit.paginate(app.state.octokit.apps.listAccountsForPlan,
{per_page: 100, plan_id: plan.id}, (response: any) => response.data)
result += `Subscriptions for plan: ${plan.name}\n`
result += '--------------------------------------------------\n'
result += listFreeTrialAccounts(app, accounts)
result += '--------------------------------------------------\n'
result += `Total: ${accounts.length}\n`
}
}
return result
}
function listFreeTrialAccounts(app: Probot, accounts: Array<any>) {
let result = ''
for (const account of accounts) {
const purchase = account.marketplace_purchase
const pendingChange = account.marketplace_pending_change
if (pendingChange || purchase.on_free_trial) {
result +=
`Org: ${account.login}, free trial: ${purchase.on_free_trial}, billing_cycle: ${purchase.billing_cycle}, ` +
`pending change to plan: ${pendingChange.plan.name} on: ${pendingChange.effective_date}\n`
} else {
result += `Org: ${account.login}, billing_cycle: ${purchase.billing_cycle}\n`
}
}
return result
}