Commit c6a47799 authored by Jacques Erasmus's avatar Jacques Erasmus

Merge branch '216861-custom-mr-title-description' into 'master'

Initial SSE merge request title/description meta

See merge request gitlab-org/gitlab!43909
parents 8a147445 a965b809
......@@ -3,11 +3,17 @@ import savedContentMetaQuery from '../queries/saved_content_meta.query.graphql';
const submitContentChangesResolver = (
_,
{ input: { project: projectId, username, sourcePath, content, images } },
{ input: { project: projectId, username, sourcePath, content, images, mergeRequestMeta } },
{ cache },
) => {
return submitContentChanges({ projectId, username, sourcePath, content, images }).then(
savedContentMeta => {
return submitContentChanges({
projectId,
username,
sourcePath,
content,
images,
mergeRequestMeta,
}).then(savedContentMeta => {
cache.writeQuery({
query: savedContentMetaQuery,
data: {
......@@ -17,8 +23,7 @@ const submitContentChangesResolver = (
},
},
});
},
);
});
};
export default submitContentChangesResolver;
<script>
import { deprecatedCreateFlash as createFlash } from '~/flash';
import { s__, sprintf } from '~/locale';
import Tracking from '~/tracking';
import SkeletonLoader from '../components/skeleton_loader.vue';
import EditArea from '../components/edit_area.vue';
import InvalidContentMessage from '../components/invalid_content_message.vue';
......@@ -7,8 +11,6 @@ import appDataQuery from '../graphql/queries/app_data.query.graphql';
import sourceContentQuery from '../graphql/queries/source_content.query.graphql';
import hasSubmittedChangesMutation from '../graphql/mutations/has_submitted_changes.mutation.graphql';
import submitContentChangesMutation from '../graphql/mutations/submit_content_changes.mutation.graphql';
import { deprecatedCreateFlash as createFlash } from '~/flash';
import Tracking from '~/tracking';
import { LOAD_CONTENT_ERROR, TRACKING_ACTION_INITIALIZE_EDITOR } from '../constants';
import { SUCCESS_ROUTE } from '../router/constants';
......@@ -99,6 +101,12 @@ export default {
sourcePath: this.appData.sourcePath,
content: this.content,
images,
mergeRequestMeta: {
title: sprintf(s__(`StaticSiteEditor|Update %{sourcePath} file`), {
sourcePath: this.appData.sourcePath,
}),
description: s__('StaticSiteEditor|Copy update'),
},
},
},
})
......
import Api from '~/api';
import Tracking from '~/tracking';
import { s__, sprintf } from '~/locale';
import { convertObjectPropsToSnakeCase } from '~/lib/utils/common_utils';
import generateBranchName from '~/static_site_editor/services/generate_branch_name';
......@@ -71,6 +70,7 @@ const commitContent = (projectId, message, branch, sourcePath, content, images)
const createMergeRequest = (
projectId,
title,
description,
sourceBranch,
targetBranch = DEFAULT_TARGET_BRANCH,
) => {
......@@ -80,6 +80,7 @@ const createMergeRequest = (
projectId,
convertObjectPropsToSnakeCase({
title,
description,
sourceBranch,
targetBranch,
}),
......@@ -88,11 +89,16 @@ const createMergeRequest = (
});
};
const submitContentChanges = ({ username, projectId, sourcePath, content, images }) => {
const branch = generateBranchName(username);
const mergeRequestTitle = sprintf(s__(`StaticSiteEditor|Update %{sourcePath} file`), {
const submitContentChanges = ({
username,
projectId,
sourcePath,
});
content,
images,
mergeRequestMeta,
}) => {
const branch = generateBranchName(username);
const { title: mergeRequestTitle, description: mergeRequestDescription } = mergeRequestMeta;
const meta = {};
return createBranch(projectId, branch)
......@@ -104,7 +110,7 @@ const submitContentChanges = ({ username, projectId, sourcePath, content, images
.then(({ data: { short_id: label, web_url: url } }) => {
Object.assign(meta, { commit: { label, url } });
return createMergeRequest(projectId, mergeRequestTitle, branch);
return createMergeRequest(projectId, mergeRequestTitle, mergeRequestDescription, branch);
})
.then(({ data: { iid: label, web_url: url } }) => {
Object.assign(meta, { mergeRequest: { label: label.toString(), url } });
......
......@@ -24540,6 +24540,9 @@ msgstr ""
msgid "StaticSiteEditor|Branch could not be created."
msgstr ""
msgid "StaticSiteEditor|Copy update"
msgstr ""
msgid "StaticSiteEditor|Could not commit the content changes."
msgstr ""
......
......@@ -23,7 +23,10 @@ export const username = 'gitlabuser';
export const projectId = '123456';
export const returnUrl = 'https://www.gitlab.com';
export const sourcePath = 'foobar.md.html';
export const mergeRequestMeta = {
title: `Update ${sourcePath} file`,
description: 'Copy update',
};
export const savedContentMeta = {
branch: {
label: 'foobar',
......
......@@ -17,6 +17,7 @@ import {
sourceContentTitle as title,
sourcePath,
username,
mergeRequestMeta,
savedContentMeta,
submitChangesError,
trackingCategory,
......@@ -241,6 +242,8 @@ describe('static_site_editor/pages/home', () => {
project,
sourcePath,
username,
images: undefined,
mergeRequestMeta,
},
},
});
......
......@@ -19,6 +19,7 @@ import {
commitBranchResponse,
commitMultipleResponse,
createMergeRequestResponse,
mergeRequestMeta,
sourcePath,
sourceContentYAML as content,
trackingCategory,
......@@ -28,11 +29,20 @@ import {
jest.mock('~/static_site_editor/services/generate_branch_name');
describe('submitContentChanges', () => {
const mergeRequestTitle = `Update ${sourcePath} file`;
const branch = 'branch-name';
let trackingSpy;
let origPage;
const buildPayload = (overrides = {}) => ({
username,
projectId,
sourcePath,
content,
images,
mergeRequestMeta,
...overrides,
});
beforeEach(() => {
jest.spyOn(Api, 'createBranch').mockResolvedValue({ data: commitBranchResponse });
jest.spyOn(Api, 'commitMultiple').mockResolvedValue({ data: commitMultipleResponse });
......@@ -53,7 +63,7 @@ describe('submitContentChanges', () => {
});
it('creates a branch named after the username and target branch', () => {
return submitContentChanges({ username, projectId }).then(() => {
return submitContentChanges(buildPayload()).then(() => {
expect(Api.createBranch).toHaveBeenCalledWith(projectId, {
ref: DEFAULT_TARGET_BRANCH,
branch,
......@@ -64,16 +74,16 @@ describe('submitContentChanges', () => {
it('notifies error when branch could not be created', () => {
Api.createBranch.mockRejectedValueOnce();
return expect(submitContentChanges({ username, projectId })).rejects.toThrow(
return expect(submitContentChanges(buildPayload())).rejects.toThrow(
SUBMIT_CHANGES_BRANCH_ERROR,
);
});
it('commits the content changes to the branch when creating branch succeeds', () => {
return submitContentChanges({ username, projectId, sourcePath, content, images }).then(() => {
return submitContentChanges(buildPayload()).then(() => {
expect(Api.commitMultiple).toHaveBeenCalledWith(projectId, {
branch,
commit_message: mergeRequestTitle,
commit_message: mergeRequestMeta.title,
actions: [
{
action: 'update',
......@@ -93,16 +103,11 @@ describe('submitContentChanges', () => {
it('does not commit an image if it has been removed from the content', () => {
const contentWithoutImages = '## Content without images';
return submitContentChanges({
username,
projectId,
sourcePath,
content: contentWithoutImages,
images,
}).then(() => {
const payload = buildPayload({ content: contentWithoutImages });
return submitContentChanges(payload).then(() => {
expect(Api.commitMultiple).toHaveBeenCalledWith(projectId, {
branch,
commit_message: mergeRequestTitle,
commit_message: mergeRequestMeta.title,
actions: [
{
action: 'update',
......@@ -117,17 +122,19 @@ describe('submitContentChanges', () => {
it('notifies error when content could not be committed', () => {
Api.commitMultiple.mockRejectedValueOnce();
return expect(submitContentChanges({ username, projectId, images })).rejects.toThrow(
return expect(submitContentChanges(buildPayload())).rejects.toThrow(
SUBMIT_CHANGES_COMMIT_ERROR,
);
});
it('creates a merge request when commiting changes succeeds', () => {
return submitContentChanges({ username, projectId, sourcePath, content, images }).then(() => {
it('creates a merge request when committing changes succeeds', () => {
return submitContentChanges(buildPayload()).then(() => {
const { title, description } = mergeRequestMeta;
expect(Api.createProjectMergeRequest).toHaveBeenCalledWith(
projectId,
convertObjectPropsToSnakeCase({
title: mergeRequestTitle,
title,
description,
targetBranch: DEFAULT_TARGET_BRANCH,
sourceBranch: branch,
}),
......@@ -138,7 +145,7 @@ describe('submitContentChanges', () => {
it('notifies error when merge request could not be created', () => {
Api.createProjectMergeRequest.mockRejectedValueOnce();
return expect(submitContentChanges({ username, projectId, images })).rejects.toThrow(
return expect(submitContentChanges(buildPayload())).rejects.toThrow(
SUBMIT_CHANGES_MERGE_REQUEST_ERROR,
);
});
......@@ -147,11 +154,9 @@ describe('submitContentChanges', () => {
let result;
beforeEach(() => {
return submitContentChanges({ username, projectId, sourcePath, content, images }).then(
_result => {
return submitContentChanges(buildPayload()).then(_result => {
result = _result;
},
);
});
});
it('returns the branch name', () => {
......@@ -179,7 +184,7 @@ describe('submitContentChanges', () => {
describe('sends the correct tracking event', () => {
beforeEach(() => {
return submitContentChanges({ username, projectId, sourcePath, content, images });
return submitContentChanges(buildPayload());
});
it('for committing changes', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment