Revert "feat(core): Align GitHub metadata handling with other backends (#3292)"
This reverts commit 8193b5ace89d6f14a6c756235a50b186a763b6b1.
This commit is contained in:
@ -21,35 +21,28 @@ describe('github API', () => {
|
||||
describe('editorialWorkflowGit', () => {
|
||||
it('should create PR with correct base branch name when publishing with editorial workflow', () => {
|
||||
let prBaseBranch = null;
|
||||
let labels = null;
|
||||
const api = new API({
|
||||
branch: 'gh-pages',
|
||||
repo: 'owner/my-repo',
|
||||
initialWorkflowStatus: 'draft',
|
||||
});
|
||||
const api = new API({ branch: 'gh-pages', repo: 'my-repo' });
|
||||
const responses = {
|
||||
'/repos/owner/my-repo/branches/gh-pages': () => ({ commit: { sha: 'def' } }),
|
||||
'/repos/owner/my-repo/git/trees/def': () => ({ tree: [] }),
|
||||
'/repos/owner/my-repo/git/trees': () => ({}),
|
||||
'/repos/owner/my-repo/git/commits': () => ({}),
|
||||
'/repos/owner/my-repo/git/refs': () => ({}),
|
||||
'/repos/owner/my-repo/pulls': req => {
|
||||
prBaseBranch = JSON.parse(req.body).base;
|
||||
return { head: { sha: 'cbd' }, labels: [], number: 1 };
|
||||
},
|
||||
'/repos/owner/my-repo/issues/1/labels': req => {
|
||||
labels = JSON.parse(req.body).labels;
|
||||
return {};
|
||||
'/repos/my-repo/branches/gh-pages': () => ({ commit: { sha: 'def' } }),
|
||||
'/repos/my-repo/git/trees/def': () => ({ tree: [] }),
|
||||
'/repos/my-repo/git/trees': () => ({}),
|
||||
'/repos/my-repo/git/commits': () => ({}),
|
||||
'/repos/my-repo/git/refs': () => ({}),
|
||||
'/repos/my-repo/pulls': pullRequest => {
|
||||
prBaseBranch = JSON.parse(pullRequest.body).base;
|
||||
return { head: { sha: 'cbd' } };
|
||||
},
|
||||
'/user': () => ({}),
|
||||
'/repos/my-repo/git/blobs': () => ({}),
|
||||
'/repos/my-repo/git/refs/meta/_netlify_cms': () => ({ object: {} }),
|
||||
};
|
||||
mockAPI(api, responses);
|
||||
|
||||
return expect(
|
||||
api.editorialWorkflowGit([], { slug: 'entry', sha: 'abc' }, null, {}).then(() => ({
|
||||
prBaseBranch,
|
||||
labels,
|
||||
})),
|
||||
).resolves.toEqual({ prBaseBranch: 'gh-pages', labels: ['netlify-cms/draft'] });
|
||||
api
|
||||
.editorialWorkflowGit([], { slug: 'entry', sha: 'abc' }, null, {})
|
||||
.then(() => prBaseBranch),
|
||||
).resolves.toEqual('gh-pages');
|
||||
});
|
||||
});
|
||||
|
||||
@ -298,64 +291,39 @@ describe('github API', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('migratePullRequest', () => {
|
||||
it('should migrate to pull request labels when no version', async () => {
|
||||
describe('migrateBranch', () => {
|
||||
it('should migrate to version 1 when no version', async () => {
|
||||
const api = new API({ branch: 'master', repo: 'owner/repo' });
|
||||
|
||||
const pr = {
|
||||
head: { ref: 'cms/2019-11-11-post-title' },
|
||||
title: 'pr title',
|
||||
number: 1,
|
||||
labels: [],
|
||||
};
|
||||
const newBranch = { ref: 'refs/heads/cms/posts/2019-11-11-post-title' };
|
||||
api.migrateToVersion1 = jest.fn().mockResolvedValue(newBranch);
|
||||
const metadata = { type: 'PR' };
|
||||
api.retrieveMetadataOld = jest.fn().mockResolvedValue(metadata);
|
||||
const newBranch = 'cms/posts/2019-11-11-post-title';
|
||||
const migrateToVersion1Result = {
|
||||
metadata: { ...metadata, branch: newBranch, version: '1' },
|
||||
pullRequest: { ...pr, number: 2 },
|
||||
};
|
||||
api.migrateToVersion1 = jest.fn().mockResolvedValue(migrateToVersion1Result);
|
||||
api.migrateToPullRequestLabels = jest.fn();
|
||||
api.retrieveMetadata = jest.fn().mockResolvedValue(metadata);
|
||||
|
||||
await api.migratePullRequest(pr);
|
||||
const branch = { ref: 'refs/heads/cms/2019-11-11-post-title' };
|
||||
await expect(api.migrateBranch(branch)).resolves.toBe(newBranch);
|
||||
|
||||
expect(api.migrateToVersion1).toHaveBeenCalledTimes(1);
|
||||
expect(api.migrateToVersion1).toHaveBeenCalledWith(pr, metadata);
|
||||
expect(api.migrateToVersion1).toHaveBeenCalledWith(branch, metadata);
|
||||
|
||||
expect(api.migrateToPullRequestLabels).toHaveBeenCalledTimes(1);
|
||||
expect(api.migrateToPullRequestLabels).toHaveBeenCalledWith(
|
||||
migrateToVersion1Result.pullRequest,
|
||||
migrateToVersion1Result.metadata,
|
||||
);
|
||||
|
||||
expect(api.retrieveMetadataOld).toHaveBeenCalledTimes(1);
|
||||
expect(api.retrieveMetadataOld).toHaveBeenCalledWith('2019-11-11-post-title');
|
||||
expect(api.retrieveMetadata).toHaveBeenCalledTimes(1);
|
||||
expect(api.retrieveMetadata).toHaveBeenCalledWith('2019-11-11-post-title');
|
||||
});
|
||||
|
||||
it('should migrate to pull request labels when version is 1', async () => {
|
||||
it('should not migrate to version 1 when version is 1', async () => {
|
||||
const api = new API({ branch: 'master', repo: 'owner/repo' });
|
||||
|
||||
api.migrateToVersion1 = jest.fn();
|
||||
const pr = {
|
||||
head: { ref: 'cms/posts/2019-11-11-post-title' },
|
||||
title: 'pr title',
|
||||
number: 1,
|
||||
labels: [],
|
||||
};
|
||||
const metadata = { type: 'PR', version: '1' };
|
||||
api.retrieveMetadataOld = jest.fn().mockResolvedValue(metadata);
|
||||
api.migrateToPullRequestLabels = jest.fn().mockResolvedValue(pr, metadata);
|
||||
api.retrieveMetadata = jest.fn().mockResolvedValue(metadata);
|
||||
|
||||
await api.migratePullRequest(pr);
|
||||
const branch = { ref: 'refs/heads/cms/posts/2019-11-11-post-title' };
|
||||
await expect(api.migrateBranch(branch)).resolves.toBe(branch);
|
||||
|
||||
expect(api.migrateToVersion1).toHaveBeenCalledTimes(0);
|
||||
|
||||
expect(api.migrateToPullRequestLabels).toHaveBeenCalledTimes(1);
|
||||
expect(api.migrateToPullRequestLabels).toHaveBeenCalledWith(pr, metadata);
|
||||
|
||||
expect(api.retrieveMetadataOld).toHaveBeenCalledTimes(1);
|
||||
expect(api.retrieveMetadataOld).toHaveBeenCalledWith('posts/2019-11-11-post-title');
|
||||
expect(api.retrieveMetadata).toHaveBeenCalledTimes(1);
|
||||
expect(api.retrieveMetadata).toHaveBeenCalledWith('posts/2019-11-11-post-title');
|
||||
});
|
||||
});
|
||||
|
||||
@ -363,17 +331,10 @@ describe('github API', () => {
|
||||
it('should migrate to version 1', async () => {
|
||||
const api = new API({ branch: 'master', repo: 'owner/repo' });
|
||||
|
||||
const pr = {
|
||||
head: { ref: 'cms/2019-11-11-post-title', sha: 'pr_head' },
|
||||
title: 'pr title',
|
||||
number: 1,
|
||||
labels: [],
|
||||
};
|
||||
|
||||
const newBranch = { ref: 'refs/heads/cms/posts/2019-11-11-post-title' };
|
||||
api.createBranch = jest.fn().mockResolvedValue(newBranch);
|
||||
|
||||
const newPr = { ...pr, number: 2 };
|
||||
const newPr = { number: 2, head: { sha: 'new_head' } };
|
||||
api.createPR = jest.fn().mockResolvedValue(newPr);
|
||||
|
||||
api.storeMetadata = jest.fn();
|
||||
@ -381,42 +342,35 @@ describe('github API', () => {
|
||||
api.deleteBranch = jest.fn();
|
||||
api.deleteMetadata = jest.fn();
|
||||
|
||||
const branch = 'cms/2019-11-11-post-title';
|
||||
const branch = { ref: 'refs/heads/cms/2019-11-11-post-title' };
|
||||
const metadata = {
|
||||
branch,
|
||||
branch: 'cms/2019-11-11-post-title',
|
||||
type: 'PR',
|
||||
pr: { head: pr.head.sha },
|
||||
pr: { head: 'old_head' },
|
||||
commitMessage: 'commitMessage',
|
||||
collection: 'posts',
|
||||
};
|
||||
|
||||
const expectedMetadata = {
|
||||
await expect(api.migrateToVersion1(branch, metadata)).resolves.toBe(newBranch);
|
||||
|
||||
expect(api.createBranch).toHaveBeenCalledTimes(1);
|
||||
expect(api.createBranch).toHaveBeenCalledWith('cms/posts/2019-11-11-post-title', 'old_head');
|
||||
|
||||
expect(api.createPR).toHaveBeenCalledTimes(1);
|
||||
expect(api.createPR).toHaveBeenCalledWith('commitMessage', 'cms/posts/2019-11-11-post-title');
|
||||
|
||||
expect(api.storeMetadata).toHaveBeenCalledTimes(1);
|
||||
expect(api.storeMetadata).toHaveBeenCalledWith('posts/2019-11-11-post-title', {
|
||||
type: 'PR',
|
||||
pr: { head: newPr.head.sha, number: 2 },
|
||||
pr: { head: 'new_head', number: 2 },
|
||||
commitMessage: 'commitMessage',
|
||||
collection: 'posts',
|
||||
branch: 'cms/posts/2019-11-11-post-title',
|
||||
version: '1',
|
||||
};
|
||||
await expect(api.migrateToVersion1(pr, metadata)).resolves.toEqual({
|
||||
metadata: expectedMetadata,
|
||||
pullRequest: newPr,
|
||||
});
|
||||
|
||||
expect(api.createBranch).toHaveBeenCalledTimes(1);
|
||||
expect(api.createBranch).toHaveBeenCalledWith('cms/posts/2019-11-11-post-title', 'pr_head');
|
||||
|
||||
expect(api.createPR).toHaveBeenCalledTimes(1);
|
||||
expect(api.createPR).toHaveBeenCalledWith('pr title', 'cms/posts/2019-11-11-post-title');
|
||||
|
||||
expect(api.storeMetadata).toHaveBeenCalledTimes(1);
|
||||
expect(api.storeMetadata).toHaveBeenCalledWith(
|
||||
'posts/2019-11-11-post-title',
|
||||
expectedMetadata,
|
||||
);
|
||||
|
||||
expect(api.closePR).toHaveBeenCalledTimes(1);
|
||||
expect(api.closePR).toHaveBeenCalledWith(metadata.pr.number);
|
||||
expect(api.closePR).toHaveBeenCalledWith(metadata.pr);
|
||||
|
||||
expect(api.deleteBranch).toHaveBeenCalledTimes(1);
|
||||
expect(api.deleteBranch).toHaveBeenCalledWith('cms/2019-11-11-post-title');
|
||||
@ -426,55 +380,20 @@ describe('github API', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('migrateToPullRequestLabels', () => {
|
||||
it('should migrate to pull request labels', async () => {
|
||||
const api = new API({ branch: 'master', repo: 'owner/repo' });
|
||||
|
||||
const pr = {
|
||||
head: { ref: 'cms/posts/2019-11-11-post-title', sha: 'pr_head' },
|
||||
title: 'pr title',
|
||||
number: 1,
|
||||
labels: [],
|
||||
};
|
||||
|
||||
api.setPullRequestStatus = jest.fn();
|
||||
api.deleteMetadata = jest.fn();
|
||||
|
||||
const metadata = {
|
||||
branch: pr.head.ref,
|
||||
type: 'PR',
|
||||
pr: { head: pr.head.sha },
|
||||
commitMessage: 'commitMessage',
|
||||
collection: 'posts',
|
||||
status: 'pending_review',
|
||||
};
|
||||
|
||||
await api.migrateToPullRequestLabels(pr, metadata);
|
||||
|
||||
expect(api.setPullRequestStatus).toHaveBeenCalledTimes(1);
|
||||
expect(api.setPullRequestStatus).toHaveBeenCalledWith(pr, 'pending_review');
|
||||
|
||||
expect(api.deleteMetadata).toHaveBeenCalledTimes(1);
|
||||
expect(api.deleteMetadata).toHaveBeenCalledWith('posts/2019-11-11-post-title');
|
||||
});
|
||||
});
|
||||
|
||||
describe('rebaseSingleCommit', () => {
|
||||
it('should create updated tree and commit', async () => {
|
||||
const api = new API({ branch: 'master', repo: 'owner/repo' });
|
||||
|
||||
api.getDifferences = jest.fn().mockResolvedValueOnce({
|
||||
files: [
|
||||
{ filename: 'removed.md', status: 'removed', sha: 'removed_sha' },
|
||||
{
|
||||
filename: 'renamed.md',
|
||||
status: 'renamed',
|
||||
previous_filename: 'previous_filename.md',
|
||||
sha: 'renamed_sha',
|
||||
},
|
||||
{ filename: 'added.md', status: 'added', sha: 'added_sha' },
|
||||
],
|
||||
});
|
||||
api.getCommitsDiff = jest.fn().mockResolvedValueOnce([
|
||||
{ filename: 'removed.md', status: 'removed', sha: 'removed_sha' },
|
||||
{
|
||||
filename: 'renamed.md',
|
||||
status: 'renamed',
|
||||
previous_filename: 'previous_filename.md',
|
||||
sha: 'renamed_sha',
|
||||
},
|
||||
{ filename: 'added.md', status: 'added', sha: 'added_sha' },
|
||||
]);
|
||||
|
||||
const newTree = { sha: 'new_tree_sha' };
|
||||
api.updateTree = jest.fn().mockResolvedValueOnce(newTree);
|
||||
@ -495,8 +414,8 @@ describe('github API', () => {
|
||||
|
||||
await expect(api.rebaseSingleCommit(baseCommit, commit)).resolves.toBe(newCommit);
|
||||
|
||||
expect(api.getDifferences).toHaveBeenCalledTimes(1);
|
||||
expect(api.getDifferences).toHaveBeenCalledWith('parent_sha', 'sha', '/repos/owner/repo');
|
||||
expect(api.getCommitsDiff).toHaveBeenCalledTimes(1);
|
||||
expect(api.getCommitsDiff).toHaveBeenCalledWith('parent_sha', 'sha');
|
||||
|
||||
expect(api.updateTree).toHaveBeenCalledTimes(1);
|
||||
expect(api.updateTree).toHaveBeenCalledWith('base_commit_sha', [
|
||||
@ -609,18 +528,13 @@ describe('github API', () => {
|
||||
];
|
||||
|
||||
api.request = jest.fn(() => Promise.resolve({ statuses }));
|
||||
const sha = 'sha';
|
||||
api.getBranchPullRequest = jest.fn(() => Promise.resolve({ head: { sha } }));
|
||||
|
||||
const collection = 'collection';
|
||||
const slug = 'slug';
|
||||
await expect(api.getStatuses(collection, slug)).resolves.toEqual([
|
||||
const sha = 'sha';
|
||||
await expect(api.getStatuses(sha)).resolves.toEqual([
|
||||
{ context: 'deploy', state: 'success', target_url: 'deploy-url' },
|
||||
{ context: 'build', state: 'other' },
|
||||
]);
|
||||
|
||||
expect(api.getBranchPullRequest).toHaveBeenCalledTimes(1);
|
||||
expect(api.getBranchPullRequest).toHaveBeenCalledWith('cms/collection/slug');
|
||||
expect(api.request).toHaveBeenCalledTimes(1);
|
||||
expect(api.request).toHaveBeenCalledWith(`/repos/repo/commits/${sha}/status`);
|
||||
});
|
||||
|
@ -9,7 +9,7 @@ describe('github GraphQL API', () => {
|
||||
|
||||
describe('editorialWorkflowGit', () => {
|
||||
it('should should flatten nested tree into a list of files', () => {
|
||||
const api = new GraphQLAPI({ branch: 'gh-pages', repo: 'owner/my-repo' });
|
||||
const api = new GraphQLAPI({ branch: 'gh-pages', repo: 'my-repo' });
|
||||
const entries = [
|
||||
{
|
||||
name: 'post-1.md',
|
||||
|
@ -185,9 +185,7 @@ describe('github backend implementation', () => {
|
||||
isModification: true,
|
||||
metaData: {
|
||||
branch: 'branch',
|
||||
objects: {
|
||||
entry: { path: 'entry-path', mediaFiles: [{ path: 'image.png', id: 'sha' }] },
|
||||
},
|
||||
objects: { entry: { path: 'entry-path' }, files: [{ path: 'image.png', sha: 'sha' }] },
|
||||
},
|
||||
};
|
||||
readUnpublishedBranchFile.mockResolvedValue(data);
|
||||
|
Reference in New Issue
Block a user