feat: adapt pullfrog for gitea + ollama
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import { type } from "arktype";
|
||||
import { buildPullfrogFooter, stripExistingFooter } from "../utils/buildPullfrogFooter.ts";
|
||||
import { buildShockbotFooter, stripExistingFooter } from "../utils/buildShockbotFooter.ts";
|
||||
import { log } from "../utils/cli.ts";
|
||||
import { fixDoubleEscapedString } from "../utils/fixDoubleEscapedString.ts";
|
||||
import { patchWorkflowRunFields } from "../utils/patchWorkflowRunFields.ts";
|
||||
import { $ } from "../utils/shell.ts";
|
||||
import type { ToolContext } from "./server.ts";
|
||||
import { execute, tool } from "./shared.ts";
|
||||
@@ -11,23 +10,12 @@ export const PullRequest = type({
|
||||
title: type.string.describe("the title of the pull request"),
|
||||
body: type.string.describe("the body content of the pull request"),
|
||||
base: type.string.describe("the base branch to merge into (e.g., 'main')"),
|
||||
"draft?": type.boolean.describe(
|
||||
"if true, create the pull request as a draft. use when the user explicitly asks for a draft PR."
|
||||
),
|
||||
"draft?": type.boolean.describe("if true, create the pull request as a draft."),
|
||||
});
|
||||
|
||||
function buildPrBodyWithFooter(ctx: ToolContext, body: string): string {
|
||||
const footer = buildPullfrogFooter({
|
||||
triggeredBy: true,
|
||||
workflowRun: ctx.runId
|
||||
? { owner: ctx.repo.owner, repo: ctx.repo.name, runId: ctx.runId, jobId: ctx.jobId }
|
||||
: undefined,
|
||||
model: ctx.toolState.model,
|
||||
fallbackFrom: ctx.toolState.modelFallback?.from,
|
||||
});
|
||||
|
||||
const bodyWithoutFooter = stripExistingFooter(fixDoubleEscapedString(body));
|
||||
return `${bodyWithoutFooter}${footer}`;
|
||||
const footer = buildShockbotFooter({ model: ctx.toolState.model });
|
||||
return `${stripExistingFooter(fixDoubleEscapedString(body))}${footer}`;
|
||||
}
|
||||
|
||||
export const UpdatePullRequestBody = type({
|
||||
@@ -41,23 +29,15 @@ export function UpdatePullRequestBodyTool(ctx: ToolContext) {
|
||||
description: "Update the body/description of an existing pull request",
|
||||
parameters: UpdatePullRequestBody,
|
||||
execute: execute(async (params) => {
|
||||
const bodyWithFooter = buildPrBodyWithFooter(ctx, params.body);
|
||||
|
||||
const result = await ctx.octokit.rest.pulls.update({
|
||||
const result = await ctx.gitea.rest.repository.repoEditPullRequest({
|
||||
owner: ctx.repo.owner,
|
||||
repo: ctx.repo.name,
|
||||
pull_number: params.pull_number,
|
||||
body: bodyWithFooter,
|
||||
index: params.pull_number,
|
||||
body: { body: buildPrBodyWithFooter(ctx, params.body) },
|
||||
});
|
||||
log.info(`» updated pull request #${result.data.number}`);
|
||||
|
||||
ctx.toolState.wasUpdated = true;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
number: result.data.number,
|
||||
url: result.data.html_url,
|
||||
};
|
||||
return { success: true, number: result.data.number, url: result.data.html_url };
|
||||
}),
|
||||
});
|
||||
}
|
||||
@@ -68,52 +48,42 @@ export function CreatePullRequestTool(ctx: ToolContext) {
|
||||
description: "Create a pull request from the current branch",
|
||||
parameters: PullRequest,
|
||||
execute: execute(async (params) => {
|
||||
const currentBranch = $("git", ["rev-parse", "--abbrev-ref", "HEAD"], { log: false });
|
||||
log.debug(`Current branch: ${currentBranch}`);
|
||||
const currentBranch = $("git", ["rev-parse", "--abbrev-ref", "HEAD"], { log: false }).trim();
|
||||
|
||||
const bodyWithFooter = buildPrBodyWithFooter(ctx, params.body);
|
||||
|
||||
const result = await ctx.octokit.rest.pulls.create({
|
||||
const result = await ctx.gitea.rest.repository.repoCreatePullRequest({
|
||||
owner: ctx.repo.owner,
|
||||
repo: ctx.repo.name,
|
||||
title: params.title,
|
||||
body: bodyWithFooter,
|
||||
head: currentBranch,
|
||||
base: params.base,
|
||||
draft: params.draft ?? false,
|
||||
body: {
|
||||
title: params.title,
|
||||
body: buildPrBodyWithFooter(ctx, params.body),
|
||||
head: currentBranch,
|
||||
base: params.base,
|
||||
// draft: params.draft ?? false, // not in all Gitea versions
|
||||
},
|
||||
});
|
||||
log.info(`» created pull request #${result.data.number} (id ${result.data.id})`);
|
||||
log.info(`» created pull request #${result.data.number}`);
|
||||
|
||||
// best-effort: request review from the user who triggered the workflow
|
||||
const reviewer = ctx.payload.triggerer;
|
||||
if (reviewer) {
|
||||
if (reviewer && result.data.number) {
|
||||
try {
|
||||
log.debug(`requesting review from ${reviewer} on PR #${result.data.number}`);
|
||||
await ctx.octokit.rest.pulls.requestReviewers({
|
||||
await ctx.gitea.rest.repository.repoCreatePullReviewRequests({
|
||||
owner: ctx.repo.owner,
|
||||
repo: ctx.repo.name,
|
||||
pull_number: result.data.number,
|
||||
reviewers: [reviewer],
|
||||
index: result.data.number,
|
||||
body: { reviewers: [reviewer] },
|
||||
});
|
||||
} catch {
|
||||
log.info(`failed to request review from ${reviewer} on PR #${result.data.number}`);
|
||||
log.debug(`failed to request review from ${reviewer}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof result.data.node_id === "string" && result.data.node_id.length > 0) {
|
||||
await patchWorkflowRunFields(ctx, {
|
||||
prNodeId: result.data.node_id,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
pullRequestId: result.data.id,
|
||||
number: result.data.number,
|
||||
url: result.data.html_url,
|
||||
title: result.data.title,
|
||||
head: result.data.head.ref,
|
||||
base: result.data.base.ref,
|
||||
head: result.data.head?.label ?? result.data.head?.ref,
|
||||
base: result.data.base?.label ?? result.data.base?.ref,
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user