Skip to content

Commit

Permalink
Merge pull request #195 from gentlementlegen/feat/reward-limit
Browse files Browse the repository at this point in the history
feat: reward limit
  • Loading branch information
gentlementlegen authored Nov 29, 2024
2 parents 74efe55 + 950ff17 commit 9981d32
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"default": true,
"type": "boolean"
},
"limitRewards": {
"default": true,
"description": "Should the rewards of non-assignees be limited to the task reward?",
"type": "boolean"
},
"contentEvaluator": {
"default": null,
"anyOf": [
Expand Down
26 changes: 21 additions & 5 deletions src/parser/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,28 @@ export class Processor {
return this;
}

_getRewardsLimit() {
let taskReward = Infinity;
if (!this._configuration.limitRewards) {
return taskReward;
}
for (const item of Object.keys(this._result)) {
if (this._result[item].task) {
taskReward = this._result[item].task.reward * this._result[item].task.multiplier;
return taskReward;
}
}
return taskReward;
}

async run(data: Readonly<IssueActivity>) {
for (const transformer of this._transformers) {
if (transformer.enabled) {
this._result = await transformer.transform(data, this._result);
}
// Aggregate total result
for (const item of Object.keys(this._result)) {
this._result[item].total = this._sumRewards(this._result[item]);
this._result[item].total = this._sumRewards(this._result[item], this._getRewardsLimit());
}
}
return this._result;
Expand All @@ -51,21 +65,23 @@ export class Processor {
const { file } = this._configuration;
const result = JSON.stringify(this._result, typeReplacer, 2);
if (!file) {
this._context.logger.debug(result);
this._context.logger.verbose(result);
} else {
fs.writeFileSync(file, result);
}
return result;
}

_sumRewards(obj: Record<string, unknown>) {
_sumRewards(obj: Record<string, unknown>, taskRewardLimit = Infinity) {
let totalReward = new Decimal(0);

for (const [key, value] of Object.entries(obj)) {
if (key === "reward" && typeof value === "number") {
totalReward = totalReward.add(value);
totalReward = totalReward.add(Math.min(value, taskRewardLimit));
} else if (typeof value === "object") {
totalReward = totalReward.add(this._sumRewards(value as Record<string, unknown>));
totalReward = totalReward.add(
Math.min(this._sumRewards(value as Record<string, unknown>, taskRewardLimit), taskRewardLimit)
);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/types/plugin-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export const pluginSettingsSchema = T.Object(
* If set to false, the plugin runs even if the price label is missing, and will evaluate comments.
*/
requirePriceLabel: T.Boolean({ default: true }),
limitRewards: T.Boolean({
default: true,
description: "Should the rewards of non-assignees be limited to the task reward?",
}),
contentEvaluator: T.Union([contentEvaluatorConfigurationType, T.Null()], { default: null }),
userExtractor: T.Union([userExtractorConfigurationType, T.Null()], { default: null }),
dataPurge: T.Union([dataPurgeConfigurationType, T.Null()], { default: null }),
Expand Down
8 changes: 6 additions & 2 deletions src/web/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const baseApp = createPlugin<PluginSettings, EnvConfig, SupportedEvents>(

const app = {
fetch: async (request: Request, env: object, ctx: ExecutionContext) => {
if (request.method === "POST" && new URL(request.url).pathname === "/") {
if (
request.method === "POST" &&
new URL(request.url).pathname === "/" &&
request.headers.get("origin") === "http://localhost:4000"
) {
try {
const originalBody = await request.json();
const modifiedBody = await getPayload(
Expand Down Expand Up @@ -108,7 +112,7 @@ app.get("/openai/*", () => {
return Response.json("OpenAI GET");
});

const port = 3000;
const port = 4000;

export default {
fetch: app.fetch,
Expand Down
3 changes: 2 additions & 1 deletion src/web/api/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function getPayload(ownerRepo: string, issueId: number, useOpenAi:

if (!useOpenAi) {
cfgFile.incentives.contentEvaluator.openAi = {
endpoint: "http://localhost:3000/openai",
endpoint: "http://localhost:4000/openai",
};
}

Expand All @@ -21,6 +21,7 @@ export async function getPayload(ownerRepo: string, issueId: number, useOpenAi:
eventName: "issues.closed",
action: "closed",
env: process.env,
command: null,
settings: {
...cfgFile,
evmPrivateEncrypted: cfgFile.evmPrivateEncrypted ?? process.env.EVM_PRIVATE_ENCRYPTED,
Expand Down
2 changes: 1 addition & 1 deletion src/web/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function Form() {
useCache,
};
try {
const result = await fetch("http://localhost:3000", {
const result = await fetch("http://localhost:4000", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
63 changes: 63 additions & 0 deletions tests/process.issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,67 @@ describe("Modules tests", () => {
const postBody = await githubCommentModule.getBodyContent(githubCommentAltResults as unknown as Result);
expect(postBody).not.toContain("whilefoo");
});

describe("Reward limits", () => {
it("Should return infinity if disabled", () => {
const processor = new Processor({
...ctx,
config: {
...ctx.config,
incentives: {
...ctx.config.incentives,
limitRewards: false,
},
},
});
const result = processor._getRewardsLimit();
expect(result).toBe(Infinity);
});
});

it("Should return the max corresponding to the label of the issue if enabled", async () => {
const processor = new Processor({
...ctx,
config: {
...ctx.config,
incentives: {
...ctx.config.incentives,
limitRewards: true,
},
},
});
processor["_transformers"] = [
new UserExtractorModule(ctx),
new DataPurgeModule(ctx),
new FormattingEvaluatorModule(ctx),
new ContentEvaluatorModule(ctx),
];
processor["_result"] = {
user1: {
total: 999,
task: {
multiplier: 0.5,
reward: 18.5,
},
userId: 0,
},
user2: {
total: 11111111,
userId: 1,
},
};
const result = processor._getRewardsLimit();
expect(result).toBe(9.25);
const total = await processor.run(activity);
expect(total).toMatchObject({
user1: { total: 9.25, task: { multiplier: 0.5, reward: 18.5 }, userId: 0 },
user2: { total: 0, userId: 1 },
"0x4007": {
total: 9.25,
},
whilefoo: {
total: 9.25,
},
});
});
});

0 comments on commit 9981d32

Please sign in to comment.