Skip to content

Commit

Permalink
Merge branch 'main' into fix/markdown-stability
Browse files Browse the repository at this point in the history
  • Loading branch information
demshy authored Aug 7, 2024
2 parents ebe53e7 + 044ca1d commit 23ff3bd
Show file tree
Hide file tree
Showing 16 changed files with 530 additions and 156 deletions.
189 changes: 133 additions & 56 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import AuthenticationPage from './AuthenticationPage';

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

export default class AwsCognitoGitHubProxyBackend extends GitHubBackend {
constructor(config: Config, options = {}) {
Expand Down Expand Up @@ -44,4 +45,8 @@ export default class AwsCognitoGitHubProxyBackend extends GitHubBackend {
}
return this._currentUserPromise;
}

async getPullRequestAuthor(pullRequest: Octokit.PullsListResponseItem) {
return pullRequest.user?.login;
}
}
6 changes: 5 additions & 1 deletion packages/decap-cms-backend-git-gateway/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,11 @@ export default class GitGateway implements Implementation {
if (!(await this.api!.hasWriteAccess())) {
throw new Error("You don't have sufficient permissions to access Decap CMS");
}
return { name: userData.name, login: userData.email } as User;
return {
name: userData.name,
login: userData.email,
avatar_url: userData.avatar_url,
} as unknown as User;
});
}
async restoreUser() {
Expand Down
2 changes: 1 addition & 1 deletion packages/decap-cms-backend-github/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ export default class API {
}

try {
const user = await this.user();
const user: GitHubUser = await this.request(`/users/${pullRequest.user.login}`);
return user.name || user.login;
} catch {
return;
Expand Down
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 @@ -81,12 +81,9 @@ const ControlErrorsList = styled.ul`
list-style-type: none;
font-size: 12px;
color: ${colors.errorText};
margin-bottom: 5px;
margin-bottom: 8px;
text-align: right;
text-transform: uppercase;
position: relative;
font-weight: 600;
top: 20px;
`;

export const ControlHint = styled.p`
Expand Down
10 changes: 6 additions & 4 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 @@ -182,7 +183,7 @@ async function constructRequestHeaders(headerConfig: HeaderConfig) {
const { token, headers } = headerConfig;
const baseHeaders: HeaderObj = { 'Content-Type': 'application/json; charset=utf-8', ...headers };
if (token) {
baseHeaders['Authorization'] = `token ${token}`;
baseHeaders['Authorization'] = `Bearer ${token}`;
}
return Promise.resolve(baseHeaders);
}
Expand All @@ -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
Loading

0 comments on commit 23ff3bd

Please sign in to comment.