@@ -101,12 +101,24 @@ describe('gitea API', () => {
101
101
} ) ;
102
102
103
103
describe ( 'persistFiles' , ( ) => {
104
- it ( 'should check if file exists and create a new file ' , async ( ) => {
104
+ it ( 'should create a new commit ' , async ( ) => {
105
105
const api = new API ( { branch : 'master' , repo : 'owner/repo' } ) ;
106
106
107
107
const responses = {
108
- '/repos/owner/repo/contents/content/posts/new-post.md' : ( ) => ( {
108
+ '/repos/owner/repo/git/trees/master:content%2Fposts' : ( ) => {
109
+ return { tree : [ { path : 'update-post.md' , sha : 'old-sha' } ] } ;
110
+ } ,
111
+
112
+ '/repos/owner/repo/contents' : ( ) => ( {
109
113
commit : { sha : 'new-sha' } ,
114
+ files : [
115
+ {
116
+ path : 'content/posts/new-post.md' ,
117
+ } ,
118
+ {
119
+ path : 'content/posts/update-post.md' ,
120
+ } ,
121
+ ] ,
110
122
} ) ,
111
123
} ;
112
124
mockAPI ( api , responses ) ;
@@ -115,85 +127,142 @@ describe('gitea API', () => {
115
127
dataFiles : [
116
128
{
117
129
slug : 'entry' ,
118
- sha : 'abc' ,
119
130
path : 'content/posts/new-post.md' ,
120
131
raw : 'content' ,
121
132
} ,
133
+ {
134
+ slug : 'entry' ,
135
+ sha : 'old-sha' ,
136
+ path : 'content/posts/update-post.md' ,
137
+ raw : 'content' ,
138
+ } ,
122
139
] ,
123
140
assets : [ ] ,
124
141
} ;
125
- await api . persistFiles ( entry . dataFiles , entry . assets , {
126
- commitMessage : 'commitMessage' ,
127
- newEntry : true ,
142
+ await expect (
143
+ api . persistFiles ( entry . dataFiles , entry . assets , {
144
+ commitMessage : 'commitMessage' ,
145
+ newEntry : true ,
146
+ } ) ,
147
+ ) . resolves . toEqual ( {
148
+ commit : { sha : 'new-sha' } ,
149
+ files : [
150
+ {
151
+ path : 'content/posts/new-post.md' ,
152
+ } ,
153
+ {
154
+ path : 'content/posts/update-post.md' ,
155
+ } ,
156
+ ] ,
128
157
} ) ;
129
158
130
- expect ( api . request ) . toHaveBeenCalledTimes ( 2 ) ;
159
+ expect ( api . request ) . toHaveBeenCalledTimes ( 3 ) ;
131
160
132
161
expect ( ( api . request as jest . Mock ) . mock . calls [ 0 ] ) . toEqual ( [
133
162
'/repos/owner/repo/git/trees/master:content%2Fposts' ,
134
163
] ) ;
135
164
136
165
expect ( ( api . request as jest . Mock ) . mock . calls [ 1 ] ) . toEqual ( [
137
- '/repos/owner/repo/contents/content/posts/new-post.md' ,
166
+ '/repos/owner/repo/git/trees/master:content%2Fposts' ,
167
+ ] ) ;
168
+
169
+ expect ( ( api . request as jest . Mock ) . mock . calls [ 2 ] ) . toEqual ( [
170
+ '/repos/owner/repo/contents' ,
138
171
{
139
172
method : 'POST' ,
140
173
body : JSON . stringify ( {
141
174
branch : 'master' ,
142
- content : Base64 . encode ( entry . dataFiles [ 0 ] . raw ) ,
175
+ files : [
176
+ {
177
+ operation : 'create' ,
178
+ content : Base64 . encode ( entry . dataFiles [ 0 ] . raw ) ,
179
+ path : entry . dataFiles [ 0 ] . path ,
180
+ } ,
181
+ {
182
+ operation : 'update' ,
183
+ content : Base64 . encode ( entry . dataFiles [ 1 ] . raw ) ,
184
+ path : entry . dataFiles [ 1 ] . path ,
185
+ sha : entry . dataFiles [ 1 ] . sha ,
186
+ } ,
187
+ ] ,
143
188
message : 'commitMessage' ,
144
- signoff : false ,
145
189
} ) ,
146
190
} ,
147
191
] ) ;
148
192
} ) ;
149
- it ( 'should get the file sha and update the file' , async ( ) => {
150
- jest . clearAllMocks ( ) ;
193
+ } ) ;
194
+
195
+ describe ( 'deleteFiles' , ( ) => {
196
+ it ( 'should check if files exist and delete them' , async ( ) => {
151
197
const api = new API ( { branch : 'master' , repo : 'owner/repo' } ) ;
152
198
153
199
const responses = {
154
200
'/repos/owner/repo/git/trees/master:content%2Fposts' : ( ) => {
155
- return { tree : [ { path : 'update-post.md' , sha : 'old-sha' } ] } ;
201
+ return {
202
+ tree : [
203
+ { path : 'delete-post-1.md' , sha : 'old-sha-1' } ,
204
+ { path : 'delete-post-2.md' , sha : 'old-sha-2' } ,
205
+ ] ,
206
+ } ;
156
207
} ,
157
208
158
- '/repos/owner/repo/contents/content/posts/update-post.md' : ( ) => {
159
- return { commit : { sha : 'updated-sha' } } ;
160
- } ,
209
+ '/repos/owner/repo/contents' : ( ) => ( {
210
+ commit : { sha : 'new-sha' } ,
211
+ files : [
212
+ {
213
+ path : 'content/posts/delete-post-1.md' ,
214
+ } ,
215
+ {
216
+ path : 'content/posts/delete-post-2.md' ,
217
+ } ,
218
+ ] ,
219
+ } ) ,
161
220
} ;
162
221
mockAPI ( api , responses ) ;
163
222
164
- const entry = {
165
- dataFiles : [
223
+ const deleteFiles = [ 'content/posts/delete-post-1.md' , 'content/posts/delete-post-2.md' ] ;
224
+
225
+ await expect ( api . deleteFiles ( deleteFiles , 'commitMessage' ) ) . resolves . toEqual ( {
226
+ commit : { sha : 'new-sha' } ,
227
+ files : [
166
228
{
167
- slug : 'entry ' ,
168
- sha : 'abc' ,
169
- path : 'content/posts/update-post.md' ,
170
- raw : 'content' ,
229
+ path : 'content/posts/delete-post-1.md ' ,
230
+ } ,
231
+ {
232
+ path : 'content/posts/delete-post-2.md ' ,
171
233
} ,
172
234
] ,
173
- assets : [ ] ,
174
- } ;
175
-
176
- await api . persistFiles ( entry . dataFiles , entry . assets , {
177
- commitMessage : 'commitMessage' ,
178
- newEntry : false ,
179
235
} ) ;
180
236
181
- expect ( api . request ) . toHaveBeenCalledTimes ( 2 ) ;
237
+ expect ( api . request ) . toHaveBeenCalledTimes ( 3 ) ;
182
238
183
239
expect ( ( api . request as jest . Mock ) . mock . calls [ 0 ] ) . toEqual ( [
184
240
'/repos/owner/repo/git/trees/master:content%2Fposts' ,
185
241
] ) ;
186
242
187
243
expect ( ( api . request as jest . Mock ) . mock . calls [ 1 ] ) . toEqual ( [
188
- '/repos/owner/repo/contents/content/posts/update-post.md' ,
244
+ '/repos/owner/repo/git/trees/master:content%2Fposts' ,
245
+ ] ) ;
246
+
247
+ expect ( ( api . request as jest . Mock ) . mock . calls [ 2 ] ) . toEqual ( [
248
+ '/repos/owner/repo/contents' ,
189
249
{
190
- method : 'PUT ' ,
250
+ method : 'POST ' ,
191
251
body : JSON . stringify ( {
192
252
branch : 'master' ,
193
- content : Base64 . encode ( entry . dataFiles [ 0 ] . raw ) ,
253
+ files : [
254
+ {
255
+ operation : 'delete' ,
256
+ path : deleteFiles [ 0 ] ,
257
+ sha : 'old-sha-1' ,
258
+ } ,
259
+ {
260
+ operation : 'delete' ,
261
+ path : deleteFiles [ 1 ] ,
262
+ sha : 'old-sha-2' ,
263
+ } ,
264
+ ] ,
194
265
message : 'commitMessage' ,
195
- sha : 'old-sha' ,
196
- signoff : false ,
197
266
} ) ,
198
267
} ,
199
268
] ) ;
0 commit comments