Skip to content

Commit 23ff3bd

Browse files
authored
Merge branch 'main' into fix/markdown-stability
2 parents ebe53e7 + 044ca1d commit 23ff3bd

File tree

16 files changed

+530
-156
lines changed

16 files changed

+530
-156
lines changed

package-lock.json

+133-56
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/decap-cms-backend-aws-cognito-github-proxy/src/implementation.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import AuthenticationPage from './AuthenticationPage';
55

66
import type { GitHubUser } from 'decap-cms-backend-github/src/implementation';
77
import type { Config } from 'decap-cms-lib-util/src';
8+
import type { Octokit } from '@octokit/rest';
89

910
export default class AwsCognitoGitHubProxyBackend extends GitHubBackend {
1011
constructor(config: Config, options = {}) {
@@ -44,4 +45,8 @@ export default class AwsCognitoGitHubProxyBackend extends GitHubBackend {
4445
}
4546
return this._currentUserPromise;
4647
}
48+
49+
async getPullRequestAuthor(pullRequest: Octokit.PullsListResponseItem) {
50+
return pullRequest.user?.login;
51+
}
4752
}

packages/decap-cms-backend-git-gateway/src/implementation.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,11 @@ export default class GitGateway implements Implementation {
362362
if (!(await this.api!.hasWriteAccess())) {
363363
throw new Error("You don't have sufficient permissions to access Decap CMS");
364364
}
365-
return { name: userData.name, login: userData.email } as User;
365+
return {
366+
name: userData.name,
367+
login: userData.email,
368+
avatar_url: userData.avatar_url,
369+
} as unknown as User;
366370
});
367371
}
368372
async restoreUser() {

packages/decap-cms-backend-github/src/API.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ export default class API {
580580
}
581581

582582
try {
583-
const user = await this.user();
583+
const user: GitHubUser = await this.request(`/users/${pullRequest.user.login}`);
584584
return user.name || user.login;
585585
} catch {
586586
return;

packages/decap-cms-backend-gitlab/src/implementation.ts

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export default class GitLab implements Implementation {
158158
backend: 'gitlab',
159159
repo: this.repo,
160160
token: this.token,
161+
apiRoot: this.apiRoot,
161162
});
162163
if (defaultBranchName) {
163164
this.branch = defaultBranchName;

packages/decap-cms-core/src/__tests__/backend.spec.js

+75
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,81 @@ describe('Backend', () => {
318318
});
319319
});
320320

321+
describe('persistEntry', () => {
322+
it('should update the draft with the new entry returned by preSave event', async () => {
323+
const implementation = {
324+
init: jest.fn(() => implementation),
325+
persistEntry: jest.fn(() => implementation),
326+
};
327+
328+
const config = {
329+
backend: {
330+
commit_messages: 'commit-messages',
331+
},
332+
};
333+
const collection = Map({
334+
name: 'posts',
335+
});
336+
const entry = Map({
337+
data: 'old_data',
338+
});
339+
const newEntry = Map({
340+
data: 'new_data',
341+
});
342+
const entryDraft = Map({
343+
entry,
344+
});
345+
const user = { login: 'login', name: 'name' };
346+
const backend = new Backend(implementation, { config, backendName: 'github' });
347+
348+
backend.currentUser = jest.fn().mockResolvedValue(user);
349+
backend.entryToRaw = jest.fn().mockReturnValue('content');
350+
backend.invokePreSaveEvent = jest.fn().mockReturnValueOnce(newEntry);
351+
352+
await backend.persistEntry({ config, collection, entryDraft });
353+
354+
expect(backend.entryToRaw).toHaveBeenCalledTimes(1);
355+
expect(backend.entryToRaw).toHaveBeenCalledWith(collection, newEntry);
356+
});
357+
358+
it('should update the draft with the new data returned by preSave event', async () => {
359+
const implementation = {
360+
init: jest.fn(() => implementation),
361+
persistEntry: jest.fn(() => implementation),
362+
};
363+
364+
const config = {
365+
backend: {
366+
commit_messages: 'commit-messages',
367+
},
368+
};
369+
const collection = Map({
370+
name: 'posts',
371+
});
372+
const entry = Map({
373+
data: Map({}),
374+
});
375+
const newData = Map({});
376+
const newEntry = Map({
377+
data: newData,
378+
});
379+
const entryDraft = Map({
380+
entry,
381+
});
382+
const user = { login: 'login', name: 'name' };
383+
const backend = new Backend(implementation, { config, backendName: 'github' });
384+
385+
backend.currentUser = jest.fn().mockResolvedValue(user);
386+
backend.entryToRaw = jest.fn().mockReturnValue('content');
387+
backend.invokePreSaveEvent = jest.fn().mockReturnValueOnce(newData);
388+
389+
await backend.persistEntry({ config, collection, entryDraft });
390+
391+
expect(backend.entryToRaw).toHaveBeenCalledTimes(1);
392+
expect(backend.entryToRaw).toHaveBeenCalledWith(collection, newEntry);
393+
});
394+
});
395+
321396
describe('persistMedia', () => {
322397
it('should persist media', async () => {
323398
const persistMediaResult = {};

packages/decap-cms-core/src/backend.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,14 @@ export class Backend {
10911091
unpublished = false,
10921092
status,
10931093
}: PersistArgs) {
1094-
const modifiedData = await this.invokePreSaveEvent(draft.get('entry'));
1095-
const entryDraft = (modifiedData && draft.setIn(['entry', 'data'], modifiedData)) || draft;
1094+
const updatedEntity = await this.invokePreSaveEvent(draft.get('entry'));
1095+
1096+
let entryDraft;
1097+
if (updatedEntity.get('data') === undefined) {
1098+
entryDraft = (updatedEntity && draft.setIn(['entry', 'data'], updatedEntity)) || draft;
1099+
} else {
1100+
entryDraft = (updatedEntity && draft.setIn(['entry'], updatedEntity)) || draft;
1101+
}
10961102

10971103
const newEntry = entryDraft.getIn(['entry', 'newRecord']) || false;
10981104

packages/decap-cms-core/src/components/Editor/EditorControlPane/EditorControl.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,9 @@ const ControlErrorsList = styled.ul`
8181
list-style-type: none;
8282
font-size: 12px;
8383
color: ${colors.errorText};
84-
margin-bottom: 5px;
84+
margin-bottom: 8px;
8585
text-align: right;
86-
text-transform: uppercase;
87-
position: relative;
8886
font-weight: 600;
89-
top: 20px;
9087
`;
9188

9289
export const ControlHint = styled.p`

packages/decap-cms-lib-util/src/API.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ type Backend = 'github' | 'gitlab' | 'bitbucket';
142142
type RequestConfig = Omit<RequestInit, 'headers'> &
143143
HeaderConfig & {
144144
backend: Backend;
145+
apiRoot?: string;
145146
params?: ParamObject;
146147
};
147148

@@ -182,7 +183,7 @@ async function constructRequestHeaders(headerConfig: HeaderConfig) {
182183
const { token, headers } = headerConfig;
183184
const baseHeaders: HeaderObj = { 'Content-Type': 'application/json; charset=utf-8', ...headers };
184185
if (token) {
185-
baseHeaders['Authorization'] = `token ${token}`;
186+
baseHeaders['Authorization'] = `Bearer ${token}`;
186187
}
187188
return Promise.resolve(baseHeaders);
188189
}
@@ -199,7 +200,7 @@ export async function apiRequest(
199200
const { token, backend, ...props } = config;
200201
const options = { cache: 'no-cache', ...props };
201202
const headers = await constructRequestHeaders({ headers: options.headers || {}, token });
202-
const baseUrl = apiRoots[backend];
203+
const baseUrl = config.apiRoot ?? apiRoots[backend];
203204
const url = constructUrlWithParams(`${baseUrl}${path}`, options.params);
204205
let responseStatus = 500;
205206
try {
@@ -220,9 +221,10 @@ export async function getDefaultBranchName(configs: {
220221
backend: Backend;
221222
repo: string;
222223
token?: string;
224+
apiRoot?: string;
223225
}) {
224226
let apiPath;
225-
const { token, backend, repo } = configs;
227+
const { token, backend, repo, apiRoot } = configs;
226228
switch (backend) {
227229
case 'gitlab': {
228230
apiPath = `/projects/${encodeURIComponent(repo)}`;
@@ -236,7 +238,7 @@ export async function getDefaultBranchName(configs: {
236238
apiPath = `/repos/${repo}`;
237239
}
238240
}
239-
const repoInfo = await apiRequest(apiPath, { token, backend });
241+
const repoInfo = await apiRequest(apiPath, { token, backend, apiRoot });
240242
let defaultBranchName;
241243
if (backend === 'bitbucket') {
242244
const {

0 commit comments

Comments
 (0)