summaryrefslogtreecommitdiff
path: root/app/assets/javascripts
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-04-14 09:09:34 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-04-14 09:09:34 +0000
commit97f0ae7454597105a27df65ffb772949d9d4f3cb (patch)
tree0bf4888e0e9082c8f168a211390a73a6ae810cef /app/assets/javascripts
parent5ebc4d92cd5fbb46c627eb04d500384893dbe2b4 (diff)
downloadgitlab-ce-97f0ae7454597105a27df65ffb772949d9d4f3cb.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts')
-rw-r--r--app/assets/javascripts/api.js9
-rw-r--r--app/assets/javascripts/snippets/components/snippet_blob_view.vue40
-rw-r--r--app/assets/javascripts/snippets/fragments/snippetBase.fragment.graphql15
-rw-r--r--app/assets/javascripts/snippets/queries/snippet.blob.query.graphql24
-rw-r--r--app/assets/javascripts/static_site_editor/constants.js12
-rw-r--r--app/assets/javascripts/static_site_editor/services/generate_branch_name.js8
-rw-r--r--app/assets/javascripts/static_site_editor/services/submit_content_changes.js76
-rw-r--r--app/assets/javascripts/static_site_editor/store/state.js2
8 files changed, 128 insertions, 58 deletions
diff --git a/app/assets/javascripts/api.js b/app/assets/javascripts/api.js
index 75f7fe62a7e..6301f6a3910 100644
--- a/app/assets/javascripts/api.js
+++ b/app/assets/javascripts/api.js
@@ -188,6 +188,15 @@ const Api = {
return axios.get(url, { params });
},
+ createProjectMergeRequest(projectPath, options) {
+ const url = Api.buildUrl(Api.projectMergeRequestsPath).replace(
+ ':id',
+ encodeURIComponent(projectPath),
+ );
+
+ return axios.post(url, options);
+ },
+
// Return Merge Request for project
projectMergeRequest(projectPath, mergeRequestId, params = {}) {
const url = Api.buildUrl(Api.projectMergeRequestPath)
diff --git a/app/assets/javascripts/snippets/components/snippet_blob_view.vue b/app/assets/javascripts/snippets/components/snippet_blob_view.vue
index 3e3dcab70c0..02a0fc7686d 100644
--- a/app/assets/javascripts/snippets/components/snippet_blob_view.vue
+++ b/app/assets/javascripts/snippets/components/snippet_blob_view.vue
@@ -3,10 +3,8 @@ import BlobEmbeddable from '~/blob/components/blob_embeddable.vue';
import { SNIPPET_VISIBILITY_PUBLIC } from '../constants';
import BlobHeader from '~/blob/components/blob_header.vue';
import BlobContent from '~/blob/components/blob_content.vue';
-import { GlLoadingIcon } from '@gitlab/ui';
import CloneDropdownButton from '~/vue_shared/components/clone_dropdown.vue';
-import GetSnippetBlobQuery from '../queries/snippet.blob.query.graphql';
import GetBlobContent from '../queries/snippet.blob.content.query.graphql';
import { SIMPLE_BLOB_VIEWER, RICH_BLOB_VIEWER } from '~/blob/components/constants';
@@ -16,25 +14,9 @@ export default {
BlobEmbeddable,
BlobHeader,
BlobContent,
- GlLoadingIcon,
CloneDropdownButton,
},
apollo: {
- blob: {
- query: GetSnippetBlobQuery,
- variables() {
- return {
- ids: this.snippet.id,
- };
- },
- update: data => data.snippets.edges[0].node.blob,
- result(res) {
- const viewer = res.data.snippets.edges[0].node.blob.richViewer
- ? RICH_BLOB_VIEWER
- : SIMPLE_BLOB_VIEWER;
- this.switchViewer(viewer, true);
- },
- },
blobContent: {
query: GetBlobContent,
variables() {
@@ -55,18 +37,18 @@ export default {
},
data() {
return {
- blob: {},
+ blob: this.snippet.blob,
blobContent: '',
- activeViewerType: window.location.hash ? SIMPLE_BLOB_VIEWER : '',
+ activeViewerType:
+ this.snippet.blob?.richViewer && !window.location.hash
+ ? RICH_BLOB_VIEWER
+ : SIMPLE_BLOB_VIEWER,
};
},
computed: {
embeddable() {
return this.snippet.visibilityLevel === SNIPPET_VISIBILITY_PUBLIC;
},
- isBlobLoading() {
- return this.$apollo.queries.blob.loading;
- },
isContentLoading() {
return this.$apollo.queries.blobContent.loading;
},
@@ -79,8 +61,8 @@ export default {
},
},
methods: {
- switchViewer(newViewer, respectHash = false) {
- this.activeViewerType = respectHash && window.location.hash ? SIMPLE_BLOB_VIEWER : newViewer;
+ switchViewer(newViewer) {
+ this.activeViewerType = newViewer;
},
},
};
@@ -88,13 +70,7 @@ export default {
<template>
<div>
<blob-embeddable v-if="embeddable" class="mb-3" :url="snippet.webUrl" />
- <gl-loading-icon
- v-if="isBlobLoading"
- :label="__('Loading blob')"
- size="lg"
- class="prepend-top-20 append-bottom-20"
- />
- <article v-else class="file-holder snippet-file-content">
+ <article class="file-holder snippet-file-content">
<blob-header :blob="blob" :active-viewer-type="viewer.type" @viewer-changed="switchViewer">
<template #actions>
<clone-dropdown-button
diff --git a/app/assets/javascripts/snippets/fragments/snippetBase.fragment.graphql b/app/assets/javascripts/snippets/fragments/snippetBase.fragment.graphql
index 22aab7c7795..d793d0b6bb4 100644
--- a/app/assets/javascripts/snippets/fragments/snippetBase.fragment.graphql
+++ b/app/assets/javascripts/snippets/fragments/snippetBase.fragment.graphql
@@ -1,3 +1,5 @@
+#import '~/graphql_shared/fragments/blobviewer.fragment.graphql'
+
fragment SnippetBase on Snippet {
id
title
@@ -9,6 +11,19 @@ fragment SnippetBase on Snippet {
webUrl
httpUrlToRepo
sshUrlToRepo
+ blob {
+ binary
+ name
+ path
+ rawPath
+ size
+ simpleViewer {
+ ...BlobViewer
+ }
+ richViewer {
+ ...BlobViewer
+ }
+ }
userPermissions {
adminSnippet
updateSnippet
diff --git a/app/assets/javascripts/snippets/queries/snippet.blob.query.graphql b/app/assets/javascripts/snippets/queries/snippet.blob.query.graphql
deleted file mode 100644
index 785c88c185a..00000000000
--- a/app/assets/javascripts/snippets/queries/snippet.blob.query.graphql
+++ /dev/null
@@ -1,24 +0,0 @@
-#import '~/graphql_shared/fragments/blobviewer.fragment.graphql'
-
-query SnippetBlobFull($ids: [ID!]) {
- snippets(ids: $ids) {
- edges {
- node {
- id
- blob {
- binary
- name
- path
- rawPath
- size
- simpleViewer {
- ...BlobViewer
- }
- richViewer {
- ...BlobViewer
- }
- }
- }
- }
- }
-}
diff --git a/app/assets/javascripts/static_site_editor/constants.js b/app/assets/javascripts/static_site_editor/constants.js
new file mode 100644
index 00000000000..5081d467016
--- /dev/null
+++ b/app/assets/javascripts/static_site_editor/constants.js
@@ -0,0 +1,12 @@
+import { s__ } from '~/locale';
+
+export const BRANCH_SUFFIX_COUNT = 8;
+export const DEFAULT_TARGET_BRANCH = 'master';
+
+export const SUBMIT_CHANGES_BRANCH_ERROR = s__('StaticSiteEditor|Branch could not be created.');
+export const SUBMIT_CHANGES_COMMIT_ERROR = s__(
+ 'StaticSiteEditor|Could not commit the content changes.',
+);
+export const SUBMIT_CHANGES_MERGE_REQUEST_ERROR = s__(
+ 'StaticSiteEditor|Could not create merge request.',
+);
diff --git a/app/assets/javascripts/static_site_editor/services/generate_branch_name.js b/app/assets/javascripts/static_site_editor/services/generate_branch_name.js
new file mode 100644
index 00000000000..f45ad616332
--- /dev/null
+++ b/app/assets/javascripts/static_site_editor/services/generate_branch_name.js
@@ -0,0 +1,8 @@
+import { BRANCH_SUFFIX_COUNT, DEFAULT_TARGET_BRANCH } from '../constants';
+
+const generateBranchSuffix = () => `${Date.now()}`.substr(BRANCH_SUFFIX_COUNT);
+
+const generateBranchName = (username, targetBranch = DEFAULT_TARGET_BRANCH) =>
+ `${username}-${targetBranch}-patch-${generateBranchSuffix()}`;
+
+export default generateBranchName;
diff --git a/app/assets/javascripts/static_site_editor/services/submit_content_changes.js b/app/assets/javascripts/static_site_editor/services/submit_content_changes.js
index 6b0d8c74ff7..ff591e4b245 100644
--- a/app/assets/javascripts/static_site_editor/services/submit_content_changes.js
+++ b/app/assets/javascripts/static_site_editor/services/submit_content_changes.js
@@ -1,4 +1,76 @@
-// TODO implement
-const submitContentChanges = () => new Promise(resolve => setTimeout(resolve, 1000));
+import Api from '~/api';
+import { s__, sprintf } from '~/locale';
+import { convertObjectPropsToSnakeCase } from '~/lib/utils/common_utils';
+import generateBranchName from '~/static_site_editor/services/generate_branch_name';
+
+import {
+ DEFAULT_TARGET_BRANCH,
+ SUBMIT_CHANGES_BRANCH_ERROR,
+ SUBMIT_CHANGES_COMMIT_ERROR,
+ SUBMIT_CHANGES_MERGE_REQUEST_ERROR,
+} from '../constants';
+
+const createBranch = (projectId, branch) =>
+ Api.createBranch(projectId, {
+ ref: DEFAULT_TARGET_BRANCH,
+ branch,
+ }).catch(() => {
+ throw new Error(SUBMIT_CHANGES_BRANCH_ERROR);
+ });
+
+const commitContent = (projectId, message, branch, sourcePath, content) =>
+ Api.commitMultiple(
+ projectId,
+ convertObjectPropsToSnakeCase({
+ branch,
+ commitMessage: message,
+ actions: [
+ convertObjectPropsToSnakeCase({
+ action: 'update',
+ filePath: sourcePath,
+ content,
+ }),
+ ],
+ }),
+ ).catch(() => {
+ throw new Error(SUBMIT_CHANGES_COMMIT_ERROR);
+ });
+
+const createMergeRequest = (projectId, title, sourceBranch, targetBranch = DEFAULT_TARGET_BRANCH) =>
+ Api.createProjectMergeRequest(
+ projectId,
+ convertObjectPropsToSnakeCase({
+ title,
+ sourceBranch,
+ targetBranch,
+ }),
+ ).catch(() => {
+ throw new Error(SUBMIT_CHANGES_MERGE_REQUEST_ERROR);
+ });
+
+const submitContentChanges = ({ username, projectId, sourcePath, content }) => {
+ const branch = generateBranchName(username);
+ const mergeRequestTitle = sprintf(s__(`StaticSiteEditor|Update %{sourcePath} file`), {
+ sourcePath,
+ });
+ const meta = {};
+
+ return createBranch(projectId, branch)
+ .then(() => {
+ Object.assign(meta, { branch: { label: branch } });
+
+ return commitContent(projectId, mergeRequestTitle, branch, sourcePath, content);
+ })
+ .then(({ data: { short_id: label, web_url: url } }) => {
+ Object.assign(meta, { commit: { label, url } });
+
+ return createMergeRequest(projectId, mergeRequestTitle, branch);
+ })
+ .then(({ data: { iid: label, web_url: url } }) => {
+ Object.assign(meta, { mergeRequest: { label, url } });
+
+ return meta;
+ });
+};
export default submitContentChanges;
diff --git a/app/assets/javascripts/static_site_editor/store/state.js b/app/assets/javascripts/static_site_editor/store/state.js
index e457fde591a..477ec540e02 100644
--- a/app/assets/javascripts/static_site_editor/store/state.js
+++ b/app/assets/javascripts/static_site_editor/store/state.js
@@ -10,6 +10,8 @@ const createState = (initialState = {}) => ({
content: '',
title: '',
+ savedContentMeta: null,
+
...initialState,
});