Skip to content

Commit d44e1ee

Browse files
committed
fix: 🐛 Oops, fixed plan logic
1 parent a1bb62a commit d44e1ee

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

src/context.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export function getDefaultBranch(ctx: any) {
2626
return ctx.payload.repository.default_branch
2727
}
2828

29+
export function isOrgRepo(ctx: any) {
30+
const {repository} = ctx.payload;
31+
return repository.owner.type === 'Organization';
32+
}
33+
2934
export function getIssueLabels(ctx: any) {
3035
const labels = ctx.payload.issue.labels
3136
return labels.map((l: { name: string }) => l.name)

src/plans.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Context, Probot} from "probot";
2-
import {getRepoOwnerId, getRepoOwnerLogin} from "./context";
2+
import {getRepoOwnerId, getRepoOwnerLogin, isOrgRepo} from "./context";
33
import {showFreePlanWarning} from "./config";
44
import {addComment} from "./github";
55
import {Config} from "./entities/Config";
@@ -12,11 +12,41 @@ export async function hasValidSubscription(app: Probot, ctx: Context<any>, confi
1212
if (isFreePaidSubscription(app, ctx)) {
1313
return true;
1414
}
15+
if (isOrgRepo(ctx)) {
16+
const hasPaidPlan = await isCommercialOrganizationPlan(app, ctx)
17+
if (hasPaidPlan) {
18+
return true;
19+
} else {
20+
await buyCommercialOrganizationPlanComment(ctx, config);
21+
app.log.info('Added comment to buy Commercial organization 🙏 plan');
22+
return false;
23+
}
24+
}
1525
if (await isPaidPlan(app, ctx)) {
1626
return true;
27+
} else {
28+
await freePlanWarningComment(ctx, config);
29+
return true;
30+
}
31+
}
32+
33+
export async function isCommercialOrganizationPlan(app: Probot, ctx: Context<any>) {
34+
try {
35+
const login = getRepoOwnerLogin(ctx);
36+
app.log.info(`Checking Marketplace for organization: https://github.com/${login} ...`);
37+
const id = getRepoOwnerId(ctx);
38+
const res = await ctx.octokit.apps.getSubscriptionPlanForAccount({account_id: id});
39+
const purchase = res.data.marketplace_purchase;
40+
if (purchase.plan && purchase.plan.name === 'Commercial organization') {
41+
app.log.info('Found Commercial organization 💰 plan');
42+
return true;
43+
} else {
44+
return false;
45+
}
46+
} catch (error) {
47+
app.log.info('Marketplace purchase not found');
48+
return false;
1749
}
18-
await displayFreePlanWarning(ctx, config);
19-
return false;
2050
}
2151

2252
export async function isPaidPlan(app: Probot, ctx: Context<any>) {
@@ -39,7 +69,19 @@ export async function isPaidPlan(app: Probot, ctx: Context<any>) {
3969
}
4070
}
4171

42-
export async function displayFreePlanWarning(ctx: Context<any>, config: Config) {
72+
async function buyCommercialOrganizationPlanComment(ctx: Context<any>, config: Config) {
73+
let buyComment = '';
74+
buyComment += 'Hi there :wave:\n\n';
75+
buyComment += 'Using this App for an organization repository requires a paid ';
76+
buyComment += 'subscription that you can buy on the ';
77+
buyComment += '[GitHub Marketplace](https://github.com/marketplace/create-issue-branch)\n\n';
78+
buyComment += 'If you are a non-profit organization or otherwise can not pay for such a plan, contact me by ';
79+
buyComment += '[creating an issue](https://github.com/robvanderleek/create-issue-branch/issues)';
80+
config.silent = false;
81+
await addComment(ctx, config, buyComment)
82+
}
83+
84+
async function freePlanWarningComment(ctx: Context<any>, config: Config) {
4385
if (showFreePlanWarning(config)) {
4486
let freePlanWarning = '';
4587
freePlanWarning += 'Hi there :wave:\n\n';

src/webhooks/issue-assigned.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
} from "../github";
1414
import {isRunningInGitHubActions, logMemoryUsage} from "../utils";
1515
import {setOutput} from "@actions/core";
16-
import {displayFreePlanWarning} from "../plans";
1716

1817
export async function issueAssigned(app: Probot, ctx: Context<any>) {
1918
app.log.debug('Issue was assigned');

tests/context.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
getIssueLabels,
88
getMilestoneNumber,
99
getRepoOwnerLogin,
10-
getSender
10+
getSender,
11+
isOrgRepo
1112
} from "../src/context";
1213

1314
test('get owner', () => {
@@ -30,10 +31,24 @@ test('get assignee from event', () => {
3031
})
3132

3233
test('get sender', () => {
33-
const ctx = {payload: issueCommentCreatedPayload}
34+
const ctx = {payload: issueCommentCreatedPayload};
3435

35-
expect(getSender(ctx)).toBe('robvanderleek')
36-
})
36+
expect(getSender(ctx)).toBe('robvanderleek');
37+
});
38+
39+
test('is private Org repo', () => {
40+
expect(isOrgRepo({payload: issueAssignedPayload})).toBeFalsy();
41+
42+
const payloadCopy = JSON.parse(JSON.stringify(issueAssignedPayload));
43+
44+
payloadCopy.repository.private = true;
45+
46+
expect(isOrgRepo({payload: payloadCopy})).toBeFalsy();
47+
48+
payloadCopy.repository.owner.type = 'Organization';
49+
50+
expect(isOrgRepo({payload: payloadCopy})).toBeTruthy();
51+
});
3752

3853
test('get Issue description', () => {
3954
const ctx = {payload: issueOpenedPayload}

0 commit comments

Comments
 (0)