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: create reward plugin #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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"

Amit0617 marked this conversation as resolved.
Show resolved Hide resolved
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 }}
39 changes: 38 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,49 @@
"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."
Amit0617 marked this conversation as resolved.
Show resolved Hide resolved
}
},
"configuration": {
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) {
Amit0617 marked this conversation as resolved.
Show resolved Hide resolved
const {
logger,
payload,
octokit,
// config: { configurableResponse, },
} = context;
const contributors: Record<string, number> = {};

const sender = payload.comment.user?.login;

Check failure on line 12 in src/handlers/contrib-reward.ts

View workflow job for this annotation

GitHub Actions / build

Property 'comment' does not exist on type '({ action: "created"; comment: { author_association: "COLLABORATOR" | "CONTRIBUTOR" | "FIRST_TIMER" | "FIRST_TIME_CONTRIBUTOR" | "MANNEQUIN" | "MEMBER" | "NONE" | "OWNER"; body: string; ... 9 more ...; user: { ...; } | null; }; ... 4 more ...; sender: { ...; }; } & { ...; }) | ... 40 more ... | ({ ...; } & { ...; })'.

Check failure on line 12 in src/handlers/contrib-reward.ts

View workflow job for this annotation

GitHub Actions / build

Property 'comment' does not exist on type '({ action: "created"; comment: { author_association: "COLLABORATOR" | "CONTRIBUTOR" | "FIRST_TIMER" | "FIRST_TIME_CONTRIBUTOR" | "MANNEQUIN" | "MEMBER" | "NONE" | "OWNER"; body: string; ... 9 more ...; user: { ...; } | null; }; ... 4 more ...; sender: { ...; }; } & { ...; }) | ... 40 more ... | ({ ...; } & { ...; })'.
const repo = payload.repository.name;
const issueNumber = payload.issue.number;

Check failure on line 14 in src/handlers/contrib-reward.ts

View workflow job for this annotation

GitHub Actions / build

Property 'issue' does not exist on type '({ action: "created"; comment: { author_association: "COLLABORATOR" | "CONTRIBUTOR" | "FIRST_TIMER" | "FIRST_TIME_CONTRIBUTOR" | "MANNEQUIN" | "MEMBER" | "NONE" | "OWNER"; body: string; ... 9 more ...; user: { ...; } | null; }; ... 4 more ...; sender: { ...; }; } & { ...; }) | ... 40 more ... | ({ ...; } & { ...; })'.

Check failure on line 14 in src/handlers/contrib-reward.ts

View workflow job for this annotation

GitHub Actions / build

Property 'issue' does not exist on type '({ action: "created"; comment: { author_association: "COLLABORATOR" | "CONTRIBUTOR" | "FIRST_TIMER" | "FIRST_TIME_CONTRIBUTOR" | "MANNEQUIN" | "MEMBER" | "NONE" | "OWNER"; body: string; ... 9 more ...; user: { ...; } | null; }; ... 4 more ...; sender: { ...; }; } & { ...; }) | ... 40 more ... | ({ ...; } & { ...; })'.
const owner = payload.repository.owner.login;
const body = payload.comment.body;

Check failure on line 16 in src/handlers/contrib-reward.ts

View workflow job for this annotation

GitHub Actions / build

Property 'comment' does not exist on type '({ action: "created"; comment: { author_association: "COLLABORATOR" | "CONTRIBUTOR" | "FIRST_TIMER" | "FIRST_TIME_CONTRIBUTOR" | "MANNEQUIN" | "MEMBER" | "NONE" | "OWNER"; body: string; ... 9 more ...; user: { ...; } | null; }; ... 4 more ...; sender: { ...; }; } & { ...; }) | ... 40 more ... | ({ ...; } & { ...; })'.

Check failure on line 16 in src/handlers/contrib-reward.ts

View workflow job for this annotation

GitHub Actions / build

Property 'comment' does not exist on type '({ action: "created"; comment: { author_association: "COLLABORATOR" | "CONTRIBUTOR" | "FIRST_TIMER" | "FIRST_TIME_CONTRIBUTOR" | "MANNEQUIN" | "MEMBER" | "NONE" | "OWNER"; body: string; ... 9 more ...; user: { ...; } | null; }; ... 4 more ...; sender: { ...; }; } & { ...; }) | ... 40 more ... | ({ ...; } & { ...; })'.

if (!body.match(/rewards/i)) {
Amit0617 marked this conversation as resolved.
Show resolved Hide resolved
logger.error(`Invalid use of slash command, use "/rewards".`, { body });
Copy link

Choose a reason for hiding this comment

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

I don't understand this entire conditional block's purpose.

Copy link
Owner Author

Choose a reason for hiding this comment

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

SInce, I used hello-world.ts file as reference, I thought may be this would help in some error handling and just imitated as it is.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Removed in the next commit.

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;

Check failure on line 34 in src/handlers/contrib-reward.ts

View workflow job for this annotation

GitHub Actions / build

Property 'actor' does not exist on type '{ id: number; node_id: string; url: string; actor: { name?: string | null | undefined; email?: string | null | undefined; login: string; id: number; node_id: string; avatar_url: string; gravatar_id: string | null; ... 13 more ...; starred_at?: string | undefined; }; ... 5 more ...; label: { ...; }; } | ... 20 more ....'.

Check failure on line 34 in src/handlers/contrib-reward.ts

View workflow job for this annotation

GitHub Actions / build

Property 'actor' does not exist on type '{ id: number; node_id: string; url: string; actor: { name?: string | null | undefined; email?: string | null | undefined; login: string; id: number; node_id: string; avatar_url: string; gravatar_id: string | null; ... 13 more ...; starred_at?: string | undefined; }; ... 5 more ...; label: { ...; }; } | ... 20 more ....'.
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,

Check failure on line 44 in src/handlers/contrib-reward.ts

View workflow job for this annotation

GitHub Actions / build

Property 'issue' does not exist on type '({ action: "created"; comment: { author_association: "COLLABORATOR" | "CONTRIBUTOR" | "FIRST_TIMER" | "FIRST_TIME_CONTRIBUTOR" | "MANNEQUIN" | "MEMBER" | "NONE" | "OWNER"; body: string; ... 9 more ...; user: { ...; } | null; }; ... 4 more ...; sender: { ...; }; } & { ...; }) | ... 40 more ... | ({ ...; } & { ...; })'.

Check failure on line 44 in src/handlers/contrib-reward.ts

View workflow job for this annotation

GitHub Actions / build

Property 'issue' does not exist on type '({ action: "created"; comment: { author_association: "COLLABORATOR" | "CONTRIBUTOR" | "FIRST_TIMER" | "FIRST_TIME_CONTRIBUTOR" | "MANNEQUIN" | "MEMBER" | "NONE" | "OWNER"; body: string; ... 9 more ...; user: { ...; } | null; }; ... 4 more ...; sender: { ...; }; } & { ...; }) | ... 40 more ... | ({ ...; } & { ...; })'.
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`);
}
4 changes: 2 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { returnDataToKernel } from "./helpers/validator";
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";

/**
Expand All @@ -13,7 +13,7 @@ export async function runPlugin(context: Context) {
const { logger, eventName } = context;

if (isIssueCommentEvent(context)) {
return await helloWorld(context);
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
Loading