Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
olegfedak authored Aug 4, 2024
2 parents db47712 + 853487b commit 52c1244
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/decap-cms-backend-gitlab/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export default class GitLab implements Implementation {
backend: 'gitlab',
repo: this.repo,
token: this.token,
apiRoot: this.apiRoot,
});
if (defaultBranchName) {
this.branch = defaultBranchName;
Expand Down
75 changes: 75 additions & 0 deletions packages/decap-cms-core/src/__tests__/backend.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,81 @@ describe('Backend', () => {
});
});

describe('persistEntry', () => {
it('should update the draft with the new entry returned by preSave event', async () => {
const implementation = {
init: jest.fn(() => implementation),
persistEntry: jest.fn(() => implementation),
};

const config = {
backend: {
commit_messages: 'commit-messages',
},
};
const collection = Map({
name: 'posts',
});
const entry = Map({
data: 'old_data',
});
const newEntry = Map({
data: 'new_data',
});
const entryDraft = Map({
entry,
});
const user = { login: 'login', name: 'name' };
const backend = new Backend(implementation, { config, backendName: 'github' });

backend.currentUser = jest.fn().mockResolvedValue(user);
backend.entryToRaw = jest.fn().mockReturnValue('content');
backend.invokePreSaveEvent = jest.fn().mockReturnValueOnce(newEntry);

await backend.persistEntry({ config, collection, entryDraft });

expect(backend.entryToRaw).toHaveBeenCalledTimes(1);
expect(backend.entryToRaw).toHaveBeenCalledWith(collection, newEntry);
});

it('should update the draft with the new data returned by preSave event', async () => {
const implementation = {
init: jest.fn(() => implementation),
persistEntry: jest.fn(() => implementation),
};

const config = {
backend: {
commit_messages: 'commit-messages',
},
};
const collection = Map({
name: 'posts',
});
const entry = Map({
data: Map({}),
});
const newData = Map({});
const newEntry = Map({
data: newData,
});
const entryDraft = Map({
entry,
});
const user = { login: 'login', name: 'name' };
const backend = new Backend(implementation, { config, backendName: 'github' });

backend.currentUser = jest.fn().mockResolvedValue(user);
backend.entryToRaw = jest.fn().mockReturnValue('content');
backend.invokePreSaveEvent = jest.fn().mockReturnValueOnce(newData);

await backend.persistEntry({ config, collection, entryDraft });

expect(backend.entryToRaw).toHaveBeenCalledTimes(1);
expect(backend.entryToRaw).toHaveBeenCalledWith(collection, newEntry);
});
});

describe('persistMedia', () => {
it('should persist media', async () => {
const persistMediaResult = {};
Expand Down
10 changes: 8 additions & 2 deletions packages/decap-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,14 @@ export class Backend {
unpublished = false,
status,
}: PersistArgs) {
const modifiedData = await this.invokePreSaveEvent(draft.get('entry'));
const entryDraft = (modifiedData && draft.setIn(['entry', 'data'], modifiedData)) || draft;
const updatedEntity = await this.invokePreSaveEvent(draft.get('entry'));

let entryDraft;
if (updatedEntity.get('data') === undefined) {
entryDraft = (updatedEntity && draft.setIn(['entry', 'data'], updatedEntity)) || draft;
} else {
entryDraft = (updatedEntity && draft.setIn(['entry'], updatedEntity)) || draft;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,11 @@ const ControlTopbar = styled.div`
`;
const ControlErrorsList = styled.ul`
list-style-type: none;
font-size: 12px;
color: ${colors.errorText};
text-align: right;
text-transform: uppercase;
font-weight: 600;
margin: 0;
padding: 2px 0 3px;
font-size: 12px;
color: ${colors.errorText};
margin-bottom: 8px;
text-align: right;
font-weight: 600;
`;

export const ControlHint = styled.p`
Expand Down
8 changes: 5 additions & 3 deletions packages/decap-cms-lib-util/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type Backend = 'github' | 'gitlab' | 'bitbucket';
type RequestConfig = Omit<RequestInit, 'headers'> &
HeaderConfig & {
backend: Backend;
apiRoot?: string;
params?: ParamObject;
};

Expand Down Expand Up @@ -199,7 +200,7 @@ export async function apiRequest(
const { token, backend, ...props } = config;
const options = { cache: 'no-cache', ...props };
const headers = await constructRequestHeaders({ headers: options.headers || {}, token });
const baseUrl = apiRoots[backend];
const baseUrl = config.apiRoot ?? apiRoots[backend];
const url = constructUrlWithParams(`${baseUrl}${path}`, options.params);
let responseStatus = 500;
try {
Expand All @@ -220,9 +221,10 @@ export async function getDefaultBranchName(configs: {
backend: Backend;
repo: string;
token?: string;
apiRoot?: string;
}) {
let apiPath;
const { token, backend, repo } = configs;
const { token, backend, repo, apiRoot } = configs;
switch (backend) {
case 'gitlab': {
apiPath = `/projects/${encodeURIComponent(repo)}`;
Expand All @@ -236,7 +238,7 @@ export async function getDefaultBranchName(configs: {
apiPath = `/repos/${repo}`;
}
}
const repoInfo = await apiRequest(apiPath, { token, backend });
const repoInfo = await apiRequest(apiPath, { token, backend, apiRoot });
let defaultBranchName;
if (backend === 'bitbucket') {
const {
Expand Down

0 comments on commit 52c1244

Please sign in to comment.