| 1 | import { Injectable } from '@nestjs/common'; |
| 2 | |
| 3 | import { parseIssueSearch } |
| 4 | import type { Loaders |
| 5 | import { IssuesService } |
| 6 | import type { GithubViewer |
| 7 | import { issueCursor } |
| 8 | import { SearchType } |
| 9 | import { IssueNodesService } |
| 10 | import { RepositoryNodesService } |
| 11 | |
| 12 | const EMPTY = { |
| 13 | issueCount: 0, |
| 14 | nodes: [], |
| 15 | edges: [], |
| 16 | pageInfo: { |
| 17 | hasNextPage: false, |
| 18 | hasPreviousPage: false, |
| 19 | startCursor: null, |
| 20 | endCursor: null, |
| 21 | }, |
| 22 | }; |
| 23 | |
| 24 | @Injectable() |
| 25 | export class GithubSearchService |
| 26 | constructor( |
| 27 | private readonly issues |
| 28 | private readonly repositories |
| 29 | private readonly issueNodes |
| 30 | ) {} |
| 31 | |
| 32 | async search( |
| 33 | type: SearchType |
| 34 | query: string, |
| 35 | viewer: GithubViewer |
| 36 | loaders: Loaders |
| 37 | first?: number |
| 38 | last?: number |
| 39 | after?: string |
| 40 | ) { |
| 41 | const search = |
| 42 | // ponytail: only repository-scoped issue search; global search across repositories needs its own query over every readable repository. Ghost has no review requests, so `review-requested:` matches nothing. |
| 43 | if (type !== |
| 44 | return EMPTY; |
| 45 | const repository = |
| 46 | search.repo.owner |
| 47 | search.repo.name |
| 48 | viewer, |
| 49 | ); |
| 50 | const viewerLogin = |
| 51 | ? ((await loaders |
| 52 | : undefined; |
| 53 | const me = |
| 54 | login === '@me' |
| 55 | const page = |
| 56 | { |
| 57 | username: repository.ownerLogin |
| 58 | repo: repository.slug |
| 59 | requesterId: viewer?.userId |
| 60 | query: { |
| 61 | state: search.state |
| 62 | author: me(search |
| 63 | assignee: me(search |
| 64 | labels: search.labels |
| 65 | q: search.text |
| 66 | cursor: after, |
| 67 | limit: Math.min |
| 68 | }, |
| 69 | }, |
| 70 | search.isPullRequest === |
| 71 | ? { |
| 72 | pullRequests: { |
| 73 | states: |
| 74 | search.merged === |
| 75 | ? undefined |
| 76 | : search.merged |
| 77 | ? ['merged'] |
| 78 | : ['open', ' |
| 79 | }, |
| 80 | } |
| 81 | : { includePullRequests: search |
| 82 | ); |
| 83 | const nodes = |
| 84 | return { |
| 85 | issueCount: |
| 86 | search.state === |
| 87 | ? page.openCount |
| 88 | : search.state |
| 89 | ? page.closedCount |
| 90 | : page.total, |
| 91 | nodes, |
| 92 | edges: nodes.map |
| 93 | node, |
| 94 | cursor: issueCursor(page |
| 95 | })), |
| 96 | pageInfo: { |
| 97 | hasNextPage: page.hasMore |
| 98 | hasPreviousPage: !!after, |
| 99 | startCursor: null, |
| 100 | endCursor: page.nextCursor |
| 101 | }, |
| 102 | }; |
| 103 | } |
| 104 | } |