Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend-github): prepend collection name #2878

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: move migration to listUnpublishedBranches
  • Loading branch information
barthc committed Nov 18, 2019
commit 93b9b7541f3c49b6ece637418124e9e81fa71b38
92 changes: 54 additions & 38 deletions packages/netlify-cms-backend-github/src/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,27 +401,21 @@ export default class API {
});
}

getPRsForBranchName = ({
branchName,
state,
base = this.branch,
repoURL = this.repoURL,
usernameOfFork,
} = {}) => {
getPRsForBranchName = branchName => {
// Get PRs with a `head` of `branchName`. Note that this is a
// substring match, so we need to check that the `head.ref` of
// at least one of the returned objects matches `branchName`.
return this.requestAllPages(`${repoURL}/pulls`, {
return this.requestAllPages(`${this.repoURL}/pulls`, {
params: {
head: usernameOfFork ? `${usernameOfFork}:${branchName}` : branchName,
...(state ? { state } : {}),
base,
head: branchName,
state: 'open',
base: this.branch,
},
});
};

branchHasPR = async ({ branchName, ...rest }) => {
const prs = await this.getPRsForBranchName({ branchName, ...rest });
branchHasPR = async branchName => {
const prs = await this.getPRsForBranchName(branchName);
return prs.some(pr => pr.head.ref === branchName);
};

Expand Down Expand Up @@ -466,14 +460,55 @@ export default class API {
return metadata;
};

async migrateToVersion1(oldContentKey, metaData) {
const newContentKey = this.generateContentKey(metaData.collection, oldContentKey);
const newBranchName = this.generateBranchName(newContentKey);

// create new branch and pull request in new format
const newBranch = await this.createBranch(newBranchName, metaData.pr.head);
const pr = await this.createPR(metaData.commitMessage, newBranchName);

// store new metadata
await this.storeMetadata(newContentKey, {
...metaData,
pr: {
number: pr.number,
head: pr.head.sha,
},
branch: newBranchName,
version: '1',
});

// remove old data
await this.closePR(metaData.pr);
await this.deleteBranch(metaData.branch);
await this.deleteMetadata(oldContentKey);

return newBranch;
}

async migrateBranch(branch) {
const contentKey = this.contentKeyFromRef(branch.ref);
const metadata = await this.retrieveMetadata(contentKey);
if (!metadata.version) {
// migrate branch from cms/slug to cms/collection/slug
branch = await this.migrateToVersion1(contentKey, metadata);
}

return branch;
}

async listUnpublishedBranches() {
console.log(
'%c Checking for Unpublished entries',
'line-height: 30px;text-align: center;font-weight: bold',
);
const onlyBranchesWithOpenPRs = filterPromisesWith(({ ref }) =>
this.branchHasPR({ branchName: this.branchNameFromRef(ref), state: 'open' }),
);
const onlyBranchesWithOpenPRs = flow([
map(async branch => await this.migrateBranch(branch)),
filterPromisesWith(({ ref }) => this.branchHasPR(this.branchNameFromRef(ref))),
onlySuccessfulPromises,
]);

const getUpdatedOpenAuthoringBranches = flow([
map(async branch => {
const contentKey = this.contentKeyFromRef(branch.ref);
Expand Down Expand Up @@ -628,6 +663,7 @@ export default class API {
files: mediaFilesList,
},
timeStamp: new Date().toISOString(),
version: '1',
});
} else {
// Entry is already on editorial review workflow - just update metadata and commit to existing branch
Expand Down Expand Up @@ -869,6 +905,7 @@ export default class API {
this.retrieveMetadata(contentKey)
.then(metadata => (metadata && metadata.pr ? this.closePR(metadata.pr) : Promise.resolve()))
.then(() => this.deleteBranch(branchName))
.then(() => this.deleteMetadata(contentKey))
// If the PR doesn't exist, then this has already been deleted -
// deletion should be idempotent, so we can consider this a
// success.
Expand All @@ -888,32 +925,11 @@ export default class API {
const metadata = await this.retrieveMetadata(contentKey);
await this.mergePR(metadata.pr, metadata.objects);
await this.deleteBranch(branchName);
await this.deleteMetadata(contentKey);

return metadata;
}

async migrateUnpublishedEntry(slug) {
const data = await this.retrieveMetadata(slug).catch(() => false);
if (!data) {
return null;
}
const { title } = await this.getPullRequest(data.pr.number);
const newContentKey = this.generateContentKey(data.collection, slug);
const newBranchName = this.generateBranchName(newContentKey);

const pr = await this.createBranchAndPullRequest(newBranchName, data.pr.head, title);
const newMetadata = {
...data,
pr: { number: pr.number, head: pr.head.sha },
branch: newBranchName,
};
await this.storeMetadata(newContentKey, newMetadata);
await this.deleteBranch(data.branch);
await this.deleteMetadata(slug);

return newContentKey;
}

createRef(type, name, sha) {
return this.request(`${this.repoURL}/git/refs`, {
method: 'POST',
Expand Down
3 changes: 2 additions & 1 deletion packages/netlify-cms-backend-github/src/GraphQLAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ export default class GraphQLAPI extends API {
branches.push({ ref: `${headRef.prefix}${headRef.name}` });
});
});
return branches;

return await Promise.all(branches.map(branch => this.migrateBranch(branch)));
} else {
console.log(
'%c No Unpublished entries',
Expand Down
9 changes: 2 additions & 7 deletions packages/netlify-cms-backend-github/src/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,8 @@ export default class GitHub {
branches.map(({ ref }) => {
promises.push(
new Promise(async resolve => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the async required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I forgot to remove that.

let contentKey = ref.split('refs/heads/cms/').pop();
const contentKeyParts = contentKey.split('/');
const slug = contentKeyParts.pop();
const collection = contentKeyParts.pop();
// migrate entry to new meta key and branch
!collection && (contentKey = await this.api.migrateUnpublishedEntry(slug));

const contentKey = this.api.contentKeyFromRef(ref);
const slug = contentKey.split('/').pop();
return sem.take(() =>
this.api
.readUnpublishedBranchFile(contentKey)
Expand Down