@gitkraken/core-gitlens 0.46.0 → 0.47.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.
Files changed (68) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/dist/git-cli/providers/config.js +2 -2
  3. package/dist/git-cli/providers/pausedOperations.js +2 -2
  4. package/dist/git-cli/providers/pausedOperations.js.map +1 -1
  5. package/dist/git-cli/providers/staging.js +1 -1
  6. package/dist/ipc/discovery.js +1 -1
  7. package/dist/plus/agents/providers/claudeCodeTranscript.js +6 -6
  8. package/dist/plus/agents/providers/claudeCodeTranscript.js.map +1 -1
  9. package/dist/plus/ai/providers/openAICompatibleProviderBase.js +1 -1
  10. package/dist/plus/git-github/api/enterprisePullRequestMergePolicy.d.ts +31 -0
  11. package/dist/plus/git-github/api/enterprisePullRequestMergePolicy.js +166 -0
  12. package/dist/plus/git-github/api/enterprisePullRequestMergePolicy.js.map +1 -0
  13. package/dist/plus/git-github/api/github.d.ts +7 -4
  14. package/dist/plus/git-github/api/github.js +44 -14
  15. package/dist/plus/git-github/api/github.js.map +1 -1
  16. package/dist/plus/git-github/api/issueWrites.d.ts +1 -0
  17. package/dist/plus/git-github/api/issueWrites.js +5 -5
  18. package/dist/plus/git-github/api/issueWrites.js.map +1 -1
  19. package/dist/plus/git-github/api/pullRequestWrites.d.ts +7 -3
  20. package/dist/plus/git-github/api/pullRequestWrites.js +120 -67
  21. package/dist/plus/git-github/api/pullRequestWrites.js.map +1 -1
  22. package/dist/plus/integrations/authentication/configuredIntegrationService.d.ts +4 -4
  23. package/dist/plus/integrations/authentication/configuredIntegrationService.js +14 -14
  24. package/dist/plus/integrations/authentication/configuredIntegrationService.js.map +1 -1
  25. package/dist/plus/integrations/authentication/integrationAuthenticationService.js +3 -3
  26. package/dist/plus/integrations/authentication/integrationAuthenticationService.js.map +1 -1
  27. package/dist/plus/integrations/context.d.ts +4 -4
  28. package/dist/plus/integrations/index.d.ts +8 -4
  29. package/dist/plus/integrations/index.js +2 -1
  30. package/dist/plus/integrations/index.js.map +1 -1
  31. package/dist/plus/integrations/integrationService.d.ts +9 -2
  32. package/dist/plus/integrations/integrationService.js +95 -31
  33. package/dist/plus/integrations/integrationService.js.map +1 -1
  34. package/dist/plus/integrations/manager.d.ts +16 -4
  35. package/dist/plus/integrations/models/gitHostIntegration.js +7 -4
  36. package/dist/plus/integrations/models/gitHostIntegration.js.map +1 -1
  37. package/dist/plus/integrations/models/integration.d.ts +2 -1
  38. package/dist/plus/integrations/models/integration.js +11 -4
  39. package/dist/plus/integrations/models/integration.js.map +1 -1
  40. package/dist/plus/integrations/models/issueTrackerWrites.d.ts +128 -0
  41. package/dist/plus/integrations/models/issueTrackerWrites.js +0 -0
  42. package/dist/plus/integrations/models/publicIntegration.d.ts +5 -0
  43. package/dist/plus/integrations/models/publicIntegration.js.map +1 -1
  44. package/dist/plus/integrations/providers/azure/azure.js +1 -1
  45. package/dist/plus/integrations/providers/github.d.ts +19 -6
  46. package/dist/plus/integrations/providers/github.js +126 -53
  47. package/dist/plus/integrations/providers/github.js.map +1 -1
  48. package/dist/plus/integrations/providers/issueSorts.d.ts +1 -0
  49. package/dist/plus/integrations/providers/jira.js +1 -1
  50. package/dist/plus/integrations/providers/jiraIssueByKey.d.ts +2 -0
  51. package/dist/plus/integrations/providers/jiraIssueByKey.js +1 -1
  52. package/dist/plus/integrations/providers/jiraIssueByKey.js.map +1 -1
  53. package/dist/plus/integrations/providers/jiraIssueWriteErrors.d.ts +10 -0
  54. package/dist/plus/integrations/providers/jiraIssueWriteErrors.js +68 -0
  55. package/dist/plus/integrations/providers/jiraIssueWriteErrors.js.map +1 -0
  56. package/dist/plus/integrations/providers/jiraIssueWrites.d.ts +40 -0
  57. package/dist/plus/integrations/providers/jiraIssueWrites.js +397 -0
  58. package/dist/plus/integrations/providers/jiraIssueWrites.js.map +1 -0
  59. package/dist/plus/integrations/providers/providersApi.d.ts +4 -1
  60. package/dist/plus/integrations/providers/providersApi.js +21 -10
  61. package/dist/plus/integrations/providers/providersApi.js.map +1 -1
  62. package/dist/plus/integrations/reads/drains.d.ts +1 -0
  63. package/dist/plus/integrations/utils/integration.utils.d.ts +1 -0
  64. package/dist/plus/integrations/utils/integration.utils.js +7 -1
  65. package/dist/plus/integrations/utils/integration.utils.js.map +1 -1
  66. package/dist/utils/env/node/exec.js +2 -2
  67. package/docs/integrations.md +80 -8
  68. package/package.json +1 -1
@@ -58,5 +58,6 @@ export declare function mutateGitHubIssue(input: GitHubIssueMutation, graphql: G
58
58
  origin?: string;
59
59
  isSessionCurrent?: () => Promise<boolean>;
60
60
  }): Promise<IssueWriteResult>;
61
+ export declare function isGitHubWriteUrlOnInstallation(value: unknown, origin: string | undefined): boolean;
61
62
  //#endregion
62
63
  //# sourceMappingURL=issueWrites.d.ts.map
@@ -138,7 +138,7 @@ async function mutateGitHubIssue(input, graphql, { origin, isSessionCurrent }) {
138
138
  }, origin);
139
139
  }
140
140
  const issue = target?.node;
141
- if (issue?.__typename !== "Issue" || !isIssueOnInstallation(issue, origin) || issue.number !== input.number || issue.repository.id !== repository.id || issue.repository.owner.login.toLowerCase() !== owner.toLowerCase() || issue.repository.name.toLowerCase() !== repo.toLowerCase()) return {
141
+ if (issue?.__typename !== "Issue" || !isGitHubWriteUrlOnInstallation(issue.url, origin) || issue.number !== input.number || issue.repository.id !== repository.id || issue.repository.owner.login.toLowerCase() !== owner.toLowerCase() || issue.repository.name.toLowerCase() !== repo.toLowerCase()) return {
142
142
  ok: false,
143
143
  reason: "not-found"
144
144
  };
@@ -200,14 +200,14 @@ async function writeIssue(graphql, mutation, inputType, input, origin) {
200
200
  const issue = (await graphql(`mutation WriteIssue($input: ${inputType}!) {
201
201
  change: ${mutation}(input: $input) { issue { ${issueFields} } }
202
202
  }`, { input }))?.change?.issue;
203
- return issue != null && isIssueOnInstallation(issue, origin) ? toIssueWriteResult(issue) : {
203
+ return issue != null && isGitHubWriteUrlOnInstallation(issue.url, origin) ? toIssueWriteResult(issue) : {
204
204
  ok: false,
205
205
  reason: "unknown"
206
206
  };
207
207
  }
208
- function isIssueOnInstallation(issue, origin) {
208
+ function isGitHubWriteUrlOnInstallation(value, origin) {
209
209
  if (origin == null) return true;
210
- const url = typeof issue.url === "string" ? URL.parse(issue.url) : null;
210
+ const url = typeof value === "string" ? URL.parse(value) : null;
211
211
  return url?.origin === origin && (url.protocol === "https:" || url.protocol === "http:") && !url.username && !url.password;
212
212
  }
213
213
  function toIssueWriteResult(issue, deletedFromDifferentRepository = false) {
@@ -227,6 +227,6 @@ function toIssueWriteResult(issue, deletedFromDifferentRepository = false) {
227
227
  };
228
228
  }
229
229
  //#endregion
230
- export { isValidGitHubIssueMutation, issueWriteFailure, mutateGitHubIssue };
230
+ export { isGitHubWriteUrlOnInstallation, isValidGitHubIssueMutation, issueWriteFailure, mutateGitHubIssue };
231
231
 
232
232
  //# sourceMappingURL=issueWrites.js.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\t\t/** An unconfirmed result that failed before the mutation could be sent. */\n\t\t\tmutationDispatched?: false;\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\t{ origin, isSessionCurrent }: { origin?: string; isSessionCurrent?: () => Promise<boolean> },\n): Promise<IssueWriteResult> {\n\tlet dispatched = false;\n\tconst mutate: GraphqlRequest = (query, variables) => {\n\t\tdispatched = true;\n\t\treturn graphql(query, variables);\n\t};\n\ttry {\n\t\tconst { owner, repo } = input;\n\t\tconst checkDelete = input.action === 'delete' || (input.action === 'resolve' && input.intent === 'delete');\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!) {\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\t${checkDelete ? 'viewerCanDelete' : ''}\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},\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\tif (isSessionCurrent != null && !(await isSessionCurrent())) {\n\t\t\t\treturn { ok: false, reason: 'no-connection' };\n\t\t\t}\n\n\t\t\treturn await writeIssue(\n\t\t\t\tmutate,\n\t\t\t\t'createIssue',\n\t\t\t\t'CreateIssueInput',\n\t\t\t\t{\n\t\t\t\t\trepositoryId: repository.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\t\torigin,\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\t!isIssueOnInstallation(issue, origin) ||\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\tif (checkDelete && issue.viewerCanDelete !== true) return { ok: false, reason: 'forbidden' };\n\n\t\tif (input.action === 'resolve') return toIssueWriteResult(issue);\n\n\t\tif (isSessionCurrent != null && !(await isSessionCurrent())) {\n\t\t\treturn { ok: false, reason: 'no-connection' };\n\t\t}\n\n\t\tswitch (input.action) {\n\t\t\tcase 'edit':\n\t\t\t\treturn await writeIssue(\n\t\t\t\t\tmutate,\n\t\t\t\t\t'updateIssue',\n\t\t\t\t\t'UpdateIssueInput',\n\t\t\t\t\t{\n\t\t\t\t\t\tid: issue.id,\n\t\t\t\t\t\ttitle: input.title,\n\t\t\t\t\t\tbody: input.body,\n\t\t\t\t\t},\n\t\t\t\t\torigin,\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(mutate, 'reopenIssue', 'ReopenIssueInput', { issueId: issue.id }, origin)\n\t\t\t\t\t: await writeIssue(\n\t\t\t\t\t\t\tmutate,\n\t\t\t\t\t\t\t'closeIssue',\n\t\t\t\t\t\t\t'CloseIssueInput',\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tissueId: issue.id,\n\t\t\t\t\t\t\t\tstateReason: input.reason === 'completed' ? 'COMPLETED' : 'NOT_PLANNED',\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\torigin,\n\t\t\t\t\t\t);\n\t\t\tcase 'delete': {\n\t\t\t\tconst result = await mutate<{ 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\tif (\n\t\t\torigin != null &&\n\t\t\tex instanceof GraphqlResponseError &&\n\t\t\tex.data == null &&\n\t\t\tex.errors?.some(error => {\n\t\t\t\tconst code: unknown = error.extensions?.code;\n\t\t\t\treturn (\n\t\t\t\t\tcode === 'undefinedField' || code === 'argumentNotAccepted' || code === 'variableRequiresValidType'\n\t\t\t\t);\n\t\t\t})\n\t\t) {\n\t\t\treturn { ok: false, reason: 'unsupported' };\n\t\t}\n\n\t\tif (\n\t\t\tdispatched &&\n\t\t\tex instanceof GraphqlResponseError &&\n\t\t\tex.errors?.some(error => error.path != null && error.path.length > 1)\n\t\t) {\n\t\t\treturn { ok: false, reason: 'unknown' };\n\t\t}\n\n\t\tconst failure = issueWriteFailure(ex);\n\t\treturn origin != null && failure.reason === 'unknown' && !dispatched\n\t\t\t? { ...failure, mutationDispatched: false }\n\t\t\t: failure;\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\torigin: 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\tconst issue = result?.change?.issue;\n\treturn issue != null && isIssueOnInstallation(issue, origin)\n\t\t? toIssueWriteResult(issue)\n\t\t: { ok: false, reason: 'unknown' };\n}\n\nfunction isIssueOnInstallation(issue: IssueNode, origin: string | undefined): boolean {\n\tif (origin == null) return true;\n\n\tconst url = typeof issue.url === 'string' ? URL.parse(issue.url) : null;\n\treturn (\n\t\turl?.origin === origin &&\n\t\t(url.protocol === 'https:' || url.protocol === 'http:') &&\n\t\t!url.username &&\n\t\t!url.password\n\t);\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":";;;;AAqDA,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,MAAM,KAC9B,MAAM,SAAS,KACf,MAAM,SAAS,cACf,OAAO,MAAM,mBAAmB,YAChC,CAAC,MAAM,eAAe,KAAK,IAE5B,OAAO;CAGR,QAAQ,MAAM,QAAd;EACC,KAAK,UACJ,OAAO,aAAa,MAAM,KAAK,MAAM,MAAM,SAAS,KAAA,KAAa,OAAO,MAAM,SAAS;EACxF,KAAK,QACJ,QACE,MAAM,UAAU,KAAA,KAAa,MAAM,SAAS,KAAA,OAC5C,MAAM,UAAU,KAAA,KAAa,aAAa,MAAM,KAAK,OACrD,MAAM,SAAS,KAAA,KAAa,OAAO,MAAM,SAAS;EAErD,KAAK,aACJ,OACC,MAAM,UAAU,UACf,MAAM,UAAU,aAAa,MAAM,WAAW,eAAe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ACD;AAEA,eAAe,WACd,SACA,UACA,WACA,OACA,QAC4B;CAO5B,MAAM,SAAQ,MANO,QACpB,+BAA+B,UAAU;aAC9B,SAAS,4BAA4B,YAAY;MAE5D,EAAS,MAAM,CAChB,EAAA,EACsB,QAAQ;CAC9B,OAAO,SAAS,QAAQ,sBAAsB,OAAO,MAAM,IACxD,mBAAmB,KAAK,IACxB;EAAE,IAAI;EAAO,QAAQ;CAAU;AACnC;AAEA,SAAS,sBAAsB,OAAkB,QAAqC;CACrF,IAAI,UAAU,MAAM,OAAO;CAE3B,MAAM,MAAM,OAAO,MAAM,QAAQ,WAAW,IAAI,MAAM,MAAM,GAAG,IAAI;CACnE,OACC,KAAK,WAAW,WACf,IAAI,aAAa,YAAY,IAAI,aAAa,YAC/C,CAAC,IAAI,YACL,CAAC,IAAI;AAEP;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"}
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\t\t/** An unconfirmed result that failed before the mutation could be sent. */\n\t\t\tmutationDispatched?: false;\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\t{ origin, isSessionCurrent }: { origin?: string; isSessionCurrent?: () => Promise<boolean> },\n): Promise<IssueWriteResult> {\n\tlet dispatched = false;\n\tconst mutate: GraphqlRequest = (query, variables) => {\n\t\tdispatched = true;\n\t\treturn graphql(query, variables);\n\t};\n\ttry {\n\t\tconst { owner, repo } = input;\n\t\tconst checkDelete = input.action === 'delete' || (input.action === 'resolve' && input.intent === 'delete');\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!) {\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\t${checkDelete ? 'viewerCanDelete' : ''}\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},\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\tif (isSessionCurrent != null && !(await isSessionCurrent())) {\n\t\t\t\treturn { ok: false, reason: 'no-connection' };\n\t\t\t}\n\n\t\t\treturn await writeIssue(\n\t\t\t\tmutate,\n\t\t\t\t'createIssue',\n\t\t\t\t'CreateIssueInput',\n\t\t\t\t{\n\t\t\t\t\trepositoryId: repository.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\t\torigin,\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\t!isGitHubWriteUrlOnInstallation(issue.url, origin) ||\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\tif (checkDelete && issue.viewerCanDelete !== true) return { ok: false, reason: 'forbidden' };\n\n\t\tif (input.action === 'resolve') return toIssueWriteResult(issue);\n\n\t\tif (isSessionCurrent != null && !(await isSessionCurrent())) {\n\t\t\treturn { ok: false, reason: 'no-connection' };\n\t\t}\n\n\t\tswitch (input.action) {\n\t\t\tcase 'edit':\n\t\t\t\treturn await writeIssue(\n\t\t\t\t\tmutate,\n\t\t\t\t\t'updateIssue',\n\t\t\t\t\t'UpdateIssueInput',\n\t\t\t\t\t{\n\t\t\t\t\t\tid: issue.id,\n\t\t\t\t\t\ttitle: input.title,\n\t\t\t\t\t\tbody: input.body,\n\t\t\t\t\t},\n\t\t\t\t\torigin,\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(mutate, 'reopenIssue', 'ReopenIssueInput', { issueId: issue.id }, origin)\n\t\t\t\t\t: await writeIssue(\n\t\t\t\t\t\t\tmutate,\n\t\t\t\t\t\t\t'closeIssue',\n\t\t\t\t\t\t\t'CloseIssueInput',\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tissueId: issue.id,\n\t\t\t\t\t\t\t\tstateReason: input.reason === 'completed' ? 'COMPLETED' : 'NOT_PLANNED',\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\torigin,\n\t\t\t\t\t\t);\n\t\t\tcase 'delete': {\n\t\t\t\tconst result = await mutate<{ 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\tif (\n\t\t\torigin != null &&\n\t\t\tex instanceof GraphqlResponseError &&\n\t\t\tex.data == null &&\n\t\t\tex.errors?.some(error => {\n\t\t\t\tconst code: unknown = error.extensions?.code;\n\t\t\t\treturn (\n\t\t\t\t\tcode === 'undefinedField' || code === 'argumentNotAccepted' || code === 'variableRequiresValidType'\n\t\t\t\t);\n\t\t\t})\n\t\t) {\n\t\t\treturn { ok: false, reason: 'unsupported' };\n\t\t}\n\n\t\tif (\n\t\t\tdispatched &&\n\t\t\tex instanceof GraphqlResponseError &&\n\t\t\tex.errors?.some(error => error.path != null && error.path.length > 1)\n\t\t) {\n\t\t\treturn { ok: false, reason: 'unknown' };\n\t\t}\n\n\t\tconst failure = issueWriteFailure(ex);\n\t\treturn origin != null && failure.reason === 'unknown' && !dispatched\n\t\t\t? { ...failure, mutationDispatched: false }\n\t\t\t: failure;\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\torigin: 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\tconst issue = result?.change?.issue;\n\treturn issue != null && isGitHubWriteUrlOnInstallation(issue.url, origin)\n\t\t? toIssueWriteResult(issue)\n\t\t: { ok: false, reason: 'unknown' };\n}\n\nexport function isGitHubWriteUrlOnInstallation(value: unknown, origin: string | undefined): boolean {\n\tif (origin == null) return true;\n\n\tconst url = typeof value === 'string' ? URL.parse(value) : null;\n\treturn (\n\t\turl?.origin === origin &&\n\t\t(url.protocol === 'https:' || url.protocol === 'http:') &&\n\t\t!url.username &&\n\t\t!url.password\n\t);\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":";;;;AAqDA,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,MAAM,KAC9B,MAAM,SAAS,KACf,MAAM,SAAS,cACf,OAAO,MAAM,mBAAmB,YAChC,CAAC,MAAM,eAAe,KAAK,IAE5B,OAAO;CAGR,QAAQ,MAAM,QAAd;EACC,KAAK,UACJ,OAAO,aAAa,MAAM,KAAK,MAAM,MAAM,SAAS,KAAA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,cACA,cAAc,wBACd,GAAG,QAAQ,MAAK,UAAS,MAAM,QAAQ,QAAQ,MAAM,KAAK,SAAS,CAAC,GAEpE,OAAO;GAAE,IAAI;GAAO,QAAQ;EAAU;EAGvC,MAAM,UAAU,kBAAkB,EAAE;EACpC,OAAO,UAAU,QAAQ,QAAQ,WAAW,aAAa,CAAC,aACvD;GAAE,GAAG;GAAS,oBAAoB;EAAM,IACxC;CACJ;AACD;AAEA,eAAe,WACd,SACA,UACA,WACA,OACA,QAC4B;CAO5B,MAAM,SAAQ,MANO,QACpB,+BAA+B,UAAU;aAC9B,SAAS,4BAA4B,YAAY;MAE5D,EAAS,MAAM,CAChB,EAAA,EACsB,QAAQ;CAC9B,OAAO,SAAS,QAAQ,+BAA+B,MAAM,KAAK,MAAM,IACrE,mBAAmB,KAAK,IACxB;EAAE,IAAI;EAAO,QAAQ;CAAU;AACnC;AAEA,SAAgB,+BAA+B,OAAgB,QAAqC;CACnG,IAAI,UAAU,MAAM,OAAO;CAE3B,MAAM,MAAM,OAAO,UAAU,WAAW,IAAI,MAAM,KAAK,IAAI;CAC3D,OACC,KAAK,WAAW,WACf,IAAI,aAAa,YAAY,IAAI,aAAa,YAC/C,CAAC,IAAI,YACL,CAAC,IAAI;AAEP;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"}
@@ -1,5 +1,5 @@
1
1
  import { PullRequestMergeMethod, PullRequestMergeQueueActionResult } from "../../../git/models/pullRequest.js";
2
- import { RequestParameters } from "@octokit/types";
2
+ import { EnterprisePullRequestWriteOptions, GraphqlRequest } from "./enterprisePullRequestMergePolicy.js";
3
3
  //#region ../plus/git-github/src/api/pullRequestWrites.d.ts
4
4
  type MergeMethod = `${PullRequestMergeMethod}`;
5
5
  export type GitHubPullRequestCreateTarget = {
@@ -38,6 +38,8 @@ export type PullRequestWriteFailure = {
38
38
  reason: 'unsupported' | 'no-connection' | 'forbidden' | 'not-found' | 'invalid-input' | 'rate-limit' | 'blocked' | 'head-changed' | 'base-changed' | 'unknown' | 'outcome-unknown';
39
39
  };
40
40
  export type PullRequestWriteSnapshot = {
41
+ /** The installation origin for GitHub Enterprise Server pull requests. */
42
+ domain?: string;
41
43
  owner: string;
42
44
  repo: string;
43
45
  number: number;
@@ -80,13 +82,15 @@ export type PullRequestWriteResult = {
80
82
  ok: true;
81
83
  status: 'created' | 'updated' | 'closed' | 'reopened' | 'merged' | 'queued';
82
84
  pullRequest: PullRequestWriteSnapshot;
85
+ /** The result is confirmed, but its snapshot belongs to a replaced or disconnected credential. */
86
+ credentialChanged?: true;
83
87
  } | PullRequestWriteFailure;
84
- type GraphqlRequest = <T>(query: string, variables: RequestParameters) => Promise<T | undefined>;
85
88
  export declare function pullRequestWriteFailure(ex: unknown, phase?: 'write' | 'merge' | 'enqueue'): PullRequestWriteFailure;
86
- export declare function resolveGitHubPullRequestWriteTarget(input: GitHubPullRequestWriteTarget, graphql: GraphqlRequest): Promise<PullRequestWriteTargetResult>;
89
+ export declare function resolveGitHubPullRequestWriteTarget(input: GitHubPullRequestWriteTarget, graphql: GraphqlRequest, enterprise?: EnterprisePullRequestWriteOptions): Promise<PullRequestWriteTargetResult>;
87
90
  export declare function getGitHubPullRequestCreateCapability(input: GitHubPullRequestCreateTarget, graphql: GraphqlRequest): Promise<PullRequestCreateCapabilityResult>;
88
91
  type WriteTransport = {
89
92
  graphql: GraphqlRequest;
93
+ enterprise?: EnterprisePullRequestWriteOptions;
90
94
  isSessionCurrent: () => Promise<boolean>;
91
95
  enqueue: (nodeId: string, headSha: string) => Promise<PullRequestMergeQueueActionResult>;
92
96
  };
@@ -1,5 +1,6 @@
1
1
  import { AuthenticationError, RequestNotFoundError } from "../../../git/errors.js";
2
- import { issueWriteFailure } from "./issueWrites.js";
2
+ import { isGitHubWriteUrlOnInstallation, issueWriteFailure } from "./issueWrites.js";
3
+ import { readEnterprisePullRequestMergePolicy } from "./enterprisePullRequestMergePolicy.js";
3
4
  import { GraphqlResponseError } from "@octokit/graphql";
4
5
  //#region ../plus/git-github/src/api/pullRequestWrites.ts
5
6
  const repositoryIdentity = "id owner { login } name";
@@ -81,7 +82,7 @@ function isRepositoryWritable(repository) {
81
82
  function sameRepository(a, b) {
82
83
  return a.owner.toLowerCase() === b.owner.login.toLowerCase() && a.repo.toLowerCase() === b.name.toLowerCase();
83
84
  }
84
- function toSnapshot(node) {
85
+ function toSnapshot(node, origin) {
85
86
  const closedAt = node.state === "MERGED" ? node.mergedAt ?? node.closedAt : node.closedAt;
86
87
  return {
87
88
  owner: node.repository.owner.login,
@@ -89,6 +90,7 @@ function toSnapshot(node) {
89
90
  number: node.number,
90
91
  nodeId: node.id,
91
92
  url: node.url,
93
+ ...origin != null && { domain: origin },
92
94
  title: node.title,
93
95
  body: node.body,
94
96
  state: node.state === "OPEN" ? "open" : node.state === "CLOSED" ? "closed" : "merged",
@@ -98,21 +100,20 @@ function toSnapshot(node) {
98
100
  baseRef: node.baseRefName
99
101
  };
100
102
  }
101
- async function resolveNode(input, graphql) {
103
+ async function resolveNode(input, graphql, origin) {
102
104
  const { owner, repo, expectedNodeId, number } = input;
103
105
  const result = await graphql(`query PullRequestWriteTarget($owner: String!, $repo: String!, $expectedNodeId: ID!) {
104
106
  repository(owner: $owner, name: $repo, followRenames: false) {
105
107
  ${repositoryFields}
106
- mergeCommitAllowed squashMergeAllowed rebaseMergeAllowed viewerDefaultMergeMethod
108
+ mergeCommitAllowed squashMergeAllowed rebaseMergeAllowed ${origin == null ? "viewerDefaultMergeMethod" : ""}
107
109
  }
108
110
  node(id: $expectedNodeId) {
109
111
  __typename
110
112
  ... on PullRequest {
111
113
  ${pullRequestFields}
112
114
  viewerCanUpdate viewerCanClose viewerCanReopen
113
- isDraft isInMergeQueue isMergeQueueEnabled
114
- stack { id }
115
- baseRef { id }
115
+ isDraft baseRef { id }
116
+ ${origin == null ? "isInMergeQueue isMergeQueueEnabled stack { id }" : ""}
116
117
  }
117
118
  }
118
119
  }`, {
@@ -122,7 +123,7 @@ async function resolveNode(input, graphql) {
122
123
  });
123
124
  const repository = result?.repository;
124
125
  const node = result?.node;
125
- if (repository == null || !sameRepository(input, repository) || node?.__typename !== "PullRequest" || node.number !== number || node.repository.id !== repository.id) return {
126
+ if (repository == null || !sameRepository(input, repository) || node?.__typename !== "PullRequest" || node.number !== number || node.repository.id !== repository.id || !sameRepository(input, node.repository) || !isGitHubWriteUrlOnInstallation(node.url, origin)) return {
126
127
  ok: false,
127
128
  reason: "not-found"
128
129
  };
@@ -184,7 +185,8 @@ async function readBranchRules(refId, graphql) {
184
185
  cursor = nextCursor;
185
186
  }
186
187
  }
187
- async function getMergeCapability({ node, repository }, graphql) {
188
+ async function getMergeCapability(resolved, graphql, enterprise) {
189
+ const { node, repository } = resolved;
188
190
  if (!isRepositoryWritable(repository) || node.state !== "OPEN" || node.isDraft || node.isInMergeQueue || node.baseRef == null) return {
189
191
  kind: "unavailable",
190
192
  reason: "blocked"
@@ -200,30 +202,24 @@ async function getMergeCapability({ node, repository }, graphql) {
200
202
  };
201
203
  if (node.isMergeQueueEnabled) return { kind: "queue" };
202
204
  try {
203
- const branch = await readBranchRules(node.baseRef.id, graphql);
204
- if (branch == null) return {
205
+ if (enterprise != null) {
206
+ const policy = await readEnterprisePullRequestMergePolicy({
207
+ id: node.id,
208
+ baseRef: node.baseRefName,
209
+ baseRefId: node.baseRef.id
210
+ }, {
211
+ owner: repository.owner.login,
212
+ repo: repository.name
213
+ }, graphql, enterprise.readBranchRules);
214
+ return "kind" in policy ? policy : getDirectMergeCapability(repository, policy);
215
+ }
216
+ const policy = await readBranchRules(node.baseRef.id, graphql);
217
+ if (policy == null) return {
205
218
  kind: "unavailable",
206
219
  reason: "unknown"
207
220
  };
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
- };
221
+ if (policy.rules.some((rule) => rule.type === "MERGE_QUEUE")) return { kind: "queue" };
222
+ return getDirectMergeCapability(repository, policy);
227
223
  } catch (ex) {
228
224
  return {
229
225
  kind: "unavailable",
@@ -231,24 +227,43 @@ async function getMergeCapability({ node, repository }, graphql) {
231
227
  };
232
228
  }
233
229
  }
234
- async function resolveGitHubPullRequestWriteTarget(input, graphql) {
230
+ function getDirectMergeCapability(repository, { rules, requiresLinearHistory }) {
231
+ const linearHistory = requiresLinearHistory || rules.some((rule) => rule.type === "REQUIRED_LINEAR_HISTORY");
232
+ const methods = mergeMethods.filter((method) => {
233
+ return (method === "merge" ? repository.mergeCommitAllowed && !linearHistory : method === "squash" ? repository.squashMergeAllowed : repository.rebaseMergeAllowed) && rules.every((rule) => {
234
+ const permitted = rule.type === "PULL_REQUEST" ? rule.parameters?.allowedMergeMethods : void 0;
235
+ return permitted == null || permitted.some((allowedMethod) => allowedMethod.toLowerCase() === method);
236
+ });
237
+ });
238
+ if (methods.length === 0) return {
239
+ kind: "unavailable",
240
+ reason: "blocked"
241
+ };
242
+ const preference = repository.viewerDefaultMergeMethod?.toLowerCase();
243
+ return {
244
+ kind: "direct",
245
+ methods,
246
+ defaultMethod: methods.find((method) => method === preference)
247
+ };
248
+ }
249
+ async function resolveGitHubPullRequestWriteTarget(input, graphql, enterprise) {
235
250
  if (!isValidTarget(input)) return {
236
251
  ok: false,
237
252
  reason: "invalid-input"
238
253
  };
239
254
  try {
240
- const resolved = await resolveNode(input, graphql);
255
+ const resolved = await resolveNode(input, graphql, enterprise?.origin);
241
256
  if (!resolved.ok) return resolved;
242
257
  const { node, repository } = resolved;
243
258
  const writable = isRepositoryWritable(repository);
244
259
  return {
245
260
  ok: true,
246
- pullRequest: toSnapshot(node),
261
+ pullRequest: toSnapshot(node, enterprise?.origin),
247
262
  capabilities: {
248
263
  edit: writable && node.viewerCanUpdate,
249
264
  close: writable && node.state === "OPEN" && node.viewerCanClose,
250
265
  reopen: writable && node.state === "CLOSED" && node.viewerCanReopen,
251
- merge: await getMergeCapability(resolved, graphql)
266
+ merge: await getMergeCapability(resolved, graphql, enterprise)
252
267
  }
253
268
  };
254
269
  } catch (ex) {
@@ -287,38 +302,55 @@ const mutations = {
287
302
  updated: ["updatePullRequest", "UpdatePullRequestInput"],
288
303
  closed: ["closePullRequest", "ClosePullRequestInput"],
289
304
  reopened: ["reopenPullRequest", "ReopenPullRequestInput"],
290
- merged: ["mergePullRequest", "MergePullRequestInput"]
305
+ merged: ["mergePullRequest", "MergePullRequestInput"],
306
+ queued: ["enqueuePullRequest", "EnqueuePullRequestInput"]
291
307
  };
292
- async function sendMutation(transport, status, repositoryId, values, previous) {
308
+ async function sendMutation(transport, status, repository, values, previous) {
293
309
  if (!await transport.isSessionCurrent()) return {
294
310
  ok: false,
295
311
  reason: "no-connection"
296
312
  };
297
313
  const [mutation, inputType] = mutations[status];
298
- let node;
314
+ const pullRequest = `pullRequest { ${status === "created" && transport.enterprise?.origin != null ? `${pullRequestFields} headRefName` : pullRequestFields} }`;
315
+ const selection = status === "queued" ? `mergeQueueEntry { id ${pullRequest} }` : pullRequest;
316
+ let payload;
317
+ let error;
299
318
  try {
300
- node = (await transport.graphql(`mutation WritePullRequest($input: ${inputType}!) {
301
- change: ${mutation}(input: $input) { pullRequest { ${pullRequestFields} } }
302
- }`, { input: values }))?.change?.pullRequest;
319
+ payload = await transport.graphql(`mutation WritePullRequest($input: ${inputType}!) {
320
+ change: ${mutation}(input: $input) { ${selection} }
321
+ }`, { input: values });
303
322
  } 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");
323
+ error = ex;
324
+ if (ex instanceof GraphqlResponseError) payload = ex.data;
312
325
  }
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 {
326
+ const entry = payload?.change?.mergeQueueEntry;
327
+ const node = status === "queued" ? typeof entry?.id === "string" && entry.id ? entry.pullRequest : void 0 : payload?.change?.pullRequest;
328
+ if (node == null && error != null) {
329
+ if (error instanceof GraphqlResponseError && error.errors?.some((item) => item.path?.[0] === "change" && item.path.length > 1)) return {
330
+ ok: false,
331
+ reason: "outcome-unknown"
332
+ };
333
+ return pullRequestWriteFailure(error, status === "merged" ? "merge" : status === "queued" ? "enqueue" : "write");
334
+ }
335
+ const expectedState = status === "created" || status === "reopened" || status === "queued" ? "OPEN" : status === "closed" ? "CLOSED" : status === "merged" ? "MERGED" : void 0;
336
+ if (node?.repository?.id !== repository.id || 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" || !isGitHubWriteUrlOnInstallation(node.url, transport.enterprise?.origin) || node.state !== "OPEN" && node.state !== "CLOSED" && node.state !== "MERGED") return {
315
337
  ok: false,
316
338
  reason: "outcome-unknown"
317
339
  };
340
+ if (transport.enterprise?.origin != null) {
341
+ if (status === "created" && (node.baseRefName !== values.baseRefName || node.headRefName !== values.headRefName)) return {
342
+ ok: false,
343
+ reason: "outcome-unknown"
344
+ };
345
+ if ((status === "merged" || status === "queued") && (node.baseRefName !== previous?.baseRefName || node.headRefOid.toLowerCase() !== previous.headRefOid.toLowerCase())) return {
346
+ ok: false,
347
+ reason: "outcome-unknown"
348
+ };
349
+ }
318
350
  return {
319
351
  ok: true,
320
352
  status,
321
- pullRequest: toSnapshot(node)
353
+ pullRequest: toSnapshot(node, transport.enterprise?.origin)
322
354
  };
323
355
  }
324
356
  async function createPullRequest(input, transport) {
@@ -346,7 +378,7 @@ async function createPullRequest(input, transport) {
346
378
  ok: false,
347
379
  reason: "invalid-input"
348
380
  };
349
- return sendMutation(transport, "created", repository.id, {
381
+ return sendMutation(transport, "created", repository, {
350
382
  repositoryId: repository.id,
351
383
  baseRefName: input.baseRef,
352
384
  headRefName: input.headRef,
@@ -364,26 +396,47 @@ async function mergePullRequest(input, resolved, transport) {
364
396
  ok: false,
365
397
  reason: "base-changed"
366
398
  };
367
- const capability = await getMergeCapability(resolved, transport.graphql);
399
+ const capability = await getMergeCapability(resolved, transport.graphql, transport.enterprise);
368
400
  if (capability.kind === "unavailable") return {
369
401
  ok: false,
370
402
  reason: capability.reason
371
403
  };
372
- if (input.action === "merge") {
373
- if (capability.kind !== "direct" || !capability.methods.includes(input.mergeMethod)) return {
404
+ if (input.action === "merge" ? capability.kind !== "direct" || !capability.methods.includes(input.mergeMethod) : capability.kind !== "queue") return {
405
+ ok: false,
406
+ reason: "blocked"
407
+ };
408
+ if (transport.enterprise?.origin != null) {
409
+ const current = await resolveNode({
410
+ ...input,
411
+ expectedNodeId: node.id
412
+ }, transport.graphql, transport.enterprise.origin);
413
+ if (!current.ok) return current;
414
+ if (current.node.id !== node.id || current.repository.id !== repository.id) return {
415
+ ok: false,
416
+ reason: "not-found"
417
+ };
418
+ if (current.node.baseRefName !== input.expectedBaseRef || current.node.baseRef?.id !== node.baseRef?.id) return {
419
+ ok: false,
420
+ reason: "base-changed"
421
+ };
422
+ if (current.node.headRefOid.toLowerCase() !== input.expectedHeadSha.toLowerCase()) return {
423
+ ok: false,
424
+ reason: "head-changed"
425
+ };
426
+ if (current.node.state !== "OPEN" || current.node.isDraft || !isRepositoryWritable(current.repository)) return {
374
427
  ok: false,
375
428
  reason: "blocked"
376
429
  };
377
- return sendMutation(transport, "merged", repository.id, {
378
- pullRequestId: node.id,
379
- expectedHeadOid: node.headRefOid,
380
- mergeMethod: input.mergeMethod.toUpperCase()
381
- }, node);
382
430
  }
383
- if (capability.kind !== "queue") return {
384
- ok: false,
385
- reason: "blocked"
431
+ const values = {
432
+ pullRequestId: node.id,
433
+ expectedHeadOid: node.headRefOid
386
434
  };
435
+ if (input.action === "merge") return sendMutation(transport, "merged", repository, {
436
+ ...values,
437
+ mergeMethod: input.mergeMethod.toUpperCase()
438
+ }, node);
439
+ if (transport.enterprise?.origin != null) return sendMutation(transport, "queued", repository, values, node);
387
440
  if (!await transport.isSessionCurrent()) return {
388
441
  ok: false,
389
442
  reason: "no-connection"
@@ -424,13 +477,13 @@ async function mergePullRequest(input, resolved, transport) {
424
477
  }
425
478
  }
426
479
  async function writeGitHubPullRequest(input, transport) {
427
- if (!isValidWrite(input)) return {
480
+ if (!isValidWrite(input) || transport.enterprise?.origin != null && input.action === "create" && input.expectedRepositoryId == null) return {
428
481
  ok: false,
429
482
  reason: "invalid-input"
430
483
  };
431
484
  try {
432
485
  if (input.action === "create") return await createPullRequest(input, transport);
433
- const resolved = await resolveNode(input, transport.graphql);
486
+ const resolved = await resolveNode(input, transport.graphql, transport.enterprise?.origin);
434
487
  if (!resolved.ok) return resolved;
435
488
  const { node, repository } = resolved;
436
489
  if (!isRepositoryWritable(repository)) return {
@@ -443,7 +496,7 @@ async function writeGitHubPullRequest(input, transport) {
443
496
  ok: false,
444
497
  reason: "forbidden"
445
498
  };
446
- return await sendMutation(transport, "updated", repository.id, {
499
+ return await sendMutation(transport, "updated", repository, {
447
500
  pullRequestId: node.id,
448
501
  title: input.title,
449
502
  body: input.body
@@ -458,7 +511,7 @@ async function writeGitHubPullRequest(input, transport) {
458
511
  ok: false,
459
512
  reason: "forbidden"
460
513
  };
461
- return await sendMutation(transport, close ? "closed" : "reopened", repository.id, { pullRequestId: node.id }, node);
514
+ return await sendMutation(transport, close ? "closed" : "reopened", repository, { pullRequestId: node.id }, node);
462
515
  } catch (ex) {
463
516
  return pullRequestWriteFailure(ex);
464
517
  }