mirror of
https://ghproxy.cflmy.top/https://github.com/deepseek-ai/deepseek-harness.git
synced 2026-11-29 00:30:04 +08:00
The Code review page's detail shows Open on the code host, Copy link, and Refresh status as three icon buttons after the merge action; a narrow detail folds them into the More menu as before. The merge action also turns the code host's automatic merge on, shows that it is on with its cancellation, and names why the host would not merge. - forge-github: snapshots carry `autoMerge` from `viewerCanEnableAutoMerge`, `viewerCanDisableAutoMerge`, and `autoMergeRequest`; `setAutoMerge` sends `enablePullRequestAutoMerge` with the confirmed head or `disablePullRequestAutoMerge`. A base branch with a merge queue offers none. - forge-gitlab: snapshots carry `autoMerge` from `autoMergeEnabled` and the available strategies; `setAutoMerge` accepts with the strategy that waits for every merge check, or posts `cancel_auto_merge`. Merge trains offer none. - client-ui-forge (Host): `forgePanel.setAutoMerge` and `forgePanel.reviewSetAutoMerge`. - client-ui-forge (Client): the opening actions carry the mark of the provider's forge family and name the code host instead of the browser, on the review page, the Session panel card, the changes view, and review threads. While a change request, its description, or its activity is read, a skeleton of the same layout stands in. The comment field is three lines tall with a row that names the code host and a primary button. Where the header is tight, labels fold and the repository name is cut before the actions wrap.
246 lines
13 KiB
TypeScript
246 lines
13 KiB
TypeScript
/** GitHub writes and reads of the change request overview: review requests, reviewer candidates, branch updates, and auto-merge. */
|
|
|
|
import type {
|
|
AutoMergeRequest, BranchUpdateRequest, ChangeRequestRef, ForgeActionFailure, ForgeFailure, ForgeReviewerCandidate, ForgeReviewersRequest,
|
|
} from '@deepseek-ai/dsh-experimental-forge'
|
|
import { z } from 'zod'
|
|
import type { GraphqlReader } from './commits.ts'
|
|
import { MERGE_METHOD_NAMES } from './graphql.ts'
|
|
import type { GraphqlWriter } from './mutation.ts'
|
|
import { pullRequestNode } from './pull-request-node.ts'
|
|
import { checkedRepository, isCommit } from './urls.ts'
|
|
|
|
/**
|
|
* Add review requests from users and teams by node id; variable `input` is a `RequestReviewsInput`. Every supported
|
|
* host has it, while GitHub Enterprise Server 3.19 and 3.20 lack `requestReviewsByLogin`.
|
|
*/
|
|
export const REQUEST_REVIEWS_MUTATION = `mutation($input: RequestReviewsInput!) {
|
|
requestReviews(input: $input) { clientMutationId }
|
|
}`
|
|
|
|
/** Add review requests from bots by login, which only this mutation accepts; variable `input` is a `RequestReviewsByLoginInput`. */
|
|
export const REQUEST_BOT_REVIEWS_MUTATION = `mutation($input: RequestReviewsByLoginInput!) {
|
|
requestReviewsByLogin(input: $input) { clientMutationId }
|
|
}`
|
|
|
|
/**
|
|
* Split a team reviewer named `organization/slug`.
|
|
* @param login - the team's name.
|
|
* @returns the organization and the slug, or undefined for another name.
|
|
*/
|
|
function teamOf(login: string): readonly [string, string] | undefined {
|
|
const [organization, slug, ...rest] = login.split('/')
|
|
return organization === undefined || organization === '' || slug === undefined || slug === '' || rest.length > 0 ? undefined
|
|
: [organization, slug]
|
|
}
|
|
|
|
/** Merge or rebase the base branch into the head branch; variable `input` is an `UpdatePullRequestBranchInput`. */
|
|
export const UPDATE_BRANCH_MUTATION = `mutation($input: UpdatePullRequestBranchInput!) {
|
|
updatePullRequestBranch(input: $input) { clientMutationId }
|
|
}`
|
|
|
|
/** Turn auto-merge on with a merge method for an expected head; variable `input` is an `EnablePullRequestAutoMergeInput`. */
|
|
export const ENABLE_AUTO_MERGE_MUTATION = `mutation($input: EnablePullRequestAutoMergeInput!) {
|
|
enablePullRequestAutoMerge(input: $input) { clientMutationId }
|
|
}`
|
|
|
|
/** Turn auto-merge off; variable `input` is a `DisablePullRequestAutoMergeInput`. */
|
|
export const DISABLE_AUTO_MERGE_MUTATION = `mutation($input: DisablePullRequestAutoMergeInput!) {
|
|
disablePullRequestAutoMerge(input: $input) { clientMutationId }
|
|
}`
|
|
|
|
/** Most accounts one candidate listing returns. */
|
|
const CANDIDATES = 20
|
|
|
|
/** Accounts that can review and GitHub's suggestions; variables `owner`, `name`, and `search` are strings, `number` is an integer. */
|
|
export const CANDIDATES_QUERY = `query($owner: String!, $name: String!, $number: Int!, $search: String!) {
|
|
repository(owner: $owner, name: $name) {
|
|
assignableUsers(first: ${CANDIDATES}, query: $search) { nodes { login name avatarUrl } }
|
|
pullRequest(number: $number) { author { login } suggestedReviewers { reviewer { login name avatarUrl } } }
|
|
}
|
|
}`
|
|
|
|
const account = z.object({ login: z.string(), name: z.string().nullable(), avatarUrl: z.string().nullable() })
|
|
|
|
const candidatesSchema = z.object({
|
|
data: z.object({
|
|
repository: z.object({
|
|
assignableUsers: z.object({ nodes: z.array(account) }),
|
|
pullRequest: z.object({
|
|
author: z.object({ login: z.string() }).nullable(),
|
|
suggestedReviewers: z.array(z.object({ reviewer: account })),
|
|
}).nullable(),
|
|
}).nullable(),
|
|
}),
|
|
})
|
|
|
|
/**
|
|
* Request reviews, adding to the requests GitHub holds. Users and teams are resolved to node ids in one query and asked
|
|
* with `requestReviews`; bots, named with or without GitHub's `[bot]` suffix, are asked with `requestReviewsByLogin`
|
|
* afterwards, which hosts without it refuse. A team is named `organization/slug`.
|
|
* @param read - runs one GraphQL query.
|
|
* @param write - runs one GraphQL mutation.
|
|
* @param ref - pull request address.
|
|
* @param request - the reviewers to ask.
|
|
* @param signal - cancellation of every request.
|
|
* @returns undefined once GitHub accepted every request, or why it refused; a refused bot request follows an accepted
|
|
* request of the users and teams.
|
|
*/
|
|
export async function requestGitHubReviews(
|
|
read: GraphqlReader, write: GraphqlWriter, ref: ChangeRequestRef, request: ForgeReviewersRequest, signal: AbortSignal,
|
|
): Promise<ForgeActionFailure | undefined> {
|
|
if (request.reviewers.length === 0) return { code: 'failed', message: 'No reviewer was named' }
|
|
const logins = (kind: string): string[] => request.reviewers.filter(reviewer => reviewer.kind === kind).map(reviewer => reviewer.login)
|
|
const users = logins('user')
|
|
const teams: (readonly [string, string])[] = []
|
|
for (const login of logins('team')) {
|
|
const team = teamOf(login)
|
|
if (team === undefined) return { code: 'failed', message: `Team ${JSON.stringify(login)} is not named organization/slug` }
|
|
teams.push(team)
|
|
}
|
|
const bots = logins('bot').map(login => login.endsWith('[bot]') ? login : `${login}[bot]`)
|
|
const id = await pullRequestNode(read, ref, null, signal)
|
|
if (typeof id !== 'string') return id
|
|
const { host } = ref.repository
|
|
if (users.length > 0 || teams.length > 0) {
|
|
const ids = await reviewerIds(read, host, users, teams, signal)
|
|
if ('code' in ids) return ids
|
|
const failure = await write(host, { query: REQUEST_REVIEWS_MUTATION, field: 'requestReviews', variables: {
|
|
input: { pullRequestId: id, union: true, ...ids.users.length > 0 ? { userIds: ids.users } : {},
|
|
...ids.teams.length > 0 ? { teamIds: ids.teams } : {} },
|
|
} }, signal)
|
|
if (failure !== undefined) return failure
|
|
}
|
|
if (bots.length === 0) return undefined
|
|
return await write(host, { query: REQUEST_BOT_REVIEWS_MUTATION, field: 'requestReviewsByLogin',
|
|
variables: { input: { pullRequestId: id, union: true, botLogins: bots } } }, signal)
|
|
}
|
|
|
|
const idNode = z.object({ id: z.string() })
|
|
|
|
/**
|
|
* Resolve user logins and team slugs to node ids with one aliased query whose names are variables.
|
|
* @returns the ids in request order, or why GitHub did not resolve them, as for an unknown login.
|
|
*/
|
|
async function reviewerIds(
|
|
read: GraphqlReader, host: string, users: readonly string[], teams: readonly (readonly [string, string])[], signal: AbortSignal,
|
|
): Promise<{ readonly users: readonly string[]; readonly teams: readonly string[] } | ForgeActionFailure> {
|
|
const declarations = [...users.map((_, index) => `$u${index}: String!`), ...teams.flatMap((_, index) => [`$o${index}: String!`, `$t${index}: String!`])]
|
|
const fields = [...users.map((_, index) => `u${index}: user(login: $u${index}) { id }`),
|
|
...teams.map((_, index) => `t${index}: organization(login: $o${index}) { team(slug: $t${index}) { id } }`)]
|
|
const query = `query(${declarations.join(', ')}) { ${fields.join(' ')} }`
|
|
const body = await read(['api', 'graphql', '--hostname', host, '-f', `query=${query}`,
|
|
...users.flatMap((login, index) => ['-f', `u${index}=${login}`]),
|
|
...teams.flatMap(([organization, slug], index) => ['-f', `o${index}=${organization}`, '-f', `t${index}=${slug}`])], signal)
|
|
if (!body.ok) return { code: body.failure.code === 'unauthenticated' ? 'unauthenticated' : 'failed', message: body.failure.message }
|
|
const parsed = z.object({ data: z.record(z.string(), z.unknown()) }).safeParse(body.body)
|
|
if (!parsed.success) return { code: 'failed', message: 'unexpected GitHub response to the reviewer lookup' }
|
|
const { data } = parsed.data
|
|
const userIds: string[] = []
|
|
for (const [index, login] of users.entries()) {
|
|
const user = idNode.safeParse(data[`u${index}`])
|
|
if (!user.success) return { code: 'failed', message: `GitHub has no user ${login}` }
|
|
userIds.push(user.data.id)
|
|
}
|
|
const teamIds: string[] = []
|
|
for (const [index, [organization, slug]] of teams.entries()) {
|
|
const team = z.object({ team: idNode }).safeParse(data[`t${index}`])
|
|
if (!team.success) return { code: 'failed', message: `GitHub shows the signed-in user no team ${organization}/${slug}` }
|
|
teamIds.push(team.data.team.id)
|
|
}
|
|
return { users: userIds, teams: teamIds }
|
|
}
|
|
|
|
/**
|
|
* Update the head branch with `updatePullRequestBranch`. The head is read first and the update is refused once it is
|
|
* another commit; GitHub also checks `expectedHeadOid` itself.
|
|
* @param read - runs one GraphQL query.
|
|
* @param write - runs one GraphQL mutation.
|
|
* @param ref - pull request address.
|
|
* @param request - update method and the head commit the user confirmed.
|
|
* @param signal - cancellation of both requests.
|
|
* @returns undefined once GitHub accepted the update, `stale` when the head moved, or why GitHub refused.
|
|
* @throws when `request.headSha` is not a full commit object name.
|
|
*/
|
|
export async function updateGitHubBranch(
|
|
read: GraphqlReader, write: GraphqlWriter, ref: ChangeRequestRef, request: BranchUpdateRequest, signal: AbortSignal,
|
|
): Promise<ForgeActionFailure | undefined> {
|
|
if (!isCommit(request.headSha)) throw new Error(`forge-github: invalid head commit ${JSON.stringify(request.headSha)}`)
|
|
const id = await pullRequestNode(read, ref, request.headSha, signal)
|
|
if (typeof id !== 'string') return id
|
|
return await write(ref.repository.host, { query: UPDATE_BRANCH_MUTATION, field: 'updatePullRequestBranch', variables: {
|
|
input: { pullRequestId: id, expectedHeadOid: request.headSha, updateMethod: request.method === 'rebase' ? 'REBASE' : 'MERGE' },
|
|
} }, signal)
|
|
}
|
|
|
|
/**
|
|
* Turn a pull request's auto-merge on with `enablePullRequestAutoMerge`, or off with `disablePullRequestAutoMerge`.
|
|
* Turning it on reads the head first and sends no mutation once it is another commit; GitHub also checks
|
|
* `expectedHeadOid` itself. A refusal of turning it on is followed by one more read of the head, and is `stale` when
|
|
* the head is another commit by then. Turning it off reads the pull request at any head.
|
|
* @param read - runs one GraphQL query.
|
|
* @param write - runs one GraphQL mutation.
|
|
* @param ref - pull request address.
|
|
* @param request - on with the merge method and the head commit the user confirmed, or off.
|
|
* @param signal - cancellation of every request.
|
|
* @returns undefined once GitHub accepted the change, `stale` when the head moved, or why GitHub refused or did not
|
|
* confirm it.
|
|
* @throws when the head commit of a request that turns auto-merge on is not a full commit object name.
|
|
*/
|
|
export async function setGitHubAutoMerge(
|
|
read: GraphqlReader, write: GraphqlWriter, ref: ChangeRequestRef, request: AutoMergeRequest, signal: AbortSignal,
|
|
): Promise<ForgeActionFailure | undefined> {
|
|
const { host } = ref.repository
|
|
if (!request.enabled) {
|
|
const id = await pullRequestNode(read, ref, null, signal)
|
|
if (typeof id !== 'string') return id
|
|
return await write(host, { query: DISABLE_AUTO_MERGE_MUTATION, field: 'disablePullRequestAutoMerge',
|
|
variables: { input: { pullRequestId: id } } }, signal)
|
|
}
|
|
const { headSha } = request
|
|
if (!isCommit(headSha)) throw new Error(`forge-github: invalid head commit ${JSON.stringify(headSha)}`)
|
|
const id = await pullRequestNode(read, ref, headSha, signal)
|
|
if (typeof id !== 'string') return id
|
|
const failure = await write(host, { query: ENABLE_AUTO_MERGE_MUTATION, field: 'enablePullRequestAutoMerge', variables: {
|
|
input: { pullRequestId: id, mergeMethod: MERGE_METHOD_NAMES[request.method], expectedHeadOid: headSha },
|
|
} }, signal)
|
|
if (failure?.code !== 'failed') return failure
|
|
// A refusal carries no HTTP status that names a moved head, as the REST merge's 409 does.
|
|
const moved = await pullRequestNode(read, ref, headSha, signal)
|
|
return typeof moved !== 'string' && moved.code === 'stale' ? moved : failure
|
|
}
|
|
|
|
/**
|
|
* List accounts a review can be requested from: GitHub's suggestions for the pull request when `query` is empty, then
|
|
* the accounts that can be assigned in the repository whose login or name matches `query`, without the author.
|
|
* @param read - runs one GraphQL query.
|
|
* @param ref - pull request address.
|
|
* @param query - text the login or name contains.
|
|
* @param signal - cancellation of the read.
|
|
* @returns at most 20 accounts, each once, or why GitHub did not list them.
|
|
*/
|
|
export async function gitHubReviewerCandidates(
|
|
read: GraphqlReader, ref: ChangeRequestRef, query: string, signal: AbortSignal,
|
|
): Promise<readonly ForgeReviewerCandidate[] | ForgeFailure> {
|
|
const { host, owner, name } = checkedRepository(ref)
|
|
const search = query.trim()
|
|
const body = await read([
|
|
'api', 'graphql', '--hostname', host, '-f', `query=${CANDIDATES_QUERY}`, '-f', `owner=${owner}`, '-f', `name=${name}`,
|
|
'-F', `number=${ref.number}`, '-f', `search=${search}`,
|
|
], signal)
|
|
if (!body.ok) return body.failure
|
|
const parsed = candidatesSchema.safeParse(body.body)
|
|
if (!parsed.success) return { code: 'unavailable', message: 'unexpected GitHub response to the reviewer listing' }
|
|
const repository = parsed.data.data.repository
|
|
if (repository === null) return { code: 'not-found', message: `repository ${owner}/${name} is not visible` }
|
|
const author = repository.pullRequest?.author?.login.toLowerCase()
|
|
const listed = new Map<string, ForgeReviewerCandidate>()
|
|
const add = (entry: z.infer<typeof account>, suggested: boolean): void => {
|
|
const key = entry.login.toLowerCase()
|
|
if (key !== author && !listed.has(key)) listed.set(key, { login: entry.login, name: entry.name, avatarUrl: entry.avatarUrl, suggested })
|
|
}
|
|
if (search === '') for (const suggestion of repository.pullRequest?.suggestedReviewers ?? []) add(suggestion.reviewer, true)
|
|
for (const user of repository.assignableUsers.nodes) add(user, false)
|
|
return [...listed.values()].slice(0, CANDIDATES)
|
|
}
|