@commitspark/git-adapter-github 0.20.0 → 0.21.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.
@@ -1,5 +1,11 @@
1
1
  import { AxiosCacheInstance, CacheAxiosResponse } from 'axios-cache-interceptor'
2
- import { Commit, CommitDraft, Entry } from '@commitspark/git-adapter'
2
+ import {
3
+ Commit,
4
+ CommitDraft,
5
+ Entry,
6
+ ErrorCode,
7
+ GitAdapterError,
8
+ } from '@commitspark/git-adapter'
3
9
  import { GitHubRepositoryOptions } from './index'
4
10
  import {
5
11
  createBlobContentQuery,
@@ -10,6 +16,7 @@ import {
10
16
  import { convertEntriesToActions } from './util/entries-to-actions-converter'
11
17
  import { getPathEntryFolder, getPathSchema } from './util/path-factory'
12
18
  import { createEntriesFromBlobsQueryResponseData } from './util/entry-factory'
19
+ import { handleHttpErrors, handleGraphQLErrors } from './errors'
13
20
 
14
21
  export const API_URL = 'https://api.github.com/graphql'
15
22
 
@@ -22,22 +29,37 @@ export const getEntries = async (
22
29
  const pathEntryFolder = getPathEntryFolder(gitRepositoryOptions)
23
30
 
24
31
  const queryFilesContent = createBlobsContentQuery()
25
- const filesContentResponse = await axiosCacheInstance.post(
26
- API_URL,
27
- {
28
- query: queryFilesContent,
29
- variables: {
30
- repositoryOwner: gitRepositoryOptions.repositoryOwner,
31
- repositoryName: gitRepositoryOptions.repositoryName,
32
- expression: `${commitHash}:${pathEntryFolder}`,
32
+
33
+ let filesContentResponse: CacheAxiosResponse | undefined
34
+ try {
35
+ filesContentResponse = await axiosCacheInstance.post(
36
+ API_URL,
37
+ {
38
+ query: queryFilesContent,
39
+ variables: {
40
+ repositoryOwner: gitRepositoryOptions.repositoryOwner,
41
+ repositoryName: gitRepositoryOptions.repositoryName,
42
+ expression: `${commitHash}:${pathEntryFolder}`,
43
+ },
33
44
  },
34
- },
35
- {
36
- headers: {
37
- authorization: `Bearer ${token}`,
45
+ {
46
+ headers: {
47
+ authorization: `Bearer ${token}`,
48
+ },
38
49
  },
39
- },
40
- )
50
+ )
51
+ } catch (error) {
52
+ handleHttpErrors(error)
53
+ }
54
+
55
+ if (!filesContentResponse) {
56
+ throw new GitAdapterError(
57
+ ErrorCode.INTERNAL_ERROR,
58
+ 'Failed to fetch entries',
59
+ )
60
+ }
61
+
62
+ handleGraphQLErrors(filesContentResponse)
41
63
 
42
64
  if (!filesContentResponse.data.data.repository?.object?.entries) {
43
65
  return []
@@ -59,26 +81,43 @@ export const getSchema = async (
59
81
  const schemaFilePath = getPathSchema(gitRepositoryOptions)
60
82
 
61
83
  const queryContent = createBlobContentQuery()
62
- const response = await axiosCacheInstance.post(
63
- API_URL,
64
- {
65
- query: queryContent,
66
- variables: {
67
- repositoryOwner: repositoryOwner,
68
- repositoryName: repositoryName,
69
- expression: `${commitHash}:${schemaFilePath}`,
84
+
85
+ let response: CacheAxiosResponse | undefined
86
+ try {
87
+ response = await axiosCacheInstance.post(
88
+ API_URL,
89
+ {
90
+ query: queryContent,
91
+ variables: {
92
+ repositoryOwner: repositoryOwner,
93
+ repositoryName: repositoryName,
94
+ expression: `${commitHash}:${schemaFilePath}`,
95
+ },
70
96
  },
71
- },
72
- {
73
- headers: {
74
- authorization: `Bearer ${token}`,
97
+ {
98
+ headers: {
99
+ authorization: `Bearer ${token}`,
100
+ },
75
101
  },
76
- },
77
- )
102
+ )
103
+ } catch (error) {
104
+ handleHttpErrors(error)
105
+ }
106
+
107
+ if (!response) {
108
+ throw new GitAdapterError(
109
+ ErrorCode.INTERNAL_ERROR,
110
+ `Failed to fetch schema`,
111
+ )
112
+ }
113
+
114
+ handleGraphQLErrors(response)
115
+
78
116
  const schema = response.data?.data?.repository?.object?.text
79
117
 
80
118
  if (!schema) {
81
- throw new Error(
119
+ throw new GitAdapterError(
120
+ ErrorCode.NOT_FOUND,
82
121
  `"${schemaFilePath}" not found in Git repository "${repositoryOwner}/${repositoryName}" at commit "${commitHash}"`,
83
122
  )
84
123
  }
@@ -95,26 +134,40 @@ export const getLatestCommitHash = async (
95
134
 
96
135
  const queryLatestCommit = createLatestCommitQuery()
97
136
 
98
- const response = await axiosCacheInstance.post(
99
- API_URL,
100
- {
101
- query: queryLatestCommit,
102
- variables: {
103
- repositoryOwner: gitRepositoryOptions.repositoryOwner,
104
- repositoryName: gitRepositoryOptions.repositoryName,
105
- ref: ref,
137
+ let response: CacheAxiosResponse | undefined
138
+ try {
139
+ response = await axiosCacheInstance.post(
140
+ API_URL,
141
+ {
142
+ query: queryLatestCommit,
143
+ variables: {
144
+ repositoryOwner: gitRepositoryOptions.repositoryOwner,
145
+ repositoryName: gitRepositoryOptions.repositoryName,
146
+ ref: ref,
147
+ },
106
148
  },
107
- },
108
- {
109
- cache: false, // must not use cache, so we always get the branch's current head
110
- headers: {
111
- authorization: `Bearer ${token}`,
149
+ {
150
+ cache: false, // must not use cache, so we always get the branch's current head
151
+ headers: {
152
+ authorization: `Bearer ${token}`,
153
+ },
112
154
  },
113
- },
114
- )
155
+ )
156
+ } catch (error) {
157
+ handleHttpErrors(error)
158
+ }
159
+
160
+ if (!response) {
161
+ throw new GitAdapterError(
162
+ ErrorCode.INTERNAL_ERROR,
163
+ 'Failed to fetch latest commit',
164
+ )
165
+ }
166
+ handleGraphQLErrors(response)
115
167
 
116
168
  if (!response.data.data.repository) {
117
- throw new Error(
169
+ throw new GitAdapterError(
170
+ ErrorCode.NOT_FOUND,
118
171
  `No repository found "${gitRepositoryOptions.repositoryOwner}/${gitRepositoryOptions.repositoryName}"`,
119
172
  )
120
173
  }
@@ -124,7 +177,10 @@ export const getLatestCommitHash = async (
124
177
  response.data.data.repository.object?.oid ??
125
178
  undefined
126
179
  if (!lastCommit) {
127
- throw new Error(`No commit found for ref "${ref}"`)
180
+ throw new GitAdapterError(
181
+ ErrorCode.NOT_FOUND,
182
+ `No commit found for ref "${ref}"`,
183
+ )
128
184
  }
129
185
 
130
186
  return lastCommit
@@ -144,35 +200,50 @@ export const createCommit = async (
144
200
  )
145
201
 
146
202
  const mutateCommit = createCommitMutation()
147
- const response: CacheAxiosResponse = await axiosCacheInstance.post(
148
- API_URL,
149
- {
150
- query: mutateCommit,
151
- variables: {
152
- repositoryNameWithOwner: `${gitRepositoryOptions.repositoryOwner}/${gitRepositoryOptions.repositoryName}`,
153
- branchName: commitDraft.ref,
154
- commitMessage: commitDraft.message ?? '-',
155
- precedingCommitSha: commitDraft.parentSha,
156
- additions: additions,
157
- deletions: deletions,
203
+
204
+ let response: CacheAxiosResponse | undefined
205
+ try {
206
+ response = await axiosCacheInstance.post(
207
+ API_URL,
208
+ {
209
+ query: mutateCommit,
210
+ variables: {
211
+ repositoryNameWithOwner: `${gitRepositoryOptions.repositoryOwner}/${gitRepositoryOptions.repositoryName}`,
212
+ branchName: commitDraft.ref,
213
+ commitMessage: commitDraft.message ?? '-',
214
+ precedingCommitSha: commitDraft.parentSha,
215
+ additions: additions,
216
+ deletions: deletions,
217
+ },
158
218
  },
159
- },
160
- {
161
- cache: false,
162
- headers: {
163
- authorization: `Bearer ${token}`,
219
+ {
220
+ cache: false,
221
+ headers: {
222
+ authorization: `Bearer ${token}`,
223
+ },
164
224
  },
165
- },
166
- )
225
+ )
226
+ } catch (error) {
227
+ handleHttpErrors(error)
228
+ }
167
229
 
168
- if (response.data.errors) {
169
- throw new Error(JSON.stringify(response.data.errors))
230
+ if (!response) {
231
+ throw new GitAdapterError(
232
+ ErrorCode.INTERNAL_ERROR,
233
+ `Failed to create commit`,
234
+ )
170
235
  }
171
236
 
237
+ handleGraphQLErrors(response)
238
+
172
239
  const mutationResult = response.data.data.commitCreate
173
240
 
174
241
  if (mutationResult.errors) {
175
- throw new Error(JSON.stringify(mutationResult.errors))
242
+ const errorMessage = JSON.stringify(mutationResult.errors)
243
+ throw new GitAdapterError(
244
+ ErrorCode.BAD_REQUEST,
245
+ `Failed to create commit: ${errorMessage}`,
246
+ )
176
247
  }
177
248
 
178
249
  return { ref: mutationResult.commit.oid }