1
- import { graphql , GraphqlResponseError } from '@octokit/graphql' ;
2
1
import { Octokit } from '@octokit/rest' ;
3
2
import { $ } from 'execa' ;
4
3
@@ -14,10 +13,6 @@ import { $ } from 'execa';
14
13
* @property {{login: string; association: AuthorAssociation} | null } author
15
14
*/
16
15
17
- /**
18
- * @typedef {import('./github-gql.mjs').CommitConnection } CommitConnection
19
- */
20
-
21
16
/**
22
17
* @param {Object } opts
23
18
* @param {string } opts.cwd
@@ -51,137 +46,7 @@ export async function fetchCommitsBetweenRefs({ org = 'mui', ...options }) {
51
46
}
52
47
const opts = { ...options , org } ;
53
48
54
- /**
55
- * @type {FetchedCommitDetails[] }
56
- */
57
- try {
58
- return fetchCommitsGraphql ( opts ) ;
59
- } catch ( error ) {
60
- let status = 0 ;
61
- if ( error instanceof GraphqlResponseError ) {
62
- if ( error . headers . status ) {
63
- status = parseInt ( error . headers . status , 10 ) ;
64
- // only re-throw for client errors (4xx), for server errors (5xx) we want to fall back to the REST API
65
- if ( status >= 400 && status < 500 ) {
66
- throw error ;
67
- }
68
- }
69
- }
70
- console . warn (
71
- `Failed to fetch commits using the GraphQL API, falling back to the REST API. Status Code: ${ status } ` ,
72
- ) ;
73
- return await fetchCommitsRest ( opts ) ;
74
- }
75
- }
76
-
77
- /**
78
- * Fetches commits between two refs using GitHub's GraphQL API over a single network call.
79
- * Its efficient network-wise but is not as reliable as the REST API (in my findings).
80
- * So keeping both implementations for the time being.
81
- *
82
- * @param {FetchCommitsOptions & {org: string} } param0
83
- * @returns {Promise<FetchedCommitDetails[]> }
84
- */
85
- export async function fetchCommitsGraphql ( { org, token, repo, lastRelease, release } ) {
86
- const gql = graphql . defaults ( {
87
- headers : {
88
- authorization : `token ${ token } ` ,
89
- } ,
90
- } ) ;
91
- /**
92
- * @param {string | null } commitAfter
93
- * @returns {Promise<{repository: {ref: {compare: {commits: CommitConnection}}}}> }
94
- */
95
- async function fetchCommitsPaginated ( commitAfter = null ) {
96
- return await gql ( {
97
- query : `query GetCommitsBetweenRefs($org: String!, $repo: String!, $baseRef: String!, $headRef: String!, $commitCount: Int!, $commitAfter: String) {
98
- repository(owner: $org, name: $repo) {
99
- ref(qualifiedName: $baseRef) {
100
- compare(headRef: $headRef) {
101
- commits(first: $commitCount, after: $commitAfter) {
102
- pageInfo {
103
- hasNextPage
104
- endCursor
105
- }
106
- nodes {
107
- oid
108
- authoredDate
109
- message
110
- author {
111
- user {
112
- login
113
- }
114
- }
115
- associatedPullRequests(first: 1) {
116
- nodes {
117
- number
118
- authorAssociation
119
- author {
120
- login
121
- }
122
- labels(first: 20) {
123
- nodes {
124
- name
125
- }
126
- }
127
- }
128
- }
129
- }
130
- }
131
- }
132
- }
133
- }
134
- }` ,
135
- org,
136
- repo,
137
- commitAfter,
138
- baseRef : lastRelease ,
139
- headRef : release ,
140
- commitCount : 100 ,
141
- } ) ;
142
- }
143
-
144
- let hasNextPage = true ;
145
- /**
146
- * @type {string | null }
147
- */
148
- let commitAfter = null ;
149
- /**
150
- * @type {import('./github-gql.mjs').CommitNode[] }
151
- */
152
- let allCommits = [ ] ;
153
- // fetch all commits (with pagination)
154
- do {
155
- // eslint-disable-next-line no-await-in-loop
156
- const data = await fetchCommitsPaginated ( commitAfter ) ;
157
- const commits = data . repository . ref . compare . commits ;
158
- hasNextPage = ! ! commits . pageInfo . hasNextPage ;
159
- commitAfter = hasNextPage ? commits . pageInfo . endCursor : null ;
160
- allCommits . push ( ...commits . nodes ) ;
161
- } while ( hasNextPage ) ;
162
-
163
- allCommits = allCommits . filter ( ( commit ) => commit . associatedPullRequests . nodes . length > 0 ) ;
164
-
165
- return allCommits . map ( ( commit ) => {
166
- const pr = commit . associatedPullRequests . nodes [ 0 ] ;
167
- const labels = pr . labels . nodes . map ( ( label ) => label . name ) ;
168
-
169
- /**
170
- * @type {FetchedCommitDetails }
171
- */
172
- return {
173
- sha : commit . oid ,
174
- message : commit . message ,
175
- labels,
176
- prNumber : pr . number ,
177
- author : pr . author . user ?. login
178
- ? {
179
- login : pr . author . user . login ,
180
- association : getAuthorAssociation ( pr . authorAssociation ) ,
181
- }
182
- : null ,
183
- } ;
184
- } ) ;
49
+ return await fetchCommitsRest ( opts ) ;
185
50
}
186
51
187
52
/**
@@ -193,7 +58,7 @@ export async function fetchCommitsGraphql({ org, token, repo, lastRelease, relea
193
58
*
194
59
* @returns {Promise<FetchedCommitDetails[]> }
195
60
*/
196
- export async function fetchCommitsRest ( { token, repo, lastRelease, release, org } ) {
61
+ async function fetchCommitsRest ( { token, repo, lastRelease, release, org } ) {
197
62
const octokit = new Octokit ( {
198
63
auth : token ,
199
64
} ) ;
@@ -253,7 +118,7 @@ export async function fetchCommitsRest({ token, repo, lastRelease, release, org
253
118
254
119
/**
255
120
*
256
- * @param {import('./github-gql .mjs').AuthorAssocation } input
121
+ * @param {import('./github-types .mjs').AuthorAssocation } input
257
122
* @returns {AuthorAssociation }
258
123
*/
259
124
function getAuthorAssociation ( input ) {
0 commit comments