Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit 0bea971

Browse files
committed
restore tests
1 parent ab2866d commit 0bea971

File tree

3 files changed

+155
-43
lines changed

3 files changed

+155
-43
lines changed

packages/core/src/backends/gitea/API.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { ApiRequest, FetchError } from '@staticcms/core/lib/util';
1818
import type AssetProxy from '@staticcms/core/valueObjects/AssetProxy';
1919
import type { Semaphore } from 'semaphore';
2020
import type {
21+
FilesResponse,
2122
GitGetBlobResponse,
2223
GitGetTreeResponse,
2324
GiteaUser,
@@ -372,14 +373,14 @@ export default class API {
372373
}
373374

374375
async changeFiles(operations: ChangeFileOperation[], options: PersistOptions) {
375-
await this.request(`${this.repoURL}/contents`, {
376+
return (await this.request(`${this.repoURL}/contents`, {
376377
method: 'POST',
377378
body: JSON.stringify({
378379
branch: this.branch,
379380
files: operations,
380381
message: options.commitMessage,
381382
}),
382-
});
383+
})) as FilesResponse;
383384
}
384385

385386
async getChangeFileOperations(files: { path: string; newPath?: string }[], branch: string) {

packages/core/src/backends/gitea/__tests__/API.spec.ts

Lines changed: 103 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,24 @@ describe('gitea API', () => {
101101
});
102102

103103
describe('persistFiles', () => {
104-
it('should check if file exists and create a new file', async () => {
104+
it('should create a new commit', async () => {
105105
const api = new API({ branch: 'master', repo: 'owner/repo' });
106106

107107
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': () => ({
109113
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+
],
110122
}),
111123
};
112124
mockAPI(api, responses);
@@ -115,85 +127,142 @@ describe('gitea API', () => {
115127
dataFiles: [
116128
{
117129
slug: 'entry',
118-
sha: 'abc',
119130
path: 'content/posts/new-post.md',
120131
raw: 'content',
121132
},
133+
{
134+
slug: 'entry',
135+
sha: 'old-sha',
136+
path: 'content/posts/update-post.md',
137+
raw: 'content',
138+
},
122139
],
123140
assets: [],
124141
};
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+
],
128157
});
129158

130-
expect(api.request).toHaveBeenCalledTimes(2);
159+
expect(api.request).toHaveBeenCalledTimes(3);
131160

132161
expect((api.request as jest.Mock).mock.calls[0]).toEqual([
133162
'/repos/owner/repo/git/trees/master:content%2Fposts',
134163
]);
135164

136165
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',
138171
{
139172
method: 'POST',
140173
body: JSON.stringify({
141174
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+
],
143188
message: 'commitMessage',
144-
signoff: false,
145189
}),
146190
},
147191
]);
148192
});
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 () => {
151197
const api = new API({ branch: 'master', repo: 'owner/repo' });
152198

153199
const responses = {
154200
'/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+
};
156207
},
157208

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+
}),
161220
};
162221
mockAPI(api, responses);
163222

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: [
166228
{
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',
171233
},
172234
],
173-
assets: [],
174-
};
175-
176-
await api.persistFiles(entry.dataFiles, entry.assets, {
177-
commitMessage: 'commitMessage',
178-
newEntry: false,
179235
});
180236

181-
expect(api.request).toHaveBeenCalledTimes(2);
237+
expect(api.request).toHaveBeenCalledTimes(3);
182238

183239
expect((api.request as jest.Mock).mock.calls[0]).toEqual([
184240
'/repos/owner/repo/git/trees/master:content%2Fposts',
185241
]);
186242

187243
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',
189249
{
190-
method: 'PUT',
250+
method: 'POST',
191251
body: JSON.stringify({
192252
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+
],
194265
message: 'commitMessage',
195-
sha: 'old-sha',
196-
signoff: false,
197266
}),
198267
},
199268
]);

packages/core/src/backends/gitea/types.ts

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ export type GiteaOrganization = {
4646
website: string;
4747
};
4848

49-
type ReposListCommitsResponseItemCommitUser = {
49+
type CommitUser = {
5050
date: string;
5151
email: string;
5252
name: string;
5353
};
5454

55-
type ReposListCommitsResponseItemCommitMeta = {
55+
type CommitMeta = {
5656
created: string;
5757
sha: string;
5858
url: string;
@@ -73,10 +73,10 @@ type PayloadCommitVerification = {
7373
};
7474

7575
type ReposListCommitsResponseItemCommit = {
76-
author: ReposListCommitsResponseItemCommitUser;
77-
committer: ReposListCommitsResponseItemCommitUser;
76+
author: CommitUser;
77+
committer: CommitUser;
7878
message: string;
79-
tree: ReposListCommitsResponseItemCommitMeta;
79+
tree: CommitMeta;
8080
url: string;
8181
verification: PayloadCommitVerification;
8282
};
@@ -183,7 +183,7 @@ type ReposListCommitsResponseItem = {
183183
created: string;
184184
files: Array<ReposListCommitsResponseItemCommitAffectedFiles>;
185185
html_url: string;
186-
parents: Array<ReposListCommitsResponseItemCommitMeta>;
186+
parents: Array<CommitMeta>;
187187
sha: string;
188188
stats: ReposListCommitsResponseItemCommitStats;
189189
url: string;
@@ -215,4 +215,46 @@ export type GitGetTreeResponse = {
215215
tree: Array<GitGetTreeResponseTreeItem>;
216216
truncated: boolean;
217217
url: string;
218-
};
218+
};
219+
220+
type FileLinksResponse = {
221+
git: string;
222+
html: string;
223+
self: string;
224+
};
225+
226+
type ContentsResponse = {
227+
_links: FileLinksResponse;
228+
content?: string | null;
229+
download_url: string;
230+
encoding?: string | null;
231+
git_url: string;
232+
html_url: string;
233+
last_commit_sha: string;
234+
name: string;
235+
path: string;
236+
sha: string;
237+
size: number;
238+
submodule_git_url?: string | null;
239+
target?: string | null;
240+
type: string;
241+
url: string;
242+
};
243+
244+
type FileCommitResponse = {
245+
author: CommitUser;
246+
committer: CommitUser;
247+
created: string;
248+
html_url: string;
249+
message: string;
250+
parents: Array<CommitMeta>;
251+
sha: string;
252+
tree: CommitMeta;
253+
url: string;
254+
};
255+
256+
export type FilesResponse = {
257+
commit: FileCommitResponse;
258+
content: Array<ContentsResponse>;
259+
verification: PayloadCommitVerification;
260+
};

0 commit comments

Comments
 (0)