Skip to content

Commit

Permalink
feat(plugins): contributor rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit0617 committed Oct 15, 2024
1 parent e51b641 commit 0064649
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"configuration": {
"default": {
"configurableResponse": "Hello, world! Amit passed from here"
"configurableResponse": "Hello, world! Amit passed from here, testing."
},
"type": "object",
"properties": {
Expand Down
61 changes: 61 additions & 0 deletions src/handlers/contrib-reward.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Context } from "../types";

export async function contribReward(context: Context) {
const {
logger,
payload,
octokit,
// config: { configurableResponse, },
} = context;
const contributors: Record<string, number> = {};

const sender = payload.comment.user?.login;
const repo = payload.repository.name;
const issueNumber = payload.issue.number;
const owner = payload.repository.owner.login;
const body = payload.comment.body;

if (!body.match(/rewards/i)) {
logger.error(`Invalid use of slash command, use "/rewards".`, { body });
return;
}

logger.info("Calculating rewards for contributor.");
logger.debug(`Executing contribReward:`, { sender, repo, issueNumber, owner });

try {
const events = await octokit.issues.listEventsForTimeline({
owner,
repo,
issue_number: issueNumber,
});

events.data.forEach((event) => {
const contributor = event.actor?.login;
if (!contributors[contributor]) {
contributors[contributor] = 0;
}
contributors[contributor]++;
});

await octokit.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
body: JSON.stringify(contributors),
});

logger.info(`Contributors: ${JSON.stringify(contributors)}`);
} catch (error) {
if (error instanceof Error) {
logger.error(`Error calculating rewards:`, { error });
throw error;
} else {
logger.error(`Error calculating rewards:`, { error: new Error(String(error)) });
throw error;
}
}

logger.ok(`Successfully created comment!`);
logger.verbose(`Exiting contribReward`);
}
2 changes: 1 addition & 1 deletion src/types/plugin-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const pluginSettingsSchema = T.Object(
configurableResponse: T.String(),
customStringsUrl: T.Optional(T.String()),
},
{ default: { configurableResponse: "Hello, world! Amit passed from here" } }
{ default: { configurableResponse: "Hello, world! Amit passed from here, testing" } }
);

export const pluginSettingsValidator = new StandardValidator(pluginSettingsSchema);
Expand Down

0 comments on commit 0064649

Please sign in to comment.