fix: workflow file collection (#3207)
This commit is contained in:
@ -22,7 +22,8 @@
|
||||
"dependencies": {
|
||||
"common-tags": "^1.8.0",
|
||||
"js-base64": "^2.5.1",
|
||||
"semaphore": "^1.1.0"
|
||||
"semaphore": "^1.1.0",
|
||||
"what-the-diff": "^0.6.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@emotion/core": "^10.0.9",
|
||||
|
@ -25,6 +25,7 @@ import {
|
||||
parseContentKey,
|
||||
} from 'netlify-cms-lib-util';
|
||||
import { oneLine } from 'common-tags';
|
||||
import { parse } from 'what-the-diff';
|
||||
|
||||
interface Config {
|
||||
apiRoot?: string;
|
||||
@ -125,21 +126,6 @@ type BitBucketPullRequestStatues = {
|
||||
values: BitBucketPullRequestStatus[];
|
||||
};
|
||||
|
||||
type BitBucketDiffStat = {
|
||||
pagelen: number;
|
||||
page: number;
|
||||
size: number;
|
||||
values: {
|
||||
status: string;
|
||||
lines_removed: number;
|
||||
lines_added: number;
|
||||
new: {
|
||||
path: string;
|
||||
type: 'commit_file';
|
||||
};
|
||||
}[];
|
||||
};
|
||||
|
||||
type DeleteEntry = {
|
||||
path: string;
|
||||
delete: true;
|
||||
@ -448,13 +434,18 @@ export default class API {
|
||||
}
|
||||
|
||||
async getDifferences(branch: string) {
|
||||
const diff: BitBucketDiffStat = await this.requestJSON({
|
||||
url: `${this.repoURL}/diffstat/${branch}..${this.branch}`,
|
||||
const rawDiff = await this.requestText({
|
||||
url: `${this.repoURL}/diff/${branch}..${this.branch}`,
|
||||
params: {
|
||||
pagelen: 100,
|
||||
binary: false,
|
||||
},
|
||||
});
|
||||
return diff.values;
|
||||
|
||||
return parse(rawDiff).map(d => ({
|
||||
newPath: d.newPath.replace(/b\//, ''),
|
||||
binary: d.binary || /.svg$/.test(d.newPath),
|
||||
newFile: d.status === 'added',
|
||||
}));
|
||||
}
|
||||
|
||||
async editorialWorkflowGit(files: (Entry | AssetProxy)[], entry: Entry, options: PersistOptions) {
|
||||
@ -478,8 +469,8 @@ export default class API {
|
||||
const diffs = await this.getDifferences(branch);
|
||||
const toDelete: DeleteEntry[] = [];
|
||||
for (const diff of diffs) {
|
||||
if (!files.some(file => file.path === diff.new.path)) {
|
||||
toDelete.push({ path: diff.new.path, delete: true });
|
||||
if (!files.some(file => file.path === diff.newPath)) {
|
||||
toDelete.push({ path: diff.newPath, delete: true });
|
||||
}
|
||||
}
|
||||
|
||||
@ -571,31 +562,37 @@ export default class API {
|
||||
const branch = this.branchFromContentKey(contentKey);
|
||||
const pullRequest = await this.getBranchPullRequest(branch);
|
||||
const diff = await this.getDifferences(branch);
|
||||
const path = diff.find(d => d.new.path.includes(slug))?.new.path as string;
|
||||
const { newPath: path, newFile } = diff.find(d => !d.binary) as {
|
||||
newPath: string;
|
||||
newFile: boolean;
|
||||
};
|
||||
// TODO: get real file id
|
||||
const mediaFiles = await Promise.all(
|
||||
diff.filter(d => d.new.path !== path).map(d => ({ path: d.new.path, id: null })),
|
||||
diff.filter(d => d.newPath !== path).map(d => ({ path: d.newPath, id: null })),
|
||||
);
|
||||
const label = await this.getPullRequestLabel(pullRequest.id);
|
||||
const status = labelToStatus(label);
|
||||
return { branch, collection, slug, path, status, mediaFiles };
|
||||
return { branch, collection, slug, path, status, newFile, mediaFiles };
|
||||
}
|
||||
|
||||
async readUnpublishedBranchFile(contentKey: string) {
|
||||
const { branch, collection, slug, path, status, mediaFiles } = await this.retrieveMetadata(
|
||||
contentKey,
|
||||
);
|
||||
const {
|
||||
branch,
|
||||
collection,
|
||||
slug,
|
||||
path,
|
||||
status,
|
||||
newFile,
|
||||
mediaFiles,
|
||||
} = await this.retrieveMetadata(contentKey);
|
||||
|
||||
const [fileData, isModification] = await Promise.all([
|
||||
this.readFile(path, null, { branch }) as Promise<string>,
|
||||
this.isFileExists(path, this.branch),
|
||||
]);
|
||||
const fileData = (await this.readFile(path, null, { branch })) as string;
|
||||
|
||||
return {
|
||||
slug,
|
||||
metaData: { branch, collection, objects: { entry: { path, mediaFiles } }, status },
|
||||
fileData,
|
||||
isModification,
|
||||
isModification: !newFile,
|
||||
};
|
||||
}
|
||||
|
||||
|
3
packages/netlify-cms-backend-bitbucket/src/types/what-the-diff.d.ts
vendored
Normal file
3
packages/netlify-cms-backend-bitbucket/src/types/what-the-diff.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
declare module 'what-the-diff' {
|
||||
export const parse: (rawDiff: string) => { newPath: string; binary: boolean; status: string }[];
|
||||
}
|
@ -73,6 +73,7 @@ type GitLabCommitDiff = {
|
||||
diff: string;
|
||||
new_path: string;
|
||||
old_path: string;
|
||||
new_file: boolean;
|
||||
};
|
||||
|
||||
enum GitLabCommitStatuses {
|
||||
@ -546,7 +547,10 @@ export default class API {
|
||||
},
|
||||
});
|
||||
|
||||
return result.diffs;
|
||||
return result.diffs.map(d => ({
|
||||
...d,
|
||||
binary: d.diff.startsWith('Binary') || /.svg$/.test(d.new_path),
|
||||
}));
|
||||
}
|
||||
|
||||
async retrieveMetadata(contentKey: string) {
|
||||
@ -554,7 +558,10 @@ export default class API {
|
||||
const branch = this.branchFromContentKey(contentKey);
|
||||
const mergeRequest = await this.getBranchMergeRequest(branch);
|
||||
const diff = await this.getDifferences(mergeRequest.sha);
|
||||
const path = diff.find(d => d.old_path.includes(slug))?.old_path as string;
|
||||
const { old_path: path, new_file: newFile } = diff.find(d => !d.binary) as {
|
||||
old_path: string;
|
||||
new_file: boolean;
|
||||
};
|
||||
const mediaFiles = await Promise.all(
|
||||
diff
|
||||
.filter(d => d.old_path !== path)
|
||||
@ -566,24 +573,27 @@ export default class API {
|
||||
);
|
||||
const label = mergeRequest.labels.find(isCMSLabel) as string;
|
||||
const status = labelToStatus(label);
|
||||
return { branch, collection, slug, path, status, mediaFiles };
|
||||
return { branch, collection, slug, path, status, newFile, mediaFiles };
|
||||
}
|
||||
|
||||
async readUnpublishedBranchFile(contentKey: string) {
|
||||
const { branch, collection, slug, path, status, mediaFiles } = await this.retrieveMetadata(
|
||||
contentKey,
|
||||
);
|
||||
const {
|
||||
branch,
|
||||
collection,
|
||||
slug,
|
||||
path,
|
||||
status,
|
||||
newFile,
|
||||
mediaFiles,
|
||||
} = await this.retrieveMetadata(contentKey);
|
||||
|
||||
const [fileData, isModification] = await Promise.all([
|
||||
this.readFile(path, null, { branch }) as Promise<string>,
|
||||
this.isFileExists(path, this.branch),
|
||||
]);
|
||||
const fileData = (await this.readFile(path, null, { branch })) as string;
|
||||
|
||||
return {
|
||||
slug,
|
||||
metaData: { branch, collection, objects: { entry: { path, mediaFiles } }, status },
|
||||
fileData,
|
||||
isModification,
|
||||
isModification: !newFile,
|
||||
};
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user