Skip to content

Commit

Permalink
fix: πŸ› Oops, fixed plan logic
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Jan 14, 2025
1 parent a1bb62a commit d44e1ee
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export function getDefaultBranch(ctx: any) {
return ctx.payload.repository.default_branch
}

export function isOrgRepo(ctx: any) {
const {repository} = ctx.payload;
return repository.owner.type === 'Organization';
}

export function getIssueLabels(ctx: any) {
const labels = ctx.payload.issue.labels
return labels.map((l: { name: string }) => l.name)
Expand Down
50 changes: 46 additions & 4 deletions src/plans.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Context, Probot} from "probot";
import {getRepoOwnerId, getRepoOwnerLogin} from "./context";
import {getRepoOwnerId, getRepoOwnerLogin, isOrgRepo} from "./context";
import {showFreePlanWarning} from "./config";
import {addComment} from "./github";
import {Config} from "./entities/Config";
Expand All @@ -12,11 +12,41 @@ export async function hasValidSubscription(app: Probot, ctx: Context<any>, confi
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;
}
await displayFreePlanWarning(ctx, config);
return false;
}

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

export async function displayFreePlanWarning(ctx: Context<any>, config: Config) {
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';
Expand Down
1 change: 0 additions & 1 deletion src/webhooks/issue-assigned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from "../github";
import {isRunningInGitHubActions, logMemoryUsage} from "../utils";
import {setOutput} from "@actions/core";
import {displayFreePlanWarning} from "../plans";

export async function issueAssigned(app: Probot, ctx: Context<any>) {
app.log.debug('Issue was assigned');
Expand Down
23 changes: 19 additions & 4 deletions tests/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
getIssueLabels,
getMilestoneNumber,
getRepoOwnerLogin,
getSender
getSender,
isOrgRepo
} from "../src/context";

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

test('get sender', () => {
const ctx = {payload: issueCommentCreatedPayload}
const ctx = {payload: issueCommentCreatedPayload};

expect(getSender(ctx)).toBe('robvanderleek')
})
expect(getSender(ctx)).toBe('robvanderleek');
});

test('is private Org repo', () => {
expect(isOrgRepo({payload: issueAssignedPayload})).toBeFalsy();

const payloadCopy = JSON.parse(JSON.stringify(issueAssignedPayload));

payloadCopy.repository.private = true;

expect(isOrgRepo({payload: payloadCopy})).toBeFalsy();

payloadCopy.repository.owner.type = 'Organization';

expect(isOrgRepo({payload: payloadCopy})).toBeTruthy();
});

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

0 comments on commit d44e1ee

Please sign in to comment.