Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): rewards plugin #28

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/.ubiquibot-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins:
"issue_comment.created":
- name: "reward plugin name"
command: "/rewards"
skipBotEvents: true
uses:
- plugin: amit0617/contributor-rewards:compute.yml@development
type: github
6 changes: 3 additions & 3 deletions .github/workflows/compute.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "the name of the plugin"
name: "github contributor rewards"

on:
workflow_dispatch:
Expand All @@ -18,7 +18,7 @@ on:

jobs:
compute:
name: "plugin name"
name: "github contributor rewards"
runs-on: ubuntu-latest
permissions: write-all
env:
Expand All @@ -38,7 +38,7 @@ jobs:

- name: execute directive
run: npx tsx ./src/main.ts
id: plugin-name
id: github-contrib-reward
env:
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
41 changes: 39 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,54 @@
"name": "ts-template",
"description": "ts-template for UbiquityOS plugins.",
"ubiquity:listeners": [
"issue_comment.created"
"issue_comment.created",
"issue_comment.deleted",
"issue_comment.edited",
"pull_request.assigned",
"pull_request.auto_merge_disabled",
"pull_request.auto_merge_enabled",
"pull_request.closed",
"pull_request.converted_to_draft",
"pull_request.demilestoned",
"pull_request.dequeued",
"pull_request.edited",
"pull_request.enqueued",
"pull_request.labeled",
"pull_request.locked",
"pull_request.milestoned",
"pull_request.opened",
"pull_request.ready_for_review",
"pull_request.reopened",
"pull_request.review_request_removed",
"pull_request.review_requested",
"pull_request.synchronize",
"pull_request.unassigned",
"pull_request.unlabeled",
"pull_request.unlocked",
"pull_request_review",
"pull_request_review.dismissed",
"pull_request_review.edited",
"pull_request_review.submitted",
"pull_request_review_comment",
"pull_request_review_comment.created",
"pull_request_review_comment.deleted",
"pull_request_review_comment.edited",
"pull_request_review_thread",
"pull_request_review_thread.resolved",
"pull_request_review_thread.unresolved"
],
"commands": {
"command1": {
"ubiquity:example": "/command1 argument",
"description": "Command 1 with an argument."
},
"rewards": {
"description": "Rewards to get calculate for contributors."
}
},
"configuration": {
"default": {
"configurableResponse": "Hello, world!"
"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`);
}
8 changes: 7 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import { Env, PluginInputs } from "./types";
import { Context } from "./types";
import { isIssueCommentEvent } from "./types/typeguards";
import { helloWorld } from "./handlers/hello-world";
import { contribReward } from "./handlers/contrib-reward";
import { LogLevel, Logs } from "@ubiquity-dao/ubiquibot-logger";

/**
* The main plugin function. Split for easier testing.
*/
export async function runPlugin(context: Context) {
const { logger, eventName } = context;
const { logger, eventName, payload } = context;

// let's keep it for testing purposes
if (isIssueCommentEvent(context)) {
return await helloWorld(context);
}

if (eventName === "issue_comment.created" && payload.comment.body.match(/rewards/i)) {
Copy link
Author

@Amit0617 Amit0617 Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limiting scope here isn't meaningful when it is originally desired to be called on all kind of events. Maintainer input needed.

return await contribReward(context);
}

logger.error(`Unsupported event: ${eventName}`);
}

Expand Down
37 changes: 36 additions & 1 deletion src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,42 @@ import { Logs } from "@ubiquity-dao/ubiquibot-logger";
*
* ubiquity:listeners: ["issue_comment.created", ...]
*/
export type SupportedEventsU = "issue_comment.created";
export type SupportedEventsU =
| "issue_comment.created"
| "issue_comment.deleted"
| "issue_comment.edited"
| "pull_request.assigned"
| "pull_request.auto_merge_disabled"
| "pull_request.auto_merge_enabled"
| "pull_request.closed"
| "pull_request.converted_to_draft"
| "pull_request.demilestoned"
| "pull_request.dequeued"
| "pull_request.edited"
| "pull_request.enqueued"
| "pull_request.labeled"
| "pull_request.locked"
| "pull_request.milestoned"
| "pull_request.opened"
| "pull_request.ready_for_review"
| "pull_request.reopened"
| "pull_request.review_request_removed"
| "pull_request.review_requested"
| "pull_request.synchronize"
| "pull_request.unassigned"
| "pull_request.unlabeled"
| "pull_request.unlocked"
| "pull_request_review"
| "pull_request_review.dismissed"
| "pull_request_review.edited"
| "pull_request_review.submitted"
| "pull_request_review_comment"
| "pull_request_review_comment.created"
| "pull_request_review_comment.deleted"
| "pull_request_review_comment.edited"
| "pull_request_review_thread"
| "pull_request_review_thread.resolved"
| "pull_request_review_thread.unresolved";

export type SupportedEvents = {
[K in SupportedEventsU]: K extends WebhookEventName ? WebhookEvent<K> : never;
Expand Down
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!" } }
{ default: { configurableResponse: "Hello, world! Amit passed from here, testing" } }
);
Copy link
Author

@Amit0617 Amit0617 Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For self: don't forget to remove this too.


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