@@ -2,17 +2,27 @@ import { Octokit } from 'octokit';
22import type { AuthorAssociation } from '@octokit/webhooks-types' ;
33import { ConversationInput , ConversationPartMessage } from '@gitbook/api' ;
44
5+ type RepoDiscussionParams = {
6+ octokit : Octokit ;
7+ owner : string ;
8+ repo : string ;
9+ } ;
10+
511/**
612 * Get discussions for a repository using GraphQL
713 */
8- export async function getRepoDiscussions (
9- octokit : Octokit ,
10- owner : string ,
11- repo : string ,
12- after ?: string ,
13- first : number = 20 ,
14- states : string [ ] = [ 'CLOSED' ] ,
15- ) : Promise < GitHubDiscussionsResponse > {
14+ export async function getRepoDiscussions ( {
15+ octokit,
16+ owner,
17+ repo,
18+ after,
19+ first = 20 ,
20+ states = [ 'CLOSED' ] ,
21+ } : RepoDiscussionParams & {
22+ after ?: string ;
23+ first ?: number ;
24+ states ?: string [ ] ;
25+ } ) : Promise < GitHubDiscussionsResponse > {
1626 const query = `
1727 ${ DISCUSSION_FRAGMENT }
1828 query getDiscussions($owner: String!, $name: String!, $first: Int!, $after: String, $states: [DiscussionState!]) {
@@ -31,16 +41,14 @@ export async function getRepoDiscussions(
3141 }
3242 ` ;
3343
34- const variables = {
35- owner,
36- name : repo ,
37- first,
38- after,
39- states,
40- } ;
41-
4244 try {
43- const response = await octokit . graphql < GitHubDiscussionsResponse > ( query , variables ) ;
45+ const response = await octokit . graphql < GitHubDiscussionsResponse > ( query , {
46+ owner,
47+ name : repo ,
48+ first,
49+ after,
50+ states,
51+ } ) ;
4452 return response ;
4553 } catch ( error ) {
4654 const errorMessage = error instanceof Error ? error . message : String ( error ) ;
@@ -51,12 +59,14 @@ export async function getRepoDiscussions(
5159/**
5260 * Get a single discussion by number using GraphQL
5361 */
54- export async function getRepoDiscussion (
55- octokit : Octokit ,
56- owner : string ,
57- repo : string ,
58- number : number ,
59- ) : Promise < GitHubSingleDiscussionResponse > {
62+ export async function getRepoDiscussion ( {
63+ octokit,
64+ owner,
65+ repo,
66+ number,
67+ } : RepoDiscussionParams & {
68+ number : number ;
69+ } ) : Promise < GitHubSingleDiscussionResponse > {
6070 const query = `
6171 ${ DISCUSSION_FRAGMENT }
6272 query getSingleDiscussion($owner: String!, $name: String!, $number: Int!) {
@@ -68,14 +78,12 @@ export async function getRepoDiscussion(
6878 }
6979 ` ;
7080
71- const variables = {
72- owner,
73- name : repo ,
74- number,
75- } ;
76-
7781 try {
78- const response = await octokit . graphql < GitHubSingleDiscussionResponse > ( query , variables ) ;
82+ const response = await octokit . graphql < GitHubSingleDiscussionResponse > ( query , {
83+ owner,
84+ name : repo ,
85+ number,
86+ } ) ;
7987 return response ;
8088 } catch ( error ) {
8189 const errorMessage = error instanceof Error ? error . message : String ( error ) ;
@@ -152,6 +160,52 @@ function determineMessageRole(
152160 }
153161}
154162
163+ /**
164+ * GraphQL fragment for discussion fields
165+ */
166+ const DISCUSSION_FRAGMENT = `
167+ fragment DiscussionFields on Discussion {
168+ id
169+ number
170+ title
171+ body
172+ bodyText
173+ url
174+ createdAt
175+ author {
176+ login
177+ }
178+ authorAssociation
179+ comments(first: 50) {
180+ nodes {
181+ body
182+ bodyText
183+ author {
184+ login
185+ }
186+ authorAssociation
187+ isAnswer
188+ replies(first: 10) {
189+ nodes {
190+ body
191+ bodyText
192+ author {
193+ login
194+ }
195+ authorAssociation
196+ }
197+ }
198+ }
199+ }
200+ repository {
201+ name
202+ owner {
203+ login
204+ }
205+ }
206+ }
207+ ` ;
208+
155209/**
156210 * Types matching the GraphQL query structure
157211 * These correspond exactly to the DISCUSSION_FRAGMENT fields
@@ -222,46 +276,3 @@ interface GitHubSingleDiscussionResponse {
222276 discussion : GraphQLDiscussion | null ;
223277 } ;
224278}
225-
226- const DISCUSSION_FRAGMENT = `
227- fragment DiscussionFields on Discussion {
228- id
229- number
230- title
231- body
232- bodyText
233- url
234- createdAt
235- author {
236- login
237- }
238- authorAssociation
239- comments(first: 50) {
240- nodes {
241- body
242- bodyText
243- author {
244- login
245- }
246- authorAssociation
247- isAnswer
248- replies(first: 10) {
249- nodes {
250- body
251- bodyText
252- author {
253- login
254- }
255- authorAssociation
256- }
257- }
258- }
259- }
260- repository {
261- name
262- owner {
263- login
264- }
265- }
266- }
267- ` ;
0 commit comments