Commit e98f91e7 authored by Enrique Alcantara's avatar Enrique Alcantara

Use default initial branch in SSE

Replace hardcoded master branch with the
initial branch provided by the backend
in the Static Site Editor
parent 8a7568ab
import { s__, __ } from '~/locale'; import { s__, __ } from '~/locale';
export const BRANCH_SUFFIX_COUNT = 8; export const BRANCH_SUFFIX_COUNT = 8;
export const DEFAULT_TARGET_BRANCH = 'master';
export const ISSUABLE_TYPE = 'merge_request'; export const ISSUABLE_TYPE = 'merge_request';
export const SUBMIT_CHANGES_BRANCH_ERROR = s__('StaticSiteEditor|Branch could not be created.'); export const SUBMIT_CHANGES_BRANCH_ERROR = s__('StaticSiteEditor|Branch could not be created.');
......
...@@ -9,6 +9,7 @@ const submitContentChangesResolver = ( ...@@ -9,6 +9,7 @@ const submitContentChangesResolver = (
project: projectId, project: projectId,
username, username,
sourcePath, sourcePath,
targetBranch,
content, content,
images, images,
mergeRequestMeta, mergeRequestMeta,
...@@ -21,6 +22,7 @@ const submitContentChangesResolver = ( ...@@ -21,6 +22,7 @@ const submitContentChangesResolver = (
projectId, projectId,
username, username,
sourcePath, sourcePath,
targetBranch,
content, content,
images, images,
mergeRequestMeta, mergeRequestMeta,
......
...@@ -111,6 +111,7 @@ export default { ...@@ -111,6 +111,7 @@ export default {
project: this.appData.project, project: this.appData.project,
username: this.appData.username, username: this.appData.username,
sourcePath: this.appData.sourcePath, sourcePath: this.appData.sourcePath,
targetBranch: this.appData.branch,
content: this.content, content: this.content,
formattedMarkdown: this.formattedMarkdown, formattedMarkdown: this.formattedMarkdown,
images: this.images, images: this.images,
......
import { BRANCH_SUFFIX_COUNT, DEFAULT_TARGET_BRANCH } from '../constants'; import { BRANCH_SUFFIX_COUNT } from '../constants';
const generateBranchSuffix = () => `${Date.now()}`.substr(BRANCH_SUFFIX_COUNT); const generateBranchSuffix = () => `${Date.now()}`.substr(BRANCH_SUFFIX_COUNT);
const generateBranchName = (username, targetBranch = DEFAULT_TARGET_BRANCH) => const generateBranchName = (username, targetBranch) =>
`${username}-${targetBranch}-patch-${generateBranchSuffix()}`; `${username}-${targetBranch}-patch-${generateBranchSuffix()}`;
export default generateBranchName; export default generateBranchName;
...@@ -4,7 +4,6 @@ import generateBranchName from '~/static_site_editor/services/generate_branch_na ...@@ -4,7 +4,6 @@ import generateBranchName from '~/static_site_editor/services/generate_branch_na
import Tracking from '~/tracking'; import Tracking from '~/tracking';
import { import {
DEFAULT_TARGET_BRANCH,
SUBMIT_CHANGES_BRANCH_ERROR, SUBMIT_CHANGES_BRANCH_ERROR,
SUBMIT_CHANGES_COMMIT_ERROR, SUBMIT_CHANGES_COMMIT_ERROR,
SUBMIT_CHANGES_MERGE_REQUEST_ERROR, SUBMIT_CHANGES_MERGE_REQUEST_ERROR,
...@@ -16,9 +15,9 @@ import { ...@@ -16,9 +15,9 @@ import {
DEFAULT_FORMATTING_CHANGES_COMMIT_DESCRIPTION, DEFAULT_FORMATTING_CHANGES_COMMIT_DESCRIPTION,
} from '../constants'; } from '../constants';
const createBranch = (projectId, branch) => const createBranch = (projectId, branch, targetBranch) =>
Api.createBranch(projectId, { Api.createBranch(projectId, {
ref: DEFAULT_TARGET_BRANCH, ref: targetBranch,
branch, branch,
}).catch(() => { }).catch(() => {
throw new Error(SUBMIT_CHANGES_BRANCH_ERROR); throw new Error(SUBMIT_CHANGES_BRANCH_ERROR);
...@@ -73,13 +72,7 @@ const commit = (projectId, message, branch, actions) => { ...@@ -73,13 +72,7 @@ const commit = (projectId, message, branch, actions) => {
}); });
}; };
const createMergeRequest = ( const createMergeRequest = (projectId, title, description, sourceBranch, targetBranch) => {
projectId,
title,
description,
sourceBranch,
targetBranch = DEFAULT_TARGET_BRANCH,
) => {
Tracking.event(document.body.dataset.page, TRACKING_ACTION_CREATE_MERGE_REQUEST); Tracking.event(document.body.dataset.page, TRACKING_ACTION_CREATE_MERGE_REQUEST);
Api.trackRedisCounterEvent(USAGE_PING_TRACKING_ACTION_CREATE_MERGE_REQUEST); Api.trackRedisCounterEvent(USAGE_PING_TRACKING_ACTION_CREATE_MERGE_REQUEST);
...@@ -100,16 +93,17 @@ const submitContentChanges = ({ ...@@ -100,16 +93,17 @@ const submitContentChanges = ({
username, username,
projectId, projectId,
sourcePath, sourcePath,
targetBranch,
content, content,
images, images,
mergeRequestMeta, mergeRequestMeta,
formattedMarkdown, formattedMarkdown,
}) => { }) => {
const branch = generateBranchName(username); const branch = generateBranchName(username, targetBranch);
const { title: mergeRequestTitle, description: mergeRequestDescription } = mergeRequestMeta; const { title: mergeRequestTitle, description: mergeRequestDescription } = mergeRequestMeta;
const meta = {}; const meta = {};
return createBranch(projectId, branch) return createBranch(projectId, branch, targetBranch)
.then(({ data: { web_url: url } }) => { .then(({ data: { web_url: url } }) => {
const message = `${DEFAULT_FORMATTING_CHANGES_COMMIT_MESSAGE}\n\n${DEFAULT_FORMATTING_CHANGES_COMMIT_DESCRIPTION}`; const message = `${DEFAULT_FORMATTING_CHANGES_COMMIT_MESSAGE}\n\n${DEFAULT_FORMATTING_CHANGES_COMMIT_DESCRIPTION}`;
...@@ -133,7 +127,13 @@ const submitContentChanges = ({ ...@@ -133,7 +127,13 @@ const submitContentChanges = ({
.then(({ data: { short_id: label, web_url: url } }) => { .then(({ data: { short_id: label, web_url: url } }) => {
Object.assign(meta, { commit: { label, url } }); Object.assign(meta, { commit: { label, url } });
return createMergeRequest(projectId, mergeRequestTitle, mergeRequestDescription, branch); return createMergeRequest(
projectId,
mergeRequestTitle,
mergeRequestDescription,
branch,
targetBranch,
);
}) })
.then(({ data: { iid: label, web_url: url } }) => { .then(({ data: { iid: label, web_url: url } }) => {
Object.assign(meta, { mergeRequest: { label: label.toString(), url } }); Object.assign(meta, { mergeRequest: { label: label.toString(), url } });
......
...@@ -55,7 +55,7 @@ export const mergeRequestTemplates = [ ...@@ -55,7 +55,7 @@ export const mergeRequestTemplates = [
export const submitChangesError = 'Could not save changes'; export const submitChangesError = 'Could not save changes';
export const commitBranchResponse = { export const commitBranchResponse = {
web_url: '/tree/root-master-patch-88195', web_url: '/tree/root-main-patch-88195',
}; };
export const commitMultipleResponse = { export const commitMultipleResponse = {
short_id: 'ed899a2f4b5', short_id: 'ed899a2f4b5',
...@@ -84,8 +84,8 @@ export const mounts = [ ...@@ -84,8 +84,8 @@ export const mounts = [
}, },
]; ];
export const branch = 'master'; export const branch = 'main';
export const baseUrl = '/user1/project1/-/sse/master%2Ftest.md'; export const baseUrl = '/user1/project1/-/sse/main%2Ftest.md';
export const imageRoot = 'source/images/'; export const imageRoot = 'source/images/';
...@@ -275,6 +275,7 @@ describe('static_site_editor/pages/home', () => { ...@@ -275,6 +275,7 @@ describe('static_site_editor/pages/home', () => {
formattedMarkdown, formattedMarkdown,
project, project,
sourcePath, sourcePath,
targetBranch: branch,
username, username,
images, images,
mergeRequestMeta, mergeRequestMeta,
......
import { DEFAULT_TARGET_BRANCH, BRANCH_SUFFIX_COUNT } from '~/static_site_editor/constants'; import { BRANCH_SUFFIX_COUNT } from '~/static_site_editor/constants';
import generateBranchName from '~/static_site_editor/services/generate_branch_name'; import generateBranchName from '~/static_site_editor/services/generate_branch_name';
import { username } from '../mock_data'; import { username, branch as targetBranch } from '../mock_data';
describe('generateBranchName', () => { describe('generateBranchName', () => {
const timestamp = 12345678901234; const timestamp = 12345678901234;
...@@ -11,11 +11,11 @@ describe('generateBranchName', () => { ...@@ -11,11 +11,11 @@ describe('generateBranchName', () => {
}); });
it('generates a name that includes the username and target branch', () => { it('generates a name that includes the username and target branch', () => {
expect(generateBranchName(username)).toMatch(`${username}-${DEFAULT_TARGET_BRANCH}`); expect(generateBranchName(username, targetBranch)).toMatch(`${username}-${targetBranch}`);
}); });
it(`adds the first ${BRANCH_SUFFIX_COUNT} numbers of the current timestamp`, () => { it(`adds the first ${BRANCH_SUFFIX_COUNT} numbers of the current timestamp`, () => {
expect(generateBranchName(username)).toMatch( expect(generateBranchName(username, targetBranch)).toMatch(
timestamp.toString().substring(BRANCH_SUFFIX_COUNT), timestamp.toString().substring(BRANCH_SUFFIX_COUNT),
); );
}); });
......
...@@ -47,11 +47,11 @@ describe('rich_content_editor/renderers/render_image', () => { ...@@ -47,11 +47,11 @@ describe('rich_content_editor/renderers/render_image', () => {
it.each` it.each`
destination | isAbsolute | src destination | isAbsolute | src
${'http://test.host/absolute/path/to/image.png'} | ${true} | ${'http://test.host/absolute/path/to/image.png'} ${'http://test.host/absolute/path/to/image.png'} | ${true} | ${'http://test.host/absolute/path/to/image.png'}
${'/relative/path/to/image.png'} | ${false} | ${'http://test.host/user1/project1/-/raw/master/default/source/relative/path/to/image.png'} ${'/relative/path/to/image.png'} | ${false} | ${'http://test.host/user1/project1/-/raw/main/default/source/relative/path/to/image.png'}
${'/target/image.png'} | ${false} | ${'http://test.host/user1/project1/-/raw/master/source/with/target/image.png'} ${'/target/image.png'} | ${false} | ${'http://test.host/user1/project1/-/raw/main/source/with/target/image.png'}
${'relative/to/current/image.png'} | ${false} | ${'http://test.host/user1/project1/-/raw/master/relative/to/current/image.png'} ${'relative/to/current/image.png'} | ${false} | ${'http://test.host/user1/project1/-/raw/main/relative/to/current/image.png'}
${'./relative/to/current/image.png'} | ${false} | ${'http://test.host/user1/project1/-/raw/master/./relative/to/current/image.png'} ${'./relative/to/current/image.png'} | ${false} | ${'http://test.host/user1/project1/-/raw/main/./relative/to/current/image.png'}
${'../relative/to/current/image.png'} | ${false} | ${'http://test.host/user1/project1/-/raw/master/../relative/to/current/image.png'} ${'../relative/to/current/image.png'} | ${false} | ${'http://test.host/user1/project1/-/raw/main/../relative/to/current/image.png'}
`('returns an image with the correct attributes', ({ destination, isAbsolute, src }) => { `('returns an image with the correct attributes', ({ destination, isAbsolute, src }) => {
node.destination = destination; node.destination = destination;
......
...@@ -3,7 +3,6 @@ import Api from '~/api'; ...@@ -3,7 +3,6 @@ import Api from '~/api';
import { convertObjectPropsToSnakeCase } from '~/lib/utils/common_utils'; import { convertObjectPropsToSnakeCase } from '~/lib/utils/common_utils';
import { import {
DEFAULT_TARGET_BRANCH,
SUBMIT_CHANGES_BRANCH_ERROR, SUBMIT_CHANGES_BRANCH_ERROR,
SUBMIT_CHANGES_COMMIT_ERROR, SUBMIT_CHANGES_COMMIT_ERROR,
SUBMIT_CHANGES_MERGE_REQUEST_ERROR, SUBMIT_CHANGES_MERGE_REQUEST_ERROR,
...@@ -25,6 +24,7 @@ import { ...@@ -25,6 +24,7 @@ import {
createMergeRequestResponse, createMergeRequestResponse,
mergeRequestMeta, mergeRequestMeta,
sourcePath, sourcePath,
branch as targetBranch,
sourceContentYAML as content, sourceContentYAML as content,
trackingCategory, trackingCategory,
images, images,
...@@ -33,7 +33,7 @@ import { ...@@ -33,7 +33,7 @@ import {
jest.mock('~/static_site_editor/services/generate_branch_name'); jest.mock('~/static_site_editor/services/generate_branch_name');
describe('submitContentChanges', () => { describe('submitContentChanges', () => {
const branch = 'branch-name'; const sourceBranch = 'branch-name';
let trackingSpy; let trackingSpy;
let origPage; let origPage;
...@@ -41,6 +41,7 @@ describe('submitContentChanges', () => { ...@@ -41,6 +41,7 @@ describe('submitContentChanges', () => {
username, username,
projectId, projectId,
sourcePath, sourcePath,
targetBranch,
content, content,
images, images,
mergeRequestMeta, mergeRequestMeta,
...@@ -54,7 +55,7 @@ describe('submitContentChanges', () => { ...@@ -54,7 +55,7 @@ describe('submitContentChanges', () => {
.spyOn(Api, 'createProjectMergeRequest') .spyOn(Api, 'createProjectMergeRequest')
.mockResolvedValue({ data: createMergeRequestResponse }); .mockResolvedValue({ data: createMergeRequestResponse });
generateBranchName.mockReturnValue(branch); generateBranchName.mockReturnValue(sourceBranch);
origPage = document.body.dataset.page; origPage = document.body.dataset.page;
document.body.dataset.page = trackingCategory; document.body.dataset.page = trackingCategory;
...@@ -69,8 +70,8 @@ describe('submitContentChanges', () => { ...@@ -69,8 +70,8 @@ describe('submitContentChanges', () => {
it('creates a branch named after the username and target branch', () => { it('creates a branch named after the username and target branch', () => {
return submitContentChanges(buildPayload()).then(() => { return submitContentChanges(buildPayload()).then(() => {
expect(Api.createBranch).toHaveBeenCalledWith(projectId, { expect(Api.createBranch).toHaveBeenCalledWith(projectId, {
ref: DEFAULT_TARGET_BRANCH, ref: targetBranch,
branch, branch: sourceBranch,
}); });
}); });
}); });
...@@ -86,7 +87,7 @@ describe('submitContentChanges', () => { ...@@ -86,7 +87,7 @@ describe('submitContentChanges', () => {
describe('committing markdown formatting changes', () => { describe('committing markdown formatting changes', () => {
const formattedMarkdown = `formatted ${content}`; const formattedMarkdown = `formatted ${content}`;
const commitPayload = { const commitPayload = {
branch, branch: sourceBranch,
commit_message: `${DEFAULT_FORMATTING_CHANGES_COMMIT_MESSAGE}\n\n${DEFAULT_FORMATTING_CHANGES_COMMIT_DESCRIPTION}`, commit_message: `${DEFAULT_FORMATTING_CHANGES_COMMIT_MESSAGE}\n\n${DEFAULT_FORMATTING_CHANGES_COMMIT_DESCRIPTION}`,
actions: [ actions: [
{ {
...@@ -116,7 +117,7 @@ describe('submitContentChanges', () => { ...@@ -116,7 +117,7 @@ describe('submitContentChanges', () => {
it('commits the content changes to the branch when creating branch succeeds', () => { it('commits the content changes to the branch when creating branch succeeds', () => {
return submitContentChanges(buildPayload()).then(() => { return submitContentChanges(buildPayload()).then(() => {
expect(Api.commitMultiple).toHaveBeenCalledWith(projectId, { expect(Api.commitMultiple).toHaveBeenCalledWith(projectId, {
branch, branch: sourceBranch,
commit_message: mergeRequestMeta.title, commit_message: mergeRequestMeta.title,
actions: [ actions: [
{ {
...@@ -140,7 +141,7 @@ describe('submitContentChanges', () => { ...@@ -140,7 +141,7 @@ describe('submitContentChanges', () => {
const payload = buildPayload({ content: contentWithoutImages }); const payload = buildPayload({ content: contentWithoutImages });
return submitContentChanges(payload).then(() => { return submitContentChanges(payload).then(() => {
expect(Api.commitMultiple).toHaveBeenCalledWith(projectId, { expect(Api.commitMultiple).toHaveBeenCalledWith(projectId, {
branch, branch: sourceBranch,
commit_message: mergeRequestMeta.title, commit_message: mergeRequestMeta.title,
actions: [ actions: [
{ {
...@@ -169,8 +170,8 @@ describe('submitContentChanges', () => { ...@@ -169,8 +170,8 @@ describe('submitContentChanges', () => {
convertObjectPropsToSnakeCase({ convertObjectPropsToSnakeCase({
title, title,
description, description,
targetBranch: DEFAULT_TARGET_BRANCH, targetBranch,
sourceBranch: branch, sourceBranch,
}), }),
); );
}); });
...@@ -194,7 +195,7 @@ describe('submitContentChanges', () => { ...@@ -194,7 +195,7 @@ describe('submitContentChanges', () => {
}); });
it('returns the branch name', () => { it('returns the branch name', () => {
expect(result).toMatchObject({ branch: { label: branch } }); expect(result).toMatchObject({ branch: { label: sourceBranch } });
}); });
it('returns commit short id and web url', () => { it('returns commit short id and web url', () => {
......
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