Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #910 from pavlovcik/refactor/move-to-delegated-com…
Browse files Browse the repository at this point in the history
…pute

Refactor/move to delegated compute
  • Loading branch information
0x4007 authored Jan 16, 2024
2 parents 3ee39e0 + 85177d0 commit 231b6e4
Show file tree
Hide file tree
Showing 60 changed files with 21,658 additions and 15,909 deletions.
13 changes: 2 additions & 11 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
APP_ID=
APP_PRIVATE_KEY=
PRIVATE_KEY=

# Go to https://smee.io/new set this to the URL that you are redirected to.
WEBHOOK_PROXY_URL=
WEBHOOK_SECRET=

SUPABASE_URL=
SUPABASE_KEY=
AUTO_PAY_MODE=
ANALYTICS_MODE=


# Use `trace` to get verbose logging or `info` to show less
# `fatal` `error` `info` `verbose` `debug`
LOG_LEVEL=debug
OPENAI_API_HOST=https://api.openai.com
OPENAI_API_KEY=
CHATGPT_USER_PROMPT_FOR_IMPORTANT_WORDS="I need your help to find important words (e.g. unique adjectives) from github issue below and I want to parse them easily so please separate them using #(No other contexts needed). Please separate the words by # so I can parse them easily. Please answer simply as I only need the important words. Here is the issue content.\n"
CHATGPT_USER_PROMPT_FOR_MEASURE_SIMILARITY='I have two github issues and I need to measure the possibility of the 2 issues are the same content (No other contents needed and give me only the number in %).\n Give me in number format and add % after the number.\nDo not tell other things since I only need the number (e.g. 85%). Here are two issues:\n 1. "%first%"\n2. "%second%"'
SIMILARITY_THRESHOLD=80
MEASURE_SIMILARITY_AI_TEMPERATURE=0
IMPORTANT_WORDS_AI_TEMPERATURE=0
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ on:
jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: "18.16.0"
node-version: "20.10.0"

- name: Install
run: yarn install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
# MEASURE_SIMILARITY_AI_TEMPERATURE:
# OPENAI_API_HOST:
# OPENAI_API_KEY:
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
# SIMILARITY_THRESHOLD:
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ yarn start:watch
- `WEBHOOK_PROXY_URL`: (required) should be automatically filled when you install Ubiquibot
- `WEBHOOK_SECRET`: (required) should be automatically filled when the app is installed

`APP_ID` and `APP_PRIVATE_KEY` are [here](https://t.me/c/1588400061/1627) for core team developers to use.
`APP_ID` and `PRIVATE_KEY` are [here](https://t.me/c/1588400061/1627) for core team developers to use.

If you are an external developer, `APP_ID`and `APP_PRIVATE_KEY` are automatically generated when you install the app on your repository.
If you are an external developer, `APP_ID`and `PRIVATE_KEY` are automatically generated when you install the app on your repository.

## How to run locally

Expand Down Expand Up @@ -258,7 +258,7 @@ DISQUALIFY_TIME="7 days" // 7 days
- in another instance run `yarn start:watch` (runs the bot locally)
5. Open `http://localhost:3000` and follow instructions to add the bot to one of your repositories.

At this point the `.env` files auto-fill the empty fields (`APP_PRIVATE_KEY` and `APP_ID`) if it is not previously filled.
At this point the `.env` files auto-fill the empty fields (`PRIVATE_KEY` and `APP_ID`) if it is not previously filled.
Now you can make changes to the repository on GitHub (e.g. add a bounty) and the bot should react.

6. After adding the bot (as a installed app) to your github you will need to restart the aforemention yarn start:watch so CTRL-C to stop the node daemon and `yarn start:watch` again
Expand Down
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@
"probot-app"
],
"scripts": {
"inspect": "node --inspect-brk ./dist/main.js",
"build:ci": "ncc build src/adapters/github/github-actions.ts -o ./",
"build:serverless": "ncc build src/index.ts -o ./",
"inspect": "node --inspect node_modules/.bin/probot run ./dist/main.js",
"build:gh-actions": "ncc build src/adapters/github/github-actions.ts -o ./",
"build": "tsc",
"build:watch": "tsc --watch",
"clean": "rimraf ./dist ./node_modules",
"preformat": "lint:prettier",
"format": "lint:eslint",
"lint": "lint:prettier:check && lint:eslint",
"lint:prettier:check": "prettier -c src/**/*.ts",
"lint:prettier": "prettier --write src",
"lint:eslint": "eslint --fix --ext .ts ./src",
"start:serverless": "tsx src/adapters/github/github-actions.ts",
"preformat": "yarn format:prettier",
"format": "yarn format:eslint",
"postformat": "yarn format:cspell",
"format:prettier": "prettier --write src",
"format:eslint": "eslint --fix --ext .ts ./src",
"format:cspell": "cspell --config .cspell.json 'src/**/*.{js,ts,json,md,yml}'",
"start:gh-actions": "tsx src/adapters/github/github-actions.ts",
"start:watch": "nodemon --exec 'yarn start'",
"utils:cspell": "cspell --config .cspell.json 'src/**/*.{js,ts,json,md,yml}'",
"start": "probot run ./dist/main.js",
"prepare": "husky install",
"test": "jest",
"netlify:logs": "yarn netlify logs:function webhooks"
"logs:netlify": "yarn netlify logs:function webhooks"
},
"dependencies": {
"@commitlint/cli": "^17.4.3",
Expand Down Expand Up @@ -71,15 +70,15 @@
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/eslint": "^8.40.2",
"@types/jest": "^29.5.10",
"@types/jest": "^29.5.11",
"@types/jsdom": "^21.1.4",
"@types/libsodium-wrappers": "^0.7.10",
"@types/lodash": "^4.14.197",
"@types/lodash": "^4.14.202",
"@types/markdown-it": "^13.0.4",
"@types/node": "^14.18.37",
"@types/source-map-support": "^0.5.6",
"eslint": "^8.43.0",
"jest": "^29.6.2",
"jest": "^29.7.0",
"knip": "^2.33.4",
"netlify-cli": "^17.10.1",
"octokit": "^3.1.2",
Expand All @@ -89,7 +88,7 @@
"typescript": "^4.9.5"
},
"engines": {
"node": ">=18"
"node": ">=20.10.0"
},
"lint-staged": {
"*.ts": [
Expand All @@ -111,5 +110,6 @@
"verbose": true,
"ext": "ts",
"exec": "yarn start"
}
},
"packageManager": "[email protected]"
}
2 changes: 1 addition & 1 deletion src/adapters/supabase/helpers/tables/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export class Logs {
.then(() => void 0)
.catch(() => Logs.console.fatal("Error adding logs to queue"));

Logs.console.ok(logInsert.log, logInsert);
// Logs.console.ok(logInsert.log, logInsert);
}

static _commentMetaData(metadata: any, level: LogLevel) {
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async function logAnyReturnFromHandlers(context: Context, handlerType: AllHandle
// only log main handler results
await renderMainActionOutput(context, response, action);
} else {
context.logger.ok("Completed", { action: action.name, type: handlerType.type });
// context.logger.ok("Completed", { action: action.name, type: handlerType.type });
}
} catch (report: unknown) {
await renderCatchAllWithContext(report);
Expand Down
20 changes: 19 additions & 1 deletion src/handlers/access/labels-access.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Runtime from "../../bindings/bot-runtime";
import { isUserAdminOrBillingManager, removeLabel, addLabelToIssue, addCommentToIssue } from "../../helpers/issue";
import { isUserAdminOrBillingManager, addLabelToIssue, addCommentToIssue } from "../../helpers/issue";
import { Context } from "../../types/context";
import { UserType } from "../../types/payload";

Expand Down Expand Up @@ -62,3 +62,21 @@ export async function labelAccessPermissionsCheck(context: Context) {
return false;
}
}
async function removeLabel(context: Context, name: string) {
const payload = context.payload;
if (!payload.issue) {
context.logger.debug("Invalid issue object");
return;
}

try {
await context.octokit.issues.removeLabel({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
name: name,
});
} catch (e: unknown) {
context.logger.fatal("Removing label failed!", e);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { getLinkedPullRequests } from "../../helpers/get-linked-issues-and-pull-requests";
import { closePullRequest } from "../../helpers/issue";
import { getLinkedPullRequests } from "../../helpers/get-linked-pull-requests";
import { calculateDurations, calculateLabelValue } from "../../helpers/shared";
import { Context } from "../../types/context";
import { Label } from "../../types/label";
import { GitHubPayload } from "../../types/payload";

export async function startCommandHandler(context: Context) {
export async function assignCommandHandler(context: Context) {
const config = context.config;
const logger = context.logger;
const payload = context.event.payload as GitHubPayload;
Expand Down Expand Up @@ -67,13 +66,11 @@ export async function startCommandHandler(context: Context) {
const currentDate = new Date();
const endDate = new Date(currentDate.getTime() + shortestDurationLabel * 1000);

// Format the commit message
const commitMessage = `${flattenedAssignees} the deadline is at ${endDate.toISOString()}`;
logger.debug("Creating an issue comment", { commitMessage });
// Format the comment
const comment = `${flattenedAssignees} the deadline is at ${endDate.toISOString()}`;

// Add the commit message as a comment to the issue
// await addCommentToIssue(commitMessage, payload.issue?.number);
return logger.info(commitMessage);
// Add the comment to the issue
return comment;
}

export async function closePullRequestForAnIssue(context: Context) {
Expand Down Expand Up @@ -102,3 +99,17 @@ export async function closePullRequestForAnIssue(context: Context) {
return logger.info(comment);
// await addCommentToIssue(comment, payload.issue.number);
}

async function closePullRequest(context: Context, pullNumber: number) {
const payload = context.payload as GitHubPayload;
try {
await context.octokit.rest.pulls.update({
owner: payload.repository.owner.login,
repo: payload.repository.name,
pull_number: pullNumber,
state: "closed",
});
} catch (err: unknown) {
context.logger.fatal("Closing pull requests failed!", err);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getLinkedIssues } from "../../helpers/get-linked-issues-and-pull-requests";
import { addAssignees, getAllPullRequests, getIssueByNumber, getPullByNumber } from "../../helpers/issue";
import axios from "axios";
import { HTMLElement, parse } from "node-html-parser";
import { getAllPullRequests, addAssignees } from "../../helpers/issue";
import { Context } from "../../types/context";

// Check for pull requests linked to their respective issues but not assigned to them
Expand Down Expand Up @@ -58,3 +59,54 @@ export async function checkPullRequests(context: Context) {
}
return logger.debug(`Checking pull requests done!`);
}

export async function getLinkedIssues({ owner, repository, pull }: GetLinkedParams) {
const { data } = await axios.get(`https://github.com/${owner}/${repository}/pull/${pull}`);
const dom = parse(data);
const devForm = dom.querySelector("[data-target='create-branch.developmentForm']") as HTMLElement;
const linkedIssues = devForm.querySelectorAll(".my-1");

if (linkedIssues.length === 0) {
return null;
}

const issueUrl = linkedIssues[0].querySelector("a")?.attrs?.href || null;
return issueUrl;
}

export async function getPullByNumber(context: Context, pull: number) {
const payload = context.payload;

try {
const response = await context.octokit.rest.pulls.get({
owner: payload.repository.owner.login,
repo: payload.repository.name,
pull_number: pull,
});
return response.data;
} catch (err: unknown) {
context.logger.fatal("Fetching pull request failed!", err);
return;
}
}
// Get issues by issue number
export async function getIssueByNumber(context: Context, issueNumber: number) {
const payload = context.payload;
try {
const { data: issue } = await context.octokit.rest.issues.get({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: issueNumber,
});
return issue;
} catch (e: unknown) {
context.logger.fatal("Fetching issue failed!", e);
return;
}
}
export interface GetLinkedParams {
owner: string;
repository: string;
issue?: number;
pull?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GitHubComment, GitHubPayload } from "../../types/payload";
import { commentParser, userCommands } from "./handlers/comment-handler-main";
import { verifyFirstCommentInRepository } from "./handlers/first";

export async function commentCreatedOrEdited(context: Context) {
export async function commentCreated(context: Context) {
const config = context.config,
logger = context.logger,
payload = context.event.payload as GitHubPayload;
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/comment/handlers/ask.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CreateChatCompletionRequestMessage } from "openai/resources/chat";
import { askGPT, decideContextGPT, sysMsg } from "../../../helpers/gpt";
import { getAllIssueComments, getAllLinkedIssuesAndPullsInBody } from "../../../helpers/issue";
import { getAllIssueComments } from "../../../helpers/issue";
import { Context } from "../../../types/context";
import { StreamlinedComment } from "../../../types/openai";
import { GitHubPayload, UserType } from "../../../types/payload";
import { askGPT, decideContextGPT, getAllLinkedIssuesAndPullsInBody, sysMsg } from "./ask/ask-gpt";

export async function ask(context: Context, body: string) {
// The question to ask
Expand Down
Loading

0 comments on commit 231b6e4

Please sign in to comment.