diff --git a/app/assets/javascripts/add_context_commits_modal/components/add_context_commits_modal_trigger.vue b/app/assets/javascripts/add_context_commits_modal/components/add_context_commits_modal_trigger.vue new file mode 100644 index 0000000000000000000000000000000000000000..78a575ffe965a7735598e2b48343a31c05ad1842 --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/components/add_context_commits_modal_trigger.vue @@ -0,0 +1,49 @@ + + + diff --git a/app/assets/javascripts/add_context_commits_modal/components/add_context_commits_modal_wrapper.vue b/app/assets/javascripts/add_context_commits_modal/components/add_context_commits_modal_wrapper.vue new file mode 100644 index 0000000000000000000000000000000000000000..6862ed211a98204c4e3f5caf378a5f6f73f8735a --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/components/add_context_commits_modal_wrapper.vue @@ -0,0 +1,279 @@ + + + diff --git a/app/assets/javascripts/add_context_commits_modal/components/review_tab_container.vue b/app/assets/javascripts/add_context_commits_modal/components/review_tab_container.vue new file mode 100644 index 0000000000000000000000000000000000000000..36e3449ff27f0afe7b846ea814972f60c8a3490a --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/components/review_tab_container.vue @@ -0,0 +1,57 @@ + + diff --git a/app/assets/javascripts/add_context_commits_modal/event_hub.js b/app/assets/javascripts/add_context_commits_modal/event_hub.js new file mode 100644 index 0000000000000000000000000000000000000000..e31806ad199a1105985ed7045009d84e33856386 --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/event_hub.js @@ -0,0 +1,3 @@ +import createEventHub from '~/helpers/event_hub_factory'; + +export default createEventHub(); diff --git a/app/assets/javascripts/add_context_commits_modal/index.js b/app/assets/javascripts/add_context_commits_modal/index.js new file mode 100644 index 0000000000000000000000000000000000000000..b5cd111fabce9a5ba7acdf05b3cbb440b0f94780 --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/index.js @@ -0,0 +1,64 @@ +import Vue from 'vue'; +import { parseBoolean } from '~/lib/utils/common_utils'; +import createStore from './store'; +import AddContextCommitsModalTrigger from './components/add_context_commits_modal_trigger.vue'; +import AddContextCommitsModalWrapper from './components/add_context_commits_modal_wrapper.vue'; + +export default function initAddContextCommitsTriggers() { + const addContextCommitsModalTriggerEl = document.querySelector('.add-review-item-modal-trigger'); + const addContextCommitsModalWrapperEl = document.querySelector('.add-review-item-modal-wrapper'); + + if (addContextCommitsModalTriggerEl || addContextCommitsModalWrapperEl) { + // eslint-disable-next-line no-new + new Vue({ + el: addContextCommitsModalTriggerEl, + data() { + const { commitsEmpty, contextCommitsEmpty } = this.$options.el.dataset; + return { + commitsEmpty: parseBoolean(commitsEmpty), + contextCommitsEmpty: parseBoolean(contextCommitsEmpty), + }; + }, + render(createElement) { + return createElement(AddContextCommitsModalTrigger, { + props: { + commitsEmpty: this.commitsEmpty, + contextCommitsEmpty: this.contextCommitsEmpty, + }, + }); + }, + }); + + const store = createStore(); + + // eslint-disable-next-line no-new + new Vue({ + el: addContextCommitsModalWrapperEl, + store, + data() { + const { + contextCommitsPath, + targetBranch, + mergeRequestIid, + projectId, + } = this.$options.el.dataset; + return { + contextCommitsPath, + targetBranch, + mergeRequestIid: Number(mergeRequestIid), + projectId: Number(projectId), + }; + }, + render(createElement) { + return createElement(AddContextCommitsModalWrapper, { + props: { + contextCommitsPath: this.contextCommitsPath, + targetBranch: this.targetBranch, + mergeRequestIid: this.mergeRequestIid, + projectId: this.projectId, + }, + }); + }, + }); + } +} diff --git a/app/assets/javascripts/add_context_commits_modal/store/actions.js b/app/assets/javascripts/add_context_commits_modal/store/actions.js new file mode 100644 index 0000000000000000000000000000000000000000..77f60490bc6a7c512cbdd41edd5571e7957c481b --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/store/actions.js @@ -0,0 +1,134 @@ +import _ from 'lodash'; +import axios from '~/lib/utils/axios_utils'; +import createFlash from '~/flash'; +import { s__ } from '~/locale'; +import Api from '~/api'; +import * as types from './mutation_types'; + +export const setBaseConfig = ({ commit }, options) => { + commit(types.SET_BASE_CONFIG, options); +}; + +export const setTabIndex = ({ commit }, tabIndex) => commit(types.SET_TABINDEX, tabIndex); + +export const searchCommits = ({ dispatch, commit, state }, searchText) => { + commit(types.FETCH_COMMITS); + + let params = {}; + if (searchText) { + params = { + params: { + search: searchText, + per_page: 40, + }, + }; + } + + return axios + .get(state.contextCommitsPath, params) + .then(({ data }) => { + let commits = data.map(o => ({ ...o, isSelected: false })); + commits = commits.map(c => { + const isPresent = state.selectedCommits.find( + selectedCommit => selectedCommit.short_id === c.short_id && selectedCommit.isSelected, + ); + if (isPresent) { + return { ...c, isSelected: true }; + } + return c; + }); + if (!searchText) { + dispatch('setCommits', { commits: [...commits, ...state.contextCommits] }); + } else { + dispatch('setCommits', { commits }); + } + }) + .catch(() => { + commit(types.FETCH_COMMITS_ERROR); + }); +}; + +export const setCommits = ({ commit }, { commits: data, silentAddition = false }) => { + let commits = _.uniqBy(data, 'short_id'); + commits = _.orderBy(data, c => new Date(c.committed_date), ['desc']); + if (silentAddition) { + commit(types.SET_COMMITS_SILENT, commits); + } else { + commit(types.SET_COMMITS, commits); + } +}; + +export const createContextCommits = ({ state }, { commits, forceReload = false }) => + Api.createContextCommits(state.projectId, state.mergeRequestIid, { + commits: commits.map(commit => commit.short_id), + }) + .then(() => { + if (forceReload) { + window.location.reload(); + } + + return true; + }) + .catch(() => { + if (forceReload) { + createFlash(s__('ContextCommits|Failed to create context commits. Please try again.')); + } + + return false; + }); + +export const fetchContextCommits = ({ dispatch, commit, state }) => { + commit(types.FETCH_CONTEXT_COMMITS); + return Api.allContextCommits(state.projectId, state.mergeRequestIid) + .then(({ data }) => { + const contextCommits = data.map(o => ({ ...o, isSelected: true })); + dispatch('setContextCommits', contextCommits); + dispatch('setCommits', { + commits: [...state.commits, ...contextCommits], + silentAddition: true, + }); + dispatch('setSelectedCommits', contextCommits); + }) + .catch(() => { + commit(types.FETCH_CONTEXT_COMMITS_ERROR); + }); +}; + +export const setContextCommits = ({ commit }, data) => { + commit(types.SET_CONTEXT_COMMITS, data); +}; + +export const removeContextCommits = ({ state }, forceReload = false) => + Api.removeContextCommits(state.projectId, state.mergeRequestIid, { + commits: state.toRemoveCommits, + }) + .then(() => { + if (forceReload) { + window.location.reload(); + } + + return true; + }) + .catch(() => { + if (forceReload) { + createFlash(s__('ContextCommits|Failed to delete context commits. Please try again.')); + } + + return false; + }); + +export const setSelectedCommits = ({ commit }, selected) => { + let selectedCommits = _.uniqBy(selected, 'short_id'); + selectedCommits = _.orderBy( + selectedCommits, + selectedCommit => new Date(selectedCommit.committed_date), + ['desc'], + ); + commit(types.SET_SELECTED_COMMITS, selectedCommits); +}; + +export const setSearchText = ({ commit }, searchText) => commit(types.SET_SEARCH_TEXT, searchText); + +export const setToRemoveCommits = ({ commit }, data) => commit(types.SET_TO_REMOVE_COMMITS, data); + +export const resetModalState = ({ commit }) => commit(types.RESET_MODAL_STATE); diff --git a/app/assets/javascripts/add_context_commits_modal/store/index.js b/app/assets/javascripts/add_context_commits_modal/store/index.js new file mode 100644 index 0000000000000000000000000000000000000000..0bf3441379b4edb8bf856c3f8fe33d1858b8d5f8 --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/store/index.js @@ -0,0 +1,15 @@ +import Vue from 'vue'; +import Vuex from 'vuex'; +import * as actions from './actions'; +import mutations from './mutations'; +import state from './state'; + +Vue.use(Vuex); + +export default () => + new Vuex.Store({ + namespaced: true, + state: state(), + actions, + mutations, + }); diff --git a/app/assets/javascripts/add_context_commits_modal/store/mutation_types.js b/app/assets/javascripts/add_context_commits_modal/store/mutation_types.js new file mode 100644 index 0000000000000000000000000000000000000000..eda82f3984dae85db2cd252e38c5e842fa4c7b79 --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/store/mutation_types.js @@ -0,0 +1,20 @@ +export const SET_BASE_CONFIG = 'SET_BASE_CONFIG'; + +export const SET_TABINDEX = 'SET_TABINDEX'; + +export const FETCH_COMMITS = 'FETCH_COMMITS'; +export const SET_COMMITS = 'SET_COMMITS'; +export const SET_COMMITS_SILENT = 'SET_COMMITS_SILENT'; +export const FETCH_COMMITS_ERROR = 'FETCH_COMMITS_ERROR'; + +export const FETCH_CONTEXT_COMMITS = 'FETCH_CONTEXT_COMMITS'; +export const SET_CONTEXT_COMMITS = 'SET_CONTEXT_COMMITS'; +export const FETCH_CONTEXT_COMMITS_ERROR = 'FETCH_CONTEXT_COMMITS_ERROR'; + +export const SET_SELECTED_COMMITS = 'SET_SELECTED_COMMITS'; + +export const SET_SEARCH_TEXT = 'SET_SEARCH_TEXT'; + +export const SET_TO_REMOVE_COMMITS = 'SET_TO_REMOVE_COMMITS'; + +export const RESET_MODAL_STATE = 'RESET_MODAL_STATE'; diff --git a/app/assets/javascripts/add_context_commits_modal/store/mutations.js b/app/assets/javascripts/add_context_commits_modal/store/mutations.js new file mode 100644 index 0000000000000000000000000000000000000000..8a3da0ca248f97e60fc138535f5603e8bf704e3d --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/store/mutations.js @@ -0,0 +1,56 @@ +import * as types from './mutation_types'; + +export default { + [types.SET_BASE_CONFIG](state, options) { + Object.assign(state, { ...options }); + }, + [types.SET_TABINDEX](state, tabIndex) { + state.tabIndex = tabIndex; + }, + [types.FETCH_COMMITS](state) { + state.isLoadingCommits = true; + state.commitsLoadingError = false; + }, + [types.SET_COMMITS](state, commits) { + state.commits = commits; + state.isLoadingCommits = false; + state.commitsLoadingError = false; + }, + [types.SET_COMMITS_SILENT](state, commits) { + state.commits = commits; + }, + [types.FETCH_COMMITS_ERROR](state) { + state.commitsLoadingError = true; + state.isLoadingCommits = false; + }, + [types.FETCH_CONTEXT_COMMITS](state) { + state.isLoadingContextCommits = true; + state.contextCommitsLoadingError = false; + }, + [types.SET_CONTEXT_COMMITS](state, contextCommits) { + state.contextCommits = contextCommits; + state.isLoadingContextCommits = false; + state.contextCommitsLoadingError = false; + }, + [types.FETCH_CONTEXT_COMMITS_ERROR](state) { + state.contextCommitsLoadingError = true; + state.isLoadingContextCommits = false; + }, + [types.SET_SELECTED_COMMITS](state, commits) { + state.selectedCommits = commits; + }, + [types.SET_SEARCH_TEXT](state, searchText) { + state.searchText = searchText; + }, + [types.SET_TO_REMOVE_COMMITS](state, commits) { + state.toRemoveCommits = commits; + }, + [types.RESET_MODAL_STATE](state) { + state.tabIndex = 0; + state.commits = []; + state.contextCommits = []; + state.selectedCommits = []; + state.toRemoveCommits = []; + state.searchText = ''; + }, +}; diff --git a/app/assets/javascripts/add_context_commits_modal/store/state.js b/app/assets/javascripts/add_context_commits_modal/store/state.js new file mode 100644 index 0000000000000000000000000000000000000000..37239adccbbf589f4415d03d01f779eb8397ba1e --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/store/state.js @@ -0,0 +1,13 @@ +export default () => ({ + contextCommitsPath: '', + tabIndex: 0, + isLoadingCommits: false, + commits: [], + commitsLoadingError: false, + selectedCommits: [], + isLoadingContextCommits: false, + contextCommits: [], + contextCommitsLoadingError: false, + searchText: '', + toRemoveCommits: [], +}); diff --git a/app/assets/javascripts/add_context_commits_modal/utils.js b/app/assets/javascripts/add_context_commits_modal/utils.js new file mode 100644 index 0000000000000000000000000000000000000000..3495ee17cd3bdc557c06b9528a6249e413a9e04e --- /dev/null +++ b/app/assets/javascripts/add_context_commits_modal/utils.js @@ -0,0 +1,32 @@ +export const findCommitIndex = (commits, commitShortId) => { + return commits.findIndex(commit => commit.short_id === commitShortId); +}; + +export const setCommitStatus = (commits, commitIndex, selected) => { + const tempCommits = [...commits]; + tempCommits[commitIndex] = { + ...tempCommits[commitIndex], + isSelected: selected, + }; + return tempCommits; +}; + +export const removeIfReadyToBeRemoved = (toRemoveCommits, commitShortId) => { + const tempToRemoveCommits = [...toRemoveCommits]; + const isPresentInToRemove = tempToRemoveCommits.indexOf(commitShortId); + if (isPresentInToRemove !== -1) { + tempToRemoveCommits.splice(isPresentInToRemove, 1); + } + + return tempToRemoveCommits; +}; + +export const removeIfPresent = (selectedCommits, commitShortId) => { + const tempSelectedCommits = [...selectedCommits]; + const selectedCommitsIndex = findCommitIndex(tempSelectedCommits, commitShortId); + if (selectedCommitsIndex !== -1) { + tempSelectedCommits.splice(selectedCommitsIndex, 1); + } + + return tempSelectedCommits; +}; diff --git a/app/assets/javascripts/api.js b/app/assets/javascripts/api.js index dc70cc61014dc2535f939a05242a75b372ebb46a..b49259a09f9435e17324c7406f8cc8f15ea0e969 100644 --- a/app/assets/javascripts/api.js +++ b/app/assets/javascripts/api.js @@ -57,6 +57,8 @@ const Api = { pipelinesPath: '/api/:version/projects/:id/pipelines/', createPipelinePath: '/api/:version/projects/:id/pipeline', environmentsPath: '/api/:version/projects/:id/environments', + contextCommitsPath: + '/api/:version/projects/:id/merge_requests/:merge_request_iid/context_commits', rawFilePath: '/api/:version/projects/:id/repository/files/:path/raw', issuePath: '/api/:version/projects/:id/issues/:issue_iid', tagsPath: '/api/:version/projects/:id/repository/tags', @@ -598,6 +600,30 @@ const Api = { return axios.get(url); }, + createContextCommits(id, mergeRequestIid, data) { + const url = Api.buildUrl(this.contextCommitsPath) + .replace(':id', encodeURIComponent(id)) + .replace(':merge_request_iid', mergeRequestIid); + + return axios.post(url, data); + }, + + allContextCommits(id, mergeRequestIid) { + const url = Api.buildUrl(this.contextCommitsPath) + .replace(':id', encodeURIComponent(id)) + .replace(':merge_request_iid', mergeRequestIid); + + return axios.get(url); + }, + + removeContextCommits(id, mergeRequestIid, data) { + const url = Api.buildUrl(this.contextCommitsPath) + .replace(':id', id) + .replace(':merge_request_iid', mergeRequestIid); + + return axios.delete(url, { data }); + }, + getRawFile(id, path, params = { ref: 'master' }) { const url = Api.buildUrl(this.rawFilePath) .replace(':id', encodeURIComponent(id)) diff --git a/app/assets/javascripts/diffs/components/commit_item.vue b/app/assets/javascripts/diffs/components/commit_item.vue index f579b2ae2babf1e54179c21e8c986815594925fd..274a4027e621cbbf4cf386bdad137744be2ffa3f 100644 --- a/app/assets/javascripts/diffs/components/commit_item.vue +++ b/app/assets/javascripts/diffs/components/commit_item.vue @@ -52,10 +52,20 @@ export default { }, mixins: [glFeatureFlagsMixin()], props: { + isSelectable: { + type: Boolean, + required: false, + default: false, + }, commit: { type: Object, required: true, }, + checked: { + type: Boolean, + required: false, + default: false, + }, collapsible: { type: Boolean, required: false, @@ -83,6 +93,10 @@ export default { authorAvatar() { return this.author.avatar_url || this.commit.author_gravatar_url; }, + commitDescription() { + // Strip the newline at the beginning + return this.commit.description_html.replace(/^ /, ''); + }, nextCommitUrl() { return this.commit.next_commit_id ? setUrlParams({ commit_id: this.commit.next_commit_id }) @@ -110,13 +124,22 @@ export default {