@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.
- package/CHANGELOG.md +6 -0
- package/README.md +39 -0
- package/dist/cjs/errors.js +58 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/github-adapter.js +99 -59
- package/dist/cjs/github-adapter.js.map +1 -1
- package/dist/esm/errors.js +51 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/github-adapter.js +99 -59
- package/dist/esm/github-adapter.js.map +1 -1
- package/dist/tsconfig.build.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.build.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.build.types.tsbuildinfo +1 -1
- package/dist/types/errors.d.ts +4 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/github-adapter.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/errors.ts +54 -0
- package/src/github-adapter.ts +139 -68
package/src/github-adapter.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { AxiosCacheInstance, CacheAxiosResponse } from 'axios-cache-interceptor'
|
|
2
|
-
import {
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
37
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
74
|
-
|
|
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
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
|
169
|
-
throw new
|
|
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
|
-
|
|
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 }
|