@gitkraken/core-gitlens 0.44.0 → 0.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -1
- package/dist/git-cli/providers/config.js +2 -2
- package/dist/git-cli/providers/pausedOperations.js +2 -2
- package/dist/git-cli/providers/pausedOperations.js.map +1 -1
- package/dist/git-cli/providers/staging.js +1 -1
- package/dist/ipc/discovery.js +1 -1
- package/dist/plus/agents/providers/claudeCodeTranscript.js +6 -6
- package/dist/plus/agents/providers/claudeCodeTranscript.js.map +1 -1
- package/dist/plus/ai/providers/openAICompatibleProviderBase.js +1 -1
- package/dist/plus/git-github/api/github.d.ts +7 -0
- package/dist/plus/git-github/api/github.js +88 -29
- package/dist/plus/git-github/api/github.js.map +1 -1
- package/dist/plus/git-github/api/issueWrites.d.ts +3 -1
- package/dist/plus/git-github/api/issueWrites.js.map +1 -1
- package/dist/plus/git-github/api/pullRequestWrites.d.ts +95 -0
- package/dist/plus/git-github/api/pullRequestWrites.js +469 -0
- package/dist/plus/git-github/api/pullRequestWrites.js.map +1 -0
- package/dist/plus/integrations/authentication/configuredIntegrationService.d.ts +10 -9
- package/dist/plus/integrations/authentication/configuredIntegrationService.js +18 -12
- package/dist/plus/integrations/authentication/configuredIntegrationService.js.map +1 -1
- package/dist/plus/integrations/authentication/integrationAuthenticationProvider.d.ts +9 -4
- package/dist/plus/integrations/authentication/integrationAuthenticationProvider.js +26 -9
- package/dist/plus/integrations/authentication/integrationAuthenticationProvider.js.map +1 -1
- package/dist/plus/integrations/authentication/integrationAuthenticationService.d.ts +1 -0
- package/dist/plus/integrations/authentication/integrationAuthenticationService.js +3 -0
- package/dist/plus/integrations/authentication/integrationAuthenticationService.js.map +1 -1
- package/dist/plus/integrations/authentication/observedAuthenticationProvider.d.ts +1 -0
- package/dist/plus/integrations/authentication/observedAuthenticationProvider.js +14 -4
- package/dist/plus/integrations/authentication/observedAuthenticationProvider.js.map +1 -1
- package/dist/plus/integrations/authentication/rejectedTokenTracker.d.ts +2 -2
- package/dist/plus/integrations/authentication/rejectedTokenTracker.js +19 -5
- package/dist/plus/integrations/authentication/rejectedTokenTracker.js.map +1 -1
- package/dist/plus/integrations/index.d.ts +3 -2
- package/dist/plus/integrations/index.js.map +1 -1
- package/dist/plus/integrations/integrationService.d.ts +6 -1
- package/dist/plus/integrations/integrationService.js +45 -1
- package/dist/plus/integrations/integrationService.js.map +1 -1
- package/dist/plus/integrations/manager.d.ts +11 -4
- package/dist/plus/integrations/models/integration.d.ts +2 -2
- package/dist/plus/integrations/models/integration.js +10 -5
- package/dist/plus/integrations/models/integration.js.map +1 -1
- package/dist/plus/integrations/providers/azure/azure.js +1 -1
- package/dist/plus/integrations/providers/github.d.ts +5 -0
- package/dist/plus/integrations/providers/github.js +54 -1
- package/dist/plus/integrations/providers/github.js.map +1 -1
- package/dist/plus/integrations/providers/issueSorts.d.ts +0 -1
- package/dist/plus/integrations/reads/drains.d.ts +0 -1
- package/dist/utils/env/node/exec.js +2 -2
- package/docs/integrations.md +36 -0
- package/package.json +1 -1
|
@@ -49,7 +49,9 @@ export type GitHubIssueMutation = GitHubIssueWrite | (GitHubIssueDelete & {
|
|
|
49
49
|
});
|
|
50
50
|
type GraphqlRequest = <T>(query: string, variables: RequestParameters) => Promise<T | undefined>;
|
|
51
51
|
export declare function isValidGitHubIssueMutation(input: GitHubIssueMutation): boolean;
|
|
52
|
-
export declare function issueWriteFailure(ex: unknown): IssueWriteResult
|
|
52
|
+
export declare function issueWriteFailure(ex: unknown): Extract<IssueWriteResult, {
|
|
53
|
+
ok: false;
|
|
54
|
+
}>;
|
|
53
55
|
export declare function mutateGitHubIssue(input: GitHubIssueMutation, graphql: GraphqlRequest): Promise<IssueWriteResult>;
|
|
54
56
|
//#endregion
|
|
55
57
|
//# sourceMappingURL=issueWrites.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"issueWrites.js","names":[],"sources":["../../../../../plus/git-github/src/api/issueWrites.ts"],"sourcesContent":["import { GraphqlResponseError } from '@octokit/graphql';\nimport { RequestError } from '@octokit/request-error';\nimport type { RequestParameters } from '@octokit/types';\nimport { RequestRateLimitError } from '@gitlens/git/errors.js';\n\ntype GitHubIssueWriteTarget = { owner: string; repo: string };\n\nexport type GitHubIssueDelete = GitHubIssueWriteTarget & { number: number; expectedNodeId: string };\n\nexport type GitHubIssueWrite =\n\t| (GitHubIssueWriteTarget & { action: 'create'; title: string; body?: string })\n\t| (GitHubIssueDelete &\n\t\t\t(\n\t\t\t\t| { action: 'edit'; title?: string; body?: string }\n\t\t\t\t| { action: 'set-state'; state: 'open' }\n\t\t\t\t| { action: 'set-state'; state: 'closed'; reason: 'completed' | 'not_planned' }\n\t\t\t));\n\nexport type IssueWriteResult =\n\t| {\n\t\t\tok: true;\n\t\t\tissue: {\n\t\t\t\towner: string;\n\t\t\t\trepo: string;\n\t\t\t\tnumber: number;\n\t\t\t\tnodeId: string;\n\t\t\t\turl: string;\n\t\t\t\ttitle: string;\n\t\t\t\tbody: string;\n\t\t\t\tstate: 'open' | 'closed';\n\t\t\t};\n\t\t\tdeletedFromDifferentRepository?: true;\n\t }\n\t| {\n\t\t\tok: false;\n\t\t\treason:\n\t\t\t\t| 'unsupported'\n\t\t\t\t| 'no-connection'\n\t\t\t\t| 'forbidden'\n\t\t\t\t| 'not-found'\n\t\t\t\t| 'invalid-input'\n\t\t\t\t| 'rate-limit'\n\t\t\t\t| 'unknown';\n\t };\n\nexport type GitHubIssueMutation =\n\t| GitHubIssueWrite\n\t| (GitHubIssueDelete & { action: 'delete' })\n\t| (GitHubIssueDelete & { action: 'resolve'; intent?: 'delete' });\ntype GraphqlRequest = <T>(query: string, variables: RequestParameters) => Promise<T | undefined>;\n\nconst issueFields = `\n\tid number url title body state\n\trepository { id owner { login } name }\n`;\n\ntype IssueNode = {\n\tid: string;\n\tnumber: number;\n\turl: string;\n\ttitle: string;\n\tbody: string;\n\tstate: 'OPEN' | 'CLOSED';\n\trepository: { id: string; owner: { login: string }; name: string };\n};\n\ntype RepositoryNode = {\n\tid: string;\n\thasIssuesEnabled: boolean;\n\tisArchived: boolean;\n};\n\ntype WriteTarget = {\n\trepository: RepositoryNode | null;\n\tnode?: (IssueNode & { __typename: string; viewerCanDelete?: boolean }) | null;\n};\n\nexport function isValidGitHubIssueMutation(input: GitHubIssueMutation): boolean {\n\tif (\n\t\ttypeof input.owner !== 'string' ||\n\t\t!/^[a-z\\d_-]+$/i.test(input.owner) ||\n\t\ttypeof input.repo !== 'string' ||\n\t\t!/^[a-z\\d._-]+$/i.test(input.repo) ||\n\t\tinput.repo === '.' ||\n\t\tinput.repo === '..'\n\t) {\n\t\treturn false;\n\t}\n\n\tif (\n\t\tinput.action !== 'create' &&\n\t\t(!Number.isInteger(input.number) ||\n\t\t\tinput.number < 1 ||\n\t\t\tinput.number > 2147483647 ||\n\t\t\ttypeof input.expectedNodeId !== 'string' ||\n\t\t\t!input.expectedNodeId.trim())\n\t) {\n\t\treturn false;\n\t}\n\n\tswitch (input.action) {\n\t\tcase 'create':\n\t\t\treturn isIssueTitle(input.title) && (input.body === undefined || typeof input.body === 'string');\n\t\tcase 'edit':\n\t\t\treturn (\n\t\t\t\t(input.title !== undefined || input.body !== undefined) &&\n\t\t\t\t(input.title === undefined || isIssueTitle(input.title)) &&\n\t\t\t\t(input.body === undefined || typeof input.body === 'string')\n\t\t\t);\n\t\tcase 'set-state':\n\t\t\treturn (\n\t\t\t\tinput.state === 'open' ||\n\t\t\t\t(input.state === 'closed' && (input.reason === 'completed' || input.reason === 'not_planned'))\n\t\t\t);\n\t\tcase 'delete':\n\t\t\treturn true;\n\t\tcase 'resolve':\n\t\t\treturn input.intent === undefined || input.intent === 'delete';\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction isIssueTitle(title: unknown): title is string {\n\treturn typeof title === 'string' && title.trim().length > 0;\n}\n\nexport function issueWriteFailure(ex: unknown): IssueWriteResult {\n\tif (RequestRateLimitError.is(ex)) return { ok: false, reason: 'rate-limit' };\n\n\tif (ex instanceof GraphqlResponseError) {\n\t\tif (ex.errors?.some(error => error.type === 'RATE_LIMIT' || error.type === 'RATE_LIMITED')) {\n\t\t\treturn { ok: false, reason: 'rate-limit' };\n\t\t}\n\n\t\tswitch (ex.errors?.[0]?.type) {\n\t\t\tcase 'NOT_FOUND':\n\t\t\t\treturn { ok: false, reason: 'not-found' };\n\t\t\tcase 'FORBIDDEN':\n\t\t\tcase 'INSUFFICIENT_SCOPES':\n\t\t\t\treturn { ok: false, reason: 'forbidden' };\n\t\t\tcase 'BAD_USER_INPUT':\n\t\t\tcase 'UNPROCESSABLE':\n\t\t\t\treturn { ok: false, reason: 'invalid-input' };\n\t\t}\n\t}\n\n\tif (ex instanceof RequestError) {\n\t\tswitch (ex.status) {\n\t\t\tcase 400:\n\t\t\tcase 422:\n\t\t\t\treturn { ok: false, reason: 'invalid-input' };\n\t\t\tcase 401:\n\t\t\t\treturn { ok: false, reason: 'no-connection' };\n\t\t\tcase 403:\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\treason: ex.message.toLowerCase().includes('rate limit') ? 'rate-limit' : 'forbidden',\n\t\t\t\t};\n\t\t\tcase 404:\n\t\t\tcase 410:\n\t\t\t\treturn { ok: false, reason: 'not-found' };\n\t\t\tcase 429:\n\t\t\t\treturn { ok: false, reason: 'rate-limit' };\n\t\t}\n\t}\n\n\treturn { ok: false, reason: 'unknown' };\n}\n\nexport async function mutateGitHubIssue(\n\tinput: GitHubIssueMutation,\n\tgraphql: GraphqlRequest,\n): Promise<IssueWriteResult> {\n\ttry {\n\t\tconst { owner, repo } = input;\n\t\tconst target =\n\t\t\tinput.action === 'create'\n\t\t\t\t? await graphql<WriteTarget>(\n\t\t\t\t\t\t`\n\t\t\t\t\t\t\tquery IssueCreateTarget($owner: String!, $repo: String!) {\n\t\t\t\t\t\t\t\trepository(owner: $owner, name: $repo, followRenames: false) {\n\t\t\t\t\t\t\t\t\tid\n\t\t\t\t\t\t\t\t\thasIssuesEnabled\n\t\t\t\t\t\t\t\t\tisArchived\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t`,\n\t\t\t\t\t\t{ owner: owner, repo: repo },\n\t\t\t\t\t)\n\t\t\t\t: await graphql<WriteTarget>(\n\t\t\t\t\t\t`query IssueWriteTarget($owner: String!, $repo: String!, $expectedNodeId: ID!, $checkDelete: Boolean!) {\n\t\t\t\t\t\t\trepository(owner: $owner, name: $repo, followRenames: false) {\n\t\t\t\t\t\t\t\tid hasIssuesEnabled isArchived\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnode(id: $expectedNodeId) {\n\t\t\t\t\t\t\t\t__typename\n\t\t\t\t\t\t\t\t... on Issue {\n\t\t\t\t\t\t\t\t\t${issueFields}\n\t\t\t\t\t\t\t\t\tviewerCanDelete @include(if: $checkDelete)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}`,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\towner: owner,\n\t\t\t\t\t\t\trepo: repo,\n\t\t\t\t\t\t\texpectedNodeId: input.expectedNodeId,\n\t\t\t\t\t\t\tcheckDelete:\n\t\t\t\t\t\t\t\tinput.action === 'delete' || (input.action === 'resolve' && input.intent === 'delete'),\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\t\tconst repository = target?.repository;\n\t\tif (repository == null) return { ok: false, reason: 'not-found' };\n\t\tif (repository.isArchived) return { ok: false, reason: 'forbidden' };\n\t\tif (!repository.hasIssuesEnabled) return { ok: false, reason: 'unsupported' };\n\n\t\tif (input.action === 'create') {\n\t\t\treturn await writeIssue(graphql, 'createIssue', 'CreateIssueInput', {\n\t\t\t\trepositoryId: repository.id,\n\t\t\t\ttitle: input.title,\n\t\t\t\tbody: input.body,\n\t\t\t});\n\t\t}\n\n\t\tconst issue = target?.node;\n\t\tif (\n\t\t\tissue?.__typename !== 'Issue' ||\n\t\t\tissue.number !== input.number ||\n\t\t\tissue.repository.id !== repository.id ||\n\t\t\tissue.repository.owner.login.toLowerCase() !== owner.toLowerCase() ||\n\t\t\tissue.repository.name.toLowerCase() !== repo.toLowerCase()\n\t\t) {\n\t\t\treturn { ok: false, reason: 'not-found' };\n\t\t}\n\n\t\tswitch (input.action) {\n\t\t\tcase 'resolve':\n\t\t\t\tif (input.intent === 'delete' && issue.viewerCanDelete !== true) {\n\t\t\t\t\treturn { ok: false, reason: 'forbidden' };\n\t\t\t\t}\n\t\t\t\treturn toIssueWriteResult(issue);\n\t\t\tcase 'edit':\n\t\t\t\treturn await writeIssue(graphql, 'updateIssue', 'UpdateIssueInput', {\n\t\t\t\t\tid: issue.id,\n\t\t\t\t\ttitle: input.title,\n\t\t\t\t\tbody: input.body,\n\t\t\t\t});\n\t\t\tcase 'set-state':\n\t\t\t\treturn input.state === 'open'\n\t\t\t\t\t? await writeIssue(graphql, 'reopenIssue', 'ReopenIssueInput', { issueId: issue.id })\n\t\t\t\t\t: await writeIssue(graphql, 'closeIssue', 'CloseIssueInput', {\n\t\t\t\t\t\t\tissueId: issue.id,\n\t\t\t\t\t\t\tstateReason: input.reason === 'completed' ? 'COMPLETED' : 'NOT_PLANNED',\n\t\t\t\t\t\t});\n\t\t\tcase 'delete': {\n\t\t\t\tif (issue.viewerCanDelete !== true) return { ok: false, reason: 'forbidden' };\n\n\t\t\t\tconst result = await graphql<{ deleteIssue: { repository: { id: string } | null } | null }>(\n\t\t\t\t\t`\n\t\t\t\t\t\tmutation DeleteIssue($issueId: ID!) {\n\t\t\t\t\t\t\tdeleteIssue(input: { issueId: $issueId }) {\n\t\t\t\t\t\t\t\trepository {\n\t\t\t\t\t\t\t\t\tid\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t`,\n\t\t\t\t\t{ issueId: issue.id },\n\t\t\t\t);\n\t\t\t\tconst deletedRepositoryId = result?.deleteIssue?.repository?.id;\n\t\t\t\treturn typeof deletedRepositoryId === 'string' && deletedRepositoryId.length > 0\n\t\t\t\t\t? toIssueWriteResult(issue, deletedRepositoryId !== repository.id)\n\t\t\t\t\t: { ok: false, reason: 'unknown' };\n\t\t\t}\n\t\t}\n\t} catch (ex) {\n\t\treturn issueWriteFailure(ex);\n\t}\n}\n\nasync function writeIssue(\n\tgraphql: GraphqlRequest,\n\tmutation: 'createIssue' | 'updateIssue' | 'closeIssue' | 'reopenIssue',\n\tinputType: string,\n\tinput: Record<string, string | undefined>,\n): Promise<IssueWriteResult> {\n\tconst result = await graphql<{ change: { issue: IssueNode | null } | null }>(\n\t\t`mutation WriteIssue($input: ${inputType}!) {\n\t\t\tchange: ${mutation}(input: $input) { issue { ${issueFields} } }\n\t\t}`,\n\t\t{ input: input },\n\t);\n\treturn result?.change?.issue != null ? toIssueWriteResult(result.change.issue) : { ok: false, reason: 'unknown' };\n}\n\nfunction toIssueWriteResult(issue: IssueNode, deletedFromDifferentRepository = false): IssueWriteResult {\n\treturn {\n\t\tok: true,\n\t\t...(deletedFromDifferentRepository && { deletedFromDifferentRepository: true }),\n\t\tissue: {\n\t\t\towner: issue.repository.owner.login,\n\t\t\trepo: issue.repository.name,\n\t\t\tnumber: issue.number,\n\t\t\tnodeId: issue.id,\n\t\t\turl: issue.url,\n\t\t\ttitle: issue.title,\n\t\t\tbody: issue.body,\n\t\t\tstate: issue.state === 'CLOSED' ? 'closed' : 'open',\n\t\t},\n\t};\n}\n"],"mappings":";;;;AAmDA,MAAM,cAAc;;;;AA0BpB,SAAgB,2BAA2B,OAAqC;CAC/E,IACC,OAAO,MAAM,UAAU,YACvB,CAAC,gBAAgB,KAAK,MAAM,KAAK,KACjC,OAAO,MAAM,SAAS,YACtB,CAAC,iBAAiB,KAAK,MAAM,IAAI,KACjC,MAAM,SAAS,OACf,MAAM,SAAS,MAEf,OAAO;CAGR,IACC,MAAM,WAAW,aAChB,CAAC,OAAO,UAAU,MAAM,MAA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AAO;EACN,IAAI;EACJ,GAAI,kCAAkC,EAAE,gCAAgC,KAAK;EAC7E,OAAO;GACN,OAAO,MAAM,WAAW,MAAM;GAC9B,MAAM,MAAM,WAAW;GACvB,QAAQ,MAAM;GACd,QAAQ,MAAM;GACd,KAAK,MAAM;GACX,OAAO,MAAM;GACb,MAAM,MAAM;GACZ,OAAO,MAAM,UAAU,WAAW,WAAW;EAC9C;CACD;AACD"}
|
|
1
|
+
{"version":3,"file":"issueWrites.js","names":[],"sources":["../../../../../plus/git-github/src/api/issueWrites.ts"],"sourcesContent":["import { GraphqlResponseError } from '@octokit/graphql';\nimport { RequestError } from '@octokit/request-error';\nimport type { RequestParameters } from '@octokit/types';\nimport { RequestRateLimitError } from '@gitlens/git/errors.js';\n\ntype GitHubIssueWriteTarget = { owner: string; repo: string };\n\nexport type GitHubIssueDelete = GitHubIssueWriteTarget & { number: number; expectedNodeId: string };\n\nexport type GitHubIssueWrite =\n\t| (GitHubIssueWriteTarget & { action: 'create'; title: string; body?: string })\n\t| (GitHubIssueDelete &\n\t\t\t(\n\t\t\t\t| { action: 'edit'; title?: string; body?: string }\n\t\t\t\t| { action: 'set-state'; state: 'open' }\n\t\t\t\t| { action: 'set-state'; state: 'closed'; reason: 'completed' | 'not_planned' }\n\t\t\t));\n\nexport type IssueWriteResult =\n\t| {\n\t\t\tok: true;\n\t\t\tissue: {\n\t\t\t\towner: string;\n\t\t\t\trepo: string;\n\t\t\t\tnumber: number;\n\t\t\t\tnodeId: string;\n\t\t\t\turl: string;\n\t\t\t\ttitle: string;\n\t\t\t\tbody: string;\n\t\t\t\tstate: 'open' | 'closed';\n\t\t\t};\n\t\t\tdeletedFromDifferentRepository?: true;\n\t }\n\t| {\n\t\t\tok: false;\n\t\t\treason:\n\t\t\t\t| 'unsupported'\n\t\t\t\t| 'no-connection'\n\t\t\t\t| 'forbidden'\n\t\t\t\t| 'not-found'\n\t\t\t\t| 'invalid-input'\n\t\t\t\t| 'rate-limit'\n\t\t\t\t| 'unknown';\n\t };\n\nexport type GitHubIssueMutation =\n\t| GitHubIssueWrite\n\t| (GitHubIssueDelete & { action: 'delete' })\n\t| (GitHubIssueDelete & { action: 'resolve'; intent?: 'delete' });\ntype GraphqlRequest = <T>(query: string, variables: RequestParameters) => Promise<T | undefined>;\n\nconst issueFields = `\n\tid number url title body state\n\trepository { id owner { login } name }\n`;\n\ntype IssueNode = {\n\tid: string;\n\tnumber: number;\n\turl: string;\n\ttitle: string;\n\tbody: string;\n\tstate: 'OPEN' | 'CLOSED';\n\trepository: { id: string; owner: { login: string }; name: string };\n};\n\ntype RepositoryNode = {\n\tid: string;\n\thasIssuesEnabled: boolean;\n\tisArchived: boolean;\n};\n\ntype WriteTarget = {\n\trepository: RepositoryNode | null;\n\tnode?: (IssueNode & { __typename: string; viewerCanDelete?: boolean }) | null;\n};\n\nexport function isValidGitHubIssueMutation(input: GitHubIssueMutation): boolean {\n\tif (\n\t\ttypeof input.owner !== 'string' ||\n\t\t!/^[a-z\\d_-]+$/i.test(input.owner) ||\n\t\ttypeof input.repo !== 'string' ||\n\t\t!/^[a-z\\d._-]+$/i.test(input.repo) ||\n\t\tinput.repo === '.' ||\n\t\tinput.repo === '..'\n\t) {\n\t\treturn false;\n\t}\n\n\tif (\n\t\tinput.action !== 'create' &&\n\t\t(!Number.isInteger(input.number) ||\n\t\t\tinput.number < 1 ||\n\t\t\tinput.number > 2147483647 ||\n\t\t\ttypeof input.expectedNodeId !== 'string' ||\n\t\t\t!input.expectedNodeId.trim())\n\t) {\n\t\treturn false;\n\t}\n\n\tswitch (input.action) {\n\t\tcase 'create':\n\t\t\treturn isIssueTitle(input.title) && (input.body === undefined || typeof input.body === 'string');\n\t\tcase 'edit':\n\t\t\treturn (\n\t\t\t\t(input.title !== undefined || input.body !== undefined) &&\n\t\t\t\t(input.title === undefined || isIssueTitle(input.title)) &&\n\t\t\t\t(input.body === undefined || typeof input.body === 'string')\n\t\t\t);\n\t\tcase 'set-state':\n\t\t\treturn (\n\t\t\t\tinput.state === 'open' ||\n\t\t\t\t(input.state === 'closed' && (input.reason === 'completed' || input.reason === 'not_planned'))\n\t\t\t);\n\t\tcase 'delete':\n\t\t\treturn true;\n\t\tcase 'resolve':\n\t\t\treturn input.intent === undefined || input.intent === 'delete';\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction isIssueTitle(title: unknown): title is string {\n\treturn typeof title === 'string' && title.trim().length > 0;\n}\n\nexport function issueWriteFailure(ex: unknown): Extract<IssueWriteResult, { ok: false }> {\n\tif (RequestRateLimitError.is(ex)) return { ok: false, reason: 'rate-limit' };\n\n\tif (ex instanceof GraphqlResponseError) {\n\t\tif (ex.errors?.some(error => error.type === 'RATE_LIMIT' || error.type === 'RATE_LIMITED')) {\n\t\t\treturn { ok: false, reason: 'rate-limit' };\n\t\t}\n\n\t\tswitch (ex.errors?.[0]?.type) {\n\t\t\tcase 'NOT_FOUND':\n\t\t\t\treturn { ok: false, reason: 'not-found' };\n\t\t\tcase 'FORBIDDEN':\n\t\t\tcase 'INSUFFICIENT_SCOPES':\n\t\t\t\treturn { ok: false, reason: 'forbidden' };\n\t\t\tcase 'BAD_USER_INPUT':\n\t\t\tcase 'UNPROCESSABLE':\n\t\t\t\treturn { ok: false, reason: 'invalid-input' };\n\t\t}\n\t}\n\n\tif (ex instanceof RequestError) {\n\t\tswitch (ex.status) {\n\t\t\tcase 400:\n\t\t\tcase 422:\n\t\t\t\treturn { ok: false, reason: 'invalid-input' };\n\t\t\tcase 401:\n\t\t\t\treturn { ok: false, reason: 'no-connection' };\n\t\t\tcase 403:\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\treason: ex.message.toLowerCase().includes('rate limit') ? 'rate-limit' : 'forbidden',\n\t\t\t\t};\n\t\t\tcase 404:\n\t\t\tcase 410:\n\t\t\t\treturn { ok: false, reason: 'not-found' };\n\t\t\tcase 429:\n\t\t\t\treturn { ok: false, reason: 'rate-limit' };\n\t\t}\n\t}\n\n\treturn { ok: false, reason: 'unknown' };\n}\n\nexport async function mutateGitHubIssue(\n\tinput: GitHubIssueMutation,\n\tgraphql: GraphqlRequest,\n): Promise<IssueWriteResult> {\n\ttry {\n\t\tconst { owner, repo } = input;\n\t\tconst target =\n\t\t\tinput.action === 'create'\n\t\t\t\t? await graphql<WriteTarget>(\n\t\t\t\t\t\t`\n\t\t\t\t\t\t\tquery IssueCreateTarget($owner: String!, $repo: String!) {\n\t\t\t\t\t\t\t\trepository(owner: $owner, name: $repo, followRenames: false) {\n\t\t\t\t\t\t\t\t\tid\n\t\t\t\t\t\t\t\t\thasIssuesEnabled\n\t\t\t\t\t\t\t\t\tisArchived\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t`,\n\t\t\t\t\t\t{ owner: owner, repo: repo },\n\t\t\t\t\t)\n\t\t\t\t: await graphql<WriteTarget>(\n\t\t\t\t\t\t`query IssueWriteTarget($owner: String!, $repo: String!, $expectedNodeId: ID!, $checkDelete: Boolean!) {\n\t\t\t\t\t\t\trepository(owner: $owner, name: $repo, followRenames: false) {\n\t\t\t\t\t\t\t\tid hasIssuesEnabled isArchived\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnode(id: $expectedNodeId) {\n\t\t\t\t\t\t\t\t__typename\n\t\t\t\t\t\t\t\t... on Issue {\n\t\t\t\t\t\t\t\t\t${issueFields}\n\t\t\t\t\t\t\t\t\tviewerCanDelete @include(if: $checkDelete)\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}`,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\towner: owner,\n\t\t\t\t\t\t\trepo: repo,\n\t\t\t\t\t\t\texpectedNodeId: input.expectedNodeId,\n\t\t\t\t\t\t\tcheckDelete:\n\t\t\t\t\t\t\t\tinput.action === 'delete' || (input.action === 'resolve' && input.intent === 'delete'),\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\t\tconst repository = target?.repository;\n\t\tif (repository == null) return { ok: false, reason: 'not-found' };\n\t\tif (repository.isArchived) return { ok: false, reason: 'forbidden' };\n\t\tif (!repository.hasIssuesEnabled) return { ok: false, reason: 'unsupported' };\n\n\t\tif (input.action === 'create') {\n\t\t\treturn await writeIssue(graphql, 'createIssue', 'CreateIssueInput', {\n\t\t\t\trepositoryId: repository.id,\n\t\t\t\ttitle: input.title,\n\t\t\t\tbody: input.body,\n\t\t\t});\n\t\t}\n\n\t\tconst issue = target?.node;\n\t\tif (\n\t\t\tissue?.__typename !== 'Issue' ||\n\t\t\tissue.number !== input.number ||\n\t\t\tissue.repository.id !== repository.id ||\n\t\t\tissue.repository.owner.login.toLowerCase() !== owner.toLowerCase() ||\n\t\t\tissue.repository.name.toLowerCase() !== repo.toLowerCase()\n\t\t) {\n\t\t\treturn { ok: false, reason: 'not-found' };\n\t\t}\n\n\t\tswitch (input.action) {\n\t\t\tcase 'resolve':\n\t\t\t\tif (input.intent === 'delete' && issue.viewerCanDelete !== true) {\n\t\t\t\t\treturn { ok: false, reason: 'forbidden' };\n\t\t\t\t}\n\t\t\t\treturn toIssueWriteResult(issue);\n\t\t\tcase 'edit':\n\t\t\t\treturn await writeIssue(graphql, 'updateIssue', 'UpdateIssueInput', {\n\t\t\t\t\tid: issue.id,\n\t\t\t\t\ttitle: input.title,\n\t\t\t\t\tbody: input.body,\n\t\t\t\t});\n\t\t\tcase 'set-state':\n\t\t\t\treturn input.state === 'open'\n\t\t\t\t\t? await writeIssue(graphql, 'reopenIssue', 'ReopenIssueInput', { issueId: issue.id })\n\t\t\t\t\t: await writeIssue(graphql, 'closeIssue', 'CloseIssueInput', {\n\t\t\t\t\t\t\tissueId: issue.id,\n\t\t\t\t\t\t\tstateReason: input.reason === 'completed' ? 'COMPLETED' : 'NOT_PLANNED',\n\t\t\t\t\t\t});\n\t\t\tcase 'delete': {\n\t\t\t\tif (issue.viewerCanDelete !== true) return { ok: false, reason: 'forbidden' };\n\n\t\t\t\tconst result = await graphql<{ deleteIssue: { repository: { id: string } | null } | null }>(\n\t\t\t\t\t`\n\t\t\t\t\t\tmutation DeleteIssue($issueId: ID!) {\n\t\t\t\t\t\t\tdeleteIssue(input: { issueId: $issueId }) {\n\t\t\t\t\t\t\t\trepository {\n\t\t\t\t\t\t\t\t\tid\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t`,\n\t\t\t\t\t{ issueId: issue.id },\n\t\t\t\t);\n\t\t\t\tconst deletedRepositoryId = result?.deleteIssue?.repository?.id;\n\t\t\t\treturn typeof deletedRepositoryId === 'string' && deletedRepositoryId.length > 0\n\t\t\t\t\t? toIssueWriteResult(issue, deletedRepositoryId !== repository.id)\n\t\t\t\t\t: { ok: false, reason: 'unknown' };\n\t\t\t}\n\t\t}\n\t} catch (ex) {\n\t\treturn issueWriteFailure(ex);\n\t}\n}\n\nasync function writeIssue(\n\tgraphql: GraphqlRequest,\n\tmutation: 'createIssue' | 'updateIssue' | 'closeIssue' | 'reopenIssue',\n\tinputType: string,\n\tinput: Record<string, string | undefined>,\n): Promise<IssueWriteResult> {\n\tconst result = await graphql<{ change: { issue: IssueNode | null } | null }>(\n\t\t`mutation WriteIssue($input: ${inputType}!) {\n\t\t\tchange: ${mutation}(input: $input) { issue { ${issueFields} } }\n\t\t}`,\n\t\t{ input: input },\n\t);\n\treturn result?.change?.issue != null ? toIssueWriteResult(result.change.issue) : { ok: false, reason: 'unknown' };\n}\n\nfunction toIssueWriteResult(issue: IssueNode, deletedFromDifferentRepository = false): IssueWriteResult {\n\treturn {\n\t\tok: true,\n\t\t...(deletedFromDifferentRepository && { deletedFromDifferentRepository: true }),\n\t\tissue: {\n\t\t\towner: issue.repository.owner.login,\n\t\t\trepo: issue.repository.name,\n\t\t\tnumber: issue.number,\n\t\t\tnodeId: issue.id,\n\t\t\turl: issue.url,\n\t\t\ttitle: issue.title,\n\t\t\tbody: issue.body,\n\t\t\tstate: issue.state === 'CLOSED' ? 'closed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,OAAO,kBAAkB,EAAE;CAC5B;AACD;AAEA,eAAe,WACd,SACA,UACA,WACA,OAC4B;CAC5B,MAAM,SAAS,MAAM,QACpB,+BAA+B,UAAU;aAC9B,SAAS,4BAA4B,YAAY;MAE5D,EAAS,MAAM,CAChB;CACA,OAAO,QAAQ,QAAQ,SAAS,OAAO,mBAAmB,OAAO,OAAO,KAAK,IAAI;EAAE,IAAI;EAAO,QAAQ;CAAU;AACjH;AAEA,SAAS,mBAAmB,OAAkB,iCAAiC,OAAyB;CACvG,OAAO;EACN,IAAI;EACJ,GAAI,kCAAkC,EAAE,gCAAgC,KAAK;EAC7E,OAAO;GACN,OAAO,MAAM,WAAW,MAAM;GAC9B,MAAM,MAAM,WAAW;GACvB,QAAQ,MAAM;GACd,QAAQ,MAAM;GACd,KAAK,MAAM;GACX,OAAO,MAAM;GACb,MAAM,MAAM;GACZ,OAAO,MAAM,UAAU,WAAW,WAAW;EAC9C;CACD;AACD"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { PullRequestMergeMethod, PullRequestMergeQueueActionResult } from "../../../git/models/pullRequest.js";
|
|
2
|
+
import { RequestParameters } from "@octokit/types";
|
|
3
|
+
//#region ../plus/git-github/src/api/pullRequestWrites.d.ts
|
|
4
|
+
type MergeMethod = `${PullRequestMergeMethod}`;
|
|
5
|
+
export type GitHubPullRequestCreateTarget = {
|
|
6
|
+
owner: string;
|
|
7
|
+
repo: string;
|
|
8
|
+
};
|
|
9
|
+
export type GitHubPullRequestWriteTarget = GitHubPullRequestCreateTarget & {
|
|
10
|
+
number: number;
|
|
11
|
+
expectedNodeId: string;
|
|
12
|
+
};
|
|
13
|
+
export type GitHubPullRequestWrite = (GitHubPullRequestCreateTarget & {
|
|
14
|
+
action: 'create';
|
|
15
|
+
title: string;
|
|
16
|
+
body?: string;
|
|
17
|
+
headRef: string;
|
|
18
|
+
baseRef: string;
|
|
19
|
+
expectedRepositoryId?: string;
|
|
20
|
+
}) | (GitHubPullRequestWriteTarget & ({
|
|
21
|
+
action: 'edit';
|
|
22
|
+
title?: string;
|
|
23
|
+
body?: string;
|
|
24
|
+
} | {
|
|
25
|
+
action: 'set-state';
|
|
26
|
+
state: 'open' | 'closed';
|
|
27
|
+
} | ({
|
|
28
|
+
expectedHeadSha: string;
|
|
29
|
+
expectedBaseRef: string;
|
|
30
|
+
} & ({
|
|
31
|
+
action: 'merge';
|
|
32
|
+
mergeMethod: MergeMethod;
|
|
33
|
+
} | {
|
|
34
|
+
action: 'enqueue';
|
|
35
|
+
}))));
|
|
36
|
+
export type PullRequestWriteFailure = {
|
|
37
|
+
ok: false;
|
|
38
|
+
reason: 'unsupported' | 'no-connection' | 'forbidden' | 'not-found' | 'invalid-input' | 'rate-limit' | 'blocked' | 'head-changed' | 'base-changed' | 'unknown' | 'outcome-unknown';
|
|
39
|
+
};
|
|
40
|
+
export type PullRequestWriteSnapshot = {
|
|
41
|
+
owner: string;
|
|
42
|
+
repo: string;
|
|
43
|
+
number: number;
|
|
44
|
+
nodeId: string;
|
|
45
|
+
url: string;
|
|
46
|
+
title: string;
|
|
47
|
+
body: string;
|
|
48
|
+
state: 'open' | 'closed' | 'merged';
|
|
49
|
+
closedAt?: string;
|
|
50
|
+
updatedAt?: string;
|
|
51
|
+
headSha: string;
|
|
52
|
+
baseRef: string;
|
|
53
|
+
};
|
|
54
|
+
export type PullRequestCreateCapabilityResult = {
|
|
55
|
+
ok: true;
|
|
56
|
+
repositoryId: string;
|
|
57
|
+
canCreate: boolean | undefined;
|
|
58
|
+
} | PullRequestWriteFailure;
|
|
59
|
+
type MergeCapability = {
|
|
60
|
+
kind: 'direct';
|
|
61
|
+
methods: MergeMethod[];
|
|
62
|
+
defaultMethod?: MergeMethod;
|
|
63
|
+
} | {
|
|
64
|
+
kind: 'queue';
|
|
65
|
+
} | {
|
|
66
|
+
kind: 'unavailable';
|
|
67
|
+
reason: PullRequestWriteFailure['reason'];
|
|
68
|
+
};
|
|
69
|
+
export type PullRequestWriteTargetResult = {
|
|
70
|
+
ok: true;
|
|
71
|
+
pullRequest: PullRequestWriteSnapshot;
|
|
72
|
+
capabilities: {
|
|
73
|
+
edit: boolean;
|
|
74
|
+
close: boolean;
|
|
75
|
+
reopen: boolean;
|
|
76
|
+
merge: MergeCapability;
|
|
77
|
+
};
|
|
78
|
+
} | PullRequestWriteFailure;
|
|
79
|
+
export type PullRequestWriteResult = {
|
|
80
|
+
ok: true;
|
|
81
|
+
status: 'created' | 'updated' | 'closed' | 'reopened' | 'merged' | 'queued';
|
|
82
|
+
pullRequest: PullRequestWriteSnapshot;
|
|
83
|
+
} | PullRequestWriteFailure;
|
|
84
|
+
type GraphqlRequest = <T>(query: string, variables: RequestParameters) => Promise<T | undefined>;
|
|
85
|
+
export declare function pullRequestWriteFailure(ex: unknown, phase?: 'write' | 'merge' | 'enqueue'): PullRequestWriteFailure;
|
|
86
|
+
export declare function resolveGitHubPullRequestWriteTarget(input: GitHubPullRequestWriteTarget, graphql: GraphqlRequest): Promise<PullRequestWriteTargetResult>;
|
|
87
|
+
export declare function getGitHubPullRequestCreateCapability(input: GitHubPullRequestCreateTarget, graphql: GraphqlRequest): Promise<PullRequestCreateCapabilityResult>;
|
|
88
|
+
type WriteTransport = {
|
|
89
|
+
graphql: GraphqlRequest;
|
|
90
|
+
isSessionCurrent: () => Promise<boolean>;
|
|
91
|
+
enqueue: (nodeId: string, headSha: string) => Promise<PullRequestMergeQueueActionResult>;
|
|
92
|
+
};
|
|
93
|
+
export declare function writeGitHubPullRequest(input: GitHubPullRequestWrite, transport: WriteTransport): Promise<PullRequestWriteResult>;
|
|
94
|
+
//#endregion
|
|
95
|
+
//# sourceMappingURL=pullRequestWrites.d.ts.map
|
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
import { AuthenticationError, RequestNotFoundError } from "../../../git/errors.js";
|
|
2
|
+
import { issueWriteFailure } from "./issueWrites.js";
|
|
3
|
+
import { GraphqlResponseError } from "@octokit/graphql";
|
|
4
|
+
//#region ../plus/git-github/src/api/pullRequestWrites.ts
|
|
5
|
+
const repositoryIdentity = "id owner { login } name";
|
|
6
|
+
const repositoryFields = `${repositoryIdentity} isArchived isDisabled isLocked viewerPermission`;
|
|
7
|
+
const pullRequestFields = `
|
|
8
|
+
id number url title body state closedAt mergedAt updatedAt headRefOid baseRefName
|
|
9
|
+
repository { ${repositoryIdentity} }
|
|
10
|
+
`;
|
|
11
|
+
const mergeMethods = [
|
|
12
|
+
"merge",
|
|
13
|
+
"squash",
|
|
14
|
+
"rebase"
|
|
15
|
+
];
|
|
16
|
+
function isValidRepository({ owner, repo }) {
|
|
17
|
+
return typeof owner === "string" && /^[a-z\d_-]+$/i.test(owner) && typeof repo === "string" && /^[a-z\d._-]+$/i.test(repo) && repo !== "." && repo !== "..";
|
|
18
|
+
}
|
|
19
|
+
function isValidTarget(input) {
|
|
20
|
+
return isValidRepository(input) && Number.isInteger(input.number) && input.number >= 1 && input.number <= 2147483647 && typeof input.expectedNodeId === "string" && input.expectedNodeId.trim().length > 0;
|
|
21
|
+
}
|
|
22
|
+
function isBranchName(value) {
|
|
23
|
+
return typeof value === "string" && value.length > 0 && value.trim() === value && !value.includes(":") && !value.startsWith("refs/") && !value.startsWith("-");
|
|
24
|
+
}
|
|
25
|
+
function isTitle(value) {
|
|
26
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
27
|
+
}
|
|
28
|
+
function isValidWrite(input) {
|
|
29
|
+
if (input.action === "create") return isValidRepository(input) && isTitle(input.title) && (input.body === void 0 || typeof input.body === "string") && (input.expectedRepositoryId === void 0 || typeof input.expectedRepositoryId === "string" && input.expectedRepositoryId.trim().length > 0) && isBranchName(input.headRef) && isBranchName(input.baseRef) && input.headRef !== input.baseRef;
|
|
30
|
+
if (!isValidTarget(input)) return false;
|
|
31
|
+
switch (input.action) {
|
|
32
|
+
case "edit": return (input.title !== void 0 || input.body !== void 0) && (input.title === void 0 || isTitle(input.title)) && (input.body === void 0 || typeof input.body === "string");
|
|
33
|
+
case "set-state": return input.state === "open" || input.state === "closed";
|
|
34
|
+
case "merge":
|
|
35
|
+
case "enqueue": return (input.action === "enqueue" || mergeMethods.includes(input.mergeMethod)) && isBranchName(input.expectedBaseRef) && typeof input.expectedHeadSha === "string" && /^[a-f\d]{40}$/i.test(input.expectedHeadSha);
|
|
36
|
+
default: return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function pullRequestWriteFailure(ex, phase) {
|
|
40
|
+
if (ex instanceof RequestNotFoundError) return {
|
|
41
|
+
ok: false,
|
|
42
|
+
reason: "not-found"
|
|
43
|
+
};
|
|
44
|
+
if (ex instanceof AuthenticationError) {
|
|
45
|
+
if (ex.reason === 3) return {
|
|
46
|
+
ok: false,
|
|
47
|
+
reason: "forbidden"
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
ok: false,
|
|
51
|
+
reason: "no-connection"
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const failure = issueWriteFailure(ex);
|
|
55
|
+
if (failure.reason === "rate-limit") return failure;
|
|
56
|
+
if (phase === "merge" || phase === "enqueue") {
|
|
57
|
+
const error = ex instanceof GraphqlResponseError ? ex.errors?.[0] : void 0;
|
|
58
|
+
if (error?.type === "UNPROCESSABLE") {
|
|
59
|
+
if (/\b(?:expected head oid|head sha) does not match\b/i.test(error.message)) return {
|
|
60
|
+
ok: false,
|
|
61
|
+
reason: "head-changed"
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
ok: false,
|
|
65
|
+
reason: "blocked"
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return phase != null && failure.reason === "unknown" ? {
|
|
70
|
+
ok: false,
|
|
71
|
+
reason: "outcome-unknown"
|
|
72
|
+
} : failure;
|
|
73
|
+
}
|
|
74
|
+
function hasWritePermission(repository) {
|
|
75
|
+
const { viewerPermission } = repository;
|
|
76
|
+
return viewerPermission == null ? void 0 : viewerPermission === "ADMIN" || viewerPermission === "MAINTAIN" || viewerPermission === "WRITE";
|
|
77
|
+
}
|
|
78
|
+
function isRepositoryWritable(repository) {
|
|
79
|
+
return !repository.isArchived && !repository.isDisabled && !repository.isLocked;
|
|
80
|
+
}
|
|
81
|
+
function sameRepository(a, b) {
|
|
82
|
+
return a.owner.toLowerCase() === b.owner.login.toLowerCase() && a.repo.toLowerCase() === b.name.toLowerCase();
|
|
83
|
+
}
|
|
84
|
+
function toSnapshot(node) {
|
|
85
|
+
const closedAt = node.state === "MERGED" ? node.mergedAt ?? node.closedAt : node.closedAt;
|
|
86
|
+
return {
|
|
87
|
+
owner: node.repository.owner.login,
|
|
88
|
+
repo: node.repository.name,
|
|
89
|
+
number: node.number,
|
|
90
|
+
nodeId: node.id,
|
|
91
|
+
url: node.url,
|
|
92
|
+
title: node.title,
|
|
93
|
+
body: node.body,
|
|
94
|
+
state: node.state === "OPEN" ? "open" : node.state === "CLOSED" ? "closed" : "merged",
|
|
95
|
+
...node.state !== "OPEN" && typeof closedAt === "string" ? { closedAt } : {},
|
|
96
|
+
...typeof node.updatedAt === "string" ? { updatedAt: node.updatedAt } : {},
|
|
97
|
+
headSha: node.headRefOid,
|
|
98
|
+
baseRef: node.baseRefName
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
async function resolveNode(input, graphql) {
|
|
102
|
+
const { owner, repo, expectedNodeId, number } = input;
|
|
103
|
+
const result = await graphql(`query PullRequestWriteTarget($owner: String!, $repo: String!, $expectedNodeId: ID!) {
|
|
104
|
+
repository(owner: $owner, name: $repo, followRenames: false) {
|
|
105
|
+
${repositoryFields}
|
|
106
|
+
mergeCommitAllowed squashMergeAllowed rebaseMergeAllowed viewerDefaultMergeMethod
|
|
107
|
+
}
|
|
108
|
+
node(id: $expectedNodeId) {
|
|
109
|
+
__typename
|
|
110
|
+
... on PullRequest {
|
|
111
|
+
${pullRequestFields}
|
|
112
|
+
viewerCanUpdate viewerCanClose viewerCanReopen
|
|
113
|
+
isDraft isInMergeQueue isMergeQueueEnabled
|
|
114
|
+
stack { id }
|
|
115
|
+
baseRef { id }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}`, {
|
|
119
|
+
owner,
|
|
120
|
+
repo,
|
|
121
|
+
expectedNodeId
|
|
122
|
+
});
|
|
123
|
+
const repository = result?.repository;
|
|
124
|
+
const node = result?.node;
|
|
125
|
+
if (repository == null || !sameRepository(input, repository) || node?.__typename !== "PullRequest" || node.number !== number || node.repository.id !== repository.id) return {
|
|
126
|
+
ok: false,
|
|
127
|
+
reason: "not-found"
|
|
128
|
+
};
|
|
129
|
+
return {
|
|
130
|
+
ok: true,
|
|
131
|
+
node,
|
|
132
|
+
repository
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
async function readBranchRules(refId, graphql) {
|
|
136
|
+
const rules = [];
|
|
137
|
+
const seen = /* @__PURE__ */ new Set();
|
|
138
|
+
let requiresLinearHistory = false;
|
|
139
|
+
let cursor;
|
|
140
|
+
for (;;) {
|
|
141
|
+
const ref = (await graphql(`
|
|
142
|
+
query PullRequestBranchRules($id: ID!, $cursor: String) {
|
|
143
|
+
node(id: $id) {
|
|
144
|
+
... on Ref {
|
|
145
|
+
id
|
|
146
|
+
refUpdateRule {
|
|
147
|
+
requiresLinearHistory
|
|
148
|
+
}
|
|
149
|
+
rules(first: 100, after: $cursor) {
|
|
150
|
+
nodes {
|
|
151
|
+
type
|
|
152
|
+
parameters {
|
|
153
|
+
... on PullRequestParameters {
|
|
154
|
+
allowedMergeMethods
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
pageInfo {
|
|
159
|
+
hasNextPage
|
|
160
|
+
endCursor
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
`, {
|
|
167
|
+
id: refId,
|
|
168
|
+
cursor
|
|
169
|
+
}))?.node;
|
|
170
|
+
const page = ref?.rules;
|
|
171
|
+
if (ref?.id !== refId || page?.nodes == null) return void 0;
|
|
172
|
+
requiresLinearHistory ||= ref.refUpdateRule?.requiresLinearHistory === true;
|
|
173
|
+
for (const rule of page.nodes) {
|
|
174
|
+
if (rule == null) return void 0;
|
|
175
|
+
rules.push(rule);
|
|
176
|
+
}
|
|
177
|
+
if (!page.pageInfo.hasNextPage) return {
|
|
178
|
+
rules,
|
|
179
|
+
requiresLinearHistory
|
|
180
|
+
};
|
|
181
|
+
const nextCursor = page.pageInfo.endCursor;
|
|
182
|
+
if (nextCursor == null || seen.has(nextCursor)) return void 0;
|
|
183
|
+
seen.add(nextCursor);
|
|
184
|
+
cursor = nextCursor;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
async function getMergeCapability({ node, repository }, graphql) {
|
|
188
|
+
if (!isRepositoryWritable(repository) || node.state !== "OPEN" || node.isDraft || node.isInMergeQueue || node.baseRef == null) return {
|
|
189
|
+
kind: "unavailable",
|
|
190
|
+
reason: "blocked"
|
|
191
|
+
};
|
|
192
|
+
const canWrite = hasWritePermission(repository);
|
|
193
|
+
if (canWrite !== true) return {
|
|
194
|
+
kind: "unavailable",
|
|
195
|
+
reason: canWrite === false ? "forbidden" : "unknown"
|
|
196
|
+
};
|
|
197
|
+
if (node.stack != null) return {
|
|
198
|
+
kind: "unavailable",
|
|
199
|
+
reason: "unsupported"
|
|
200
|
+
};
|
|
201
|
+
if (node.isMergeQueueEnabled) return { kind: "queue" };
|
|
202
|
+
try {
|
|
203
|
+
const branch = await readBranchRules(node.baseRef.id, graphql);
|
|
204
|
+
if (branch == null) return {
|
|
205
|
+
kind: "unavailable",
|
|
206
|
+
reason: "unknown"
|
|
207
|
+
};
|
|
208
|
+
const { rules, requiresLinearHistory } = branch;
|
|
209
|
+
if (rules.some((rule) => rule.type === "MERGE_QUEUE")) return { kind: "queue" };
|
|
210
|
+
const linearHistory = requiresLinearHistory || rules.some((rule) => rule.type === "REQUIRED_LINEAR_HISTORY");
|
|
211
|
+
const methods = mergeMethods.filter((method) => {
|
|
212
|
+
return (method === "merge" ? repository.mergeCommitAllowed && !linearHistory : method === "squash" ? repository.squashMergeAllowed : repository.rebaseMergeAllowed) && rules.every((rule) => {
|
|
213
|
+
const permitted = rule.type === "PULL_REQUEST" ? rule.parameters?.allowedMergeMethods : void 0;
|
|
214
|
+
return permitted == null || permitted.some((allowedMethod) => allowedMethod.toLowerCase() === method);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
if (methods.length === 0) return {
|
|
218
|
+
kind: "unavailable",
|
|
219
|
+
reason: "blocked"
|
|
220
|
+
};
|
|
221
|
+
const preference = repository.viewerDefaultMergeMethod.toLowerCase();
|
|
222
|
+
return {
|
|
223
|
+
kind: "direct",
|
|
224
|
+
methods,
|
|
225
|
+
defaultMethod: methods.find((method) => method === preference)
|
|
226
|
+
};
|
|
227
|
+
} catch (ex) {
|
|
228
|
+
return {
|
|
229
|
+
kind: "unavailable",
|
|
230
|
+
reason: pullRequestWriteFailure(ex).reason
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async function resolveGitHubPullRequestWriteTarget(input, graphql) {
|
|
235
|
+
if (!isValidTarget(input)) return {
|
|
236
|
+
ok: false,
|
|
237
|
+
reason: "invalid-input"
|
|
238
|
+
};
|
|
239
|
+
try {
|
|
240
|
+
const resolved = await resolveNode(input, graphql);
|
|
241
|
+
if (!resolved.ok) return resolved;
|
|
242
|
+
const { node, repository } = resolved;
|
|
243
|
+
const writable = isRepositoryWritable(repository);
|
|
244
|
+
return {
|
|
245
|
+
ok: true,
|
|
246
|
+
pullRequest: toSnapshot(node),
|
|
247
|
+
capabilities: {
|
|
248
|
+
edit: writable && node.viewerCanUpdate,
|
|
249
|
+
close: writable && node.state === "OPEN" && node.viewerCanClose,
|
|
250
|
+
reopen: writable && node.state === "CLOSED" && node.viewerCanReopen,
|
|
251
|
+
merge: await getMergeCapability(resolved, graphql)
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
} catch (ex) {
|
|
255
|
+
return pullRequestWriteFailure(ex);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async function getGitHubPullRequestCreateCapability(input, graphql) {
|
|
259
|
+
if (!isValidRepository(input)) return {
|
|
260
|
+
ok: false,
|
|
261
|
+
reason: "invalid-input"
|
|
262
|
+
};
|
|
263
|
+
try {
|
|
264
|
+
const repository = (await graphql(`query PullRequestCreateCapability($owner: String!, $repo: String!) {
|
|
265
|
+
repository(owner: $owner, name: $repo, followRenames: false) {
|
|
266
|
+
${repositoryFields}
|
|
267
|
+
}
|
|
268
|
+
}`, {
|
|
269
|
+
owner: input.owner,
|
|
270
|
+
repo: input.repo
|
|
271
|
+
}))?.repository;
|
|
272
|
+
if (repository == null || !sameRepository(input, repository)) return {
|
|
273
|
+
ok: false,
|
|
274
|
+
reason: "not-found"
|
|
275
|
+
};
|
|
276
|
+
return {
|
|
277
|
+
ok: true,
|
|
278
|
+
repositoryId: repository.id,
|
|
279
|
+
canCreate: isRepositoryWritable(repository) && (repository.viewerPermission == null ? void 0 : true)
|
|
280
|
+
};
|
|
281
|
+
} catch (ex) {
|
|
282
|
+
return pullRequestWriteFailure(ex);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const mutations = {
|
|
286
|
+
created: ["createPullRequest", "CreatePullRequestInput"],
|
|
287
|
+
updated: ["updatePullRequest", "UpdatePullRequestInput"],
|
|
288
|
+
closed: ["closePullRequest", "ClosePullRequestInput"],
|
|
289
|
+
reopened: ["reopenPullRequest", "ReopenPullRequestInput"],
|
|
290
|
+
merged: ["mergePullRequest", "MergePullRequestInput"]
|
|
291
|
+
};
|
|
292
|
+
async function sendMutation(transport, status, repositoryId, values, previous) {
|
|
293
|
+
if (!await transport.isSessionCurrent()) return {
|
|
294
|
+
ok: false,
|
|
295
|
+
reason: "no-connection"
|
|
296
|
+
};
|
|
297
|
+
const [mutation, inputType] = mutations[status];
|
|
298
|
+
let node;
|
|
299
|
+
try {
|
|
300
|
+
node = (await transport.graphql(`mutation WritePullRequest($input: ${inputType}!) {
|
|
301
|
+
change: ${mutation}(input: $input) { pullRequest { ${pullRequestFields} } }
|
|
302
|
+
}`, { input: values }))?.change?.pullRequest;
|
|
303
|
+
} catch (ex) {
|
|
304
|
+
if (ex instanceof GraphqlResponseError) {
|
|
305
|
+
node = ex.data?.change?.pullRequest;
|
|
306
|
+
if (node == null && ex.errors?.some((error) => error.path?.[0] === "change" && error.path.length > 1)) return {
|
|
307
|
+
ok: false,
|
|
308
|
+
reason: "outcome-unknown"
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
if (node == null) return pullRequestWriteFailure(ex, status === "merged" ? "merge" : "write");
|
|
312
|
+
}
|
|
313
|
+
const expectedState = status === "created" || status === "reopened" ? "OPEN" : status === "closed" ? "CLOSED" : status === "merged" ? "MERGED" : void 0;
|
|
314
|
+
if (node?.repository?.id !== repositoryId || previous != null && (node.id !== previous.id || node.number !== previous.number) || expectedState != null && node.state !== expectedState || typeof node.repository.owner?.login !== "string" || typeof node.repository.name !== "string" || typeof node.id !== "string" || !Number.isInteger(node.number) || typeof node.url !== "string" || typeof node.title !== "string" || typeof node.body !== "string" || typeof node.headRefOid !== "string" || typeof node.baseRefName !== "string" || node.state !== "OPEN" && node.state !== "CLOSED" && node.state !== "MERGED") return {
|
|
315
|
+
ok: false,
|
|
316
|
+
reason: "outcome-unknown"
|
|
317
|
+
};
|
|
318
|
+
return {
|
|
319
|
+
ok: true,
|
|
320
|
+
status,
|
|
321
|
+
pullRequest: toSnapshot(node)
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
async function createPullRequest(input, transport) {
|
|
325
|
+
const repository = (await transport.graphql(`query PullRequestCreateTarget($owner: String!, $repo: String!, $base: String!, $head: String!) {
|
|
326
|
+
repository(owner: $owner, name: $repo, followRenames: false) {
|
|
327
|
+
${repositoryFields}
|
|
328
|
+
base: ref(qualifiedName: $base) { id }
|
|
329
|
+
head: ref(qualifiedName: $head) { id }
|
|
330
|
+
}
|
|
331
|
+
}`, {
|
|
332
|
+
owner: input.owner,
|
|
333
|
+
repo: input.repo,
|
|
334
|
+
base: `refs/heads/${input.baseRef}`,
|
|
335
|
+
head: `refs/heads/${input.headRef}`
|
|
336
|
+
}))?.repository;
|
|
337
|
+
if (repository == null || !sameRepository(input, repository) || input.expectedRepositoryId != null && input.expectedRepositoryId !== repository.id) return {
|
|
338
|
+
ok: false,
|
|
339
|
+
reason: "not-found"
|
|
340
|
+
};
|
|
341
|
+
if (!isRepositoryWritable(repository)) return {
|
|
342
|
+
ok: false,
|
|
343
|
+
reason: "blocked"
|
|
344
|
+
};
|
|
345
|
+
if (repository.base == null || repository.head == null) return {
|
|
346
|
+
ok: false,
|
|
347
|
+
reason: "invalid-input"
|
|
348
|
+
};
|
|
349
|
+
return sendMutation(transport, "created", repository.id, {
|
|
350
|
+
repositoryId: repository.id,
|
|
351
|
+
baseRefName: input.baseRef,
|
|
352
|
+
headRefName: input.headRef,
|
|
353
|
+
title: input.title,
|
|
354
|
+
body: input.body
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
async function mergePullRequest(input, resolved, transport) {
|
|
358
|
+
const { node, repository } = resolved;
|
|
359
|
+
if (node.headRefOid.toLowerCase() !== input.expectedHeadSha.toLowerCase()) return {
|
|
360
|
+
ok: false,
|
|
361
|
+
reason: "head-changed"
|
|
362
|
+
};
|
|
363
|
+
if (node.baseRefName !== input.expectedBaseRef) return {
|
|
364
|
+
ok: false,
|
|
365
|
+
reason: "base-changed"
|
|
366
|
+
};
|
|
367
|
+
const capability = await getMergeCapability(resolved, transport.graphql);
|
|
368
|
+
if (capability.kind === "unavailable") return {
|
|
369
|
+
ok: false,
|
|
370
|
+
reason: capability.reason
|
|
371
|
+
};
|
|
372
|
+
if (input.action === "merge") {
|
|
373
|
+
if (capability.kind !== "direct" || !capability.methods.includes(input.mergeMethod)) return {
|
|
374
|
+
ok: false,
|
|
375
|
+
reason: "blocked"
|
|
376
|
+
};
|
|
377
|
+
return sendMutation(transport, "merged", repository.id, {
|
|
378
|
+
pullRequestId: node.id,
|
|
379
|
+
expectedHeadOid: node.headRefOid,
|
|
380
|
+
mergeMethod: input.mergeMethod.toUpperCase()
|
|
381
|
+
}, node);
|
|
382
|
+
}
|
|
383
|
+
if (capability.kind !== "queue") return {
|
|
384
|
+
ok: false,
|
|
385
|
+
reason: "blocked"
|
|
386
|
+
};
|
|
387
|
+
if (!await transport.isSessionCurrent()) return {
|
|
388
|
+
ok: false,
|
|
389
|
+
reason: "no-connection"
|
|
390
|
+
};
|
|
391
|
+
try {
|
|
392
|
+
const result = await transport.enqueue(node.id, node.headRefOid);
|
|
393
|
+
if (result.status === "queued" && result.entry != null) return {
|
|
394
|
+
ok: true,
|
|
395
|
+
status: "queued",
|
|
396
|
+
pullRequest: toSnapshot(node)
|
|
397
|
+
};
|
|
398
|
+
if (result.status !== "failed") return {
|
|
399
|
+
ok: false,
|
|
400
|
+
reason: "outcome-unknown"
|
|
401
|
+
};
|
|
402
|
+
switch (result.reason) {
|
|
403
|
+
case "permission": return {
|
|
404
|
+
ok: false,
|
|
405
|
+
reason: "forbidden"
|
|
406
|
+
};
|
|
407
|
+
case "already-queued":
|
|
408
|
+
case "not-allowed": return {
|
|
409
|
+
ok: false,
|
|
410
|
+
reason: "blocked"
|
|
411
|
+
};
|
|
412
|
+
case "head-changed":
|
|
413
|
+
case "unsupported": return {
|
|
414
|
+
ok: false,
|
|
415
|
+
reason: result.reason
|
|
416
|
+
};
|
|
417
|
+
default: return {
|
|
418
|
+
ok: false,
|
|
419
|
+
reason: "outcome-unknown"
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
} catch (ex) {
|
|
423
|
+
return pullRequestWriteFailure(ex, "enqueue");
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
async function writeGitHubPullRequest(input, transport) {
|
|
427
|
+
if (!isValidWrite(input)) return {
|
|
428
|
+
ok: false,
|
|
429
|
+
reason: "invalid-input"
|
|
430
|
+
};
|
|
431
|
+
try {
|
|
432
|
+
if (input.action === "create") return await createPullRequest(input, transport);
|
|
433
|
+
const resolved = await resolveNode(input, transport.graphql);
|
|
434
|
+
if (!resolved.ok) return resolved;
|
|
435
|
+
const { node, repository } = resolved;
|
|
436
|
+
if (!isRepositoryWritable(repository)) return {
|
|
437
|
+
ok: false,
|
|
438
|
+
reason: "blocked"
|
|
439
|
+
};
|
|
440
|
+
if (input.action === "merge" || input.action === "enqueue") return await mergePullRequest(input, resolved, transport);
|
|
441
|
+
if (input.action === "edit") {
|
|
442
|
+
if (!node.viewerCanUpdate) return {
|
|
443
|
+
ok: false,
|
|
444
|
+
reason: "forbidden"
|
|
445
|
+
};
|
|
446
|
+
return await sendMutation(transport, "updated", repository.id, {
|
|
447
|
+
pullRequestId: node.id,
|
|
448
|
+
title: input.title,
|
|
449
|
+
body: input.body
|
|
450
|
+
}, node);
|
|
451
|
+
}
|
|
452
|
+
const close = input.state === "closed";
|
|
453
|
+
if (node.state !== (close ? "OPEN" : "CLOSED")) return {
|
|
454
|
+
ok: false,
|
|
455
|
+
reason: "blocked"
|
|
456
|
+
};
|
|
457
|
+
if (close ? !node.viewerCanClose : !node.viewerCanReopen) return {
|
|
458
|
+
ok: false,
|
|
459
|
+
reason: "forbidden"
|
|
460
|
+
};
|
|
461
|
+
return await sendMutation(transport, close ? "closed" : "reopened", repository.id, { pullRequestId: node.id }, node);
|
|
462
|
+
} catch (ex) {
|
|
463
|
+
return pullRequestWriteFailure(ex);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
//#endregion
|
|
467
|
+
export { getGitHubPullRequestCreateCapability, pullRequestWriteFailure, resolveGitHubPullRequestWriteTarget, writeGitHubPullRequest };
|
|
468
|
+
|
|
469
|
+
//# sourceMappingURL=pullRequestWrites.js.map
|