diff options
Diffstat (limited to 'app/assets/javascripts/releases/stores/modules')
5 files changed, 192 insertions, 173 deletions
diff --git a/app/assets/javascripts/releases/stores/modules/edit_new/actions.js b/app/assets/javascripts/releases/stores/modules/edit_new/actions.js index 8dc2083dd2b..b312c2a7506 100644 --- a/app/assets/javascripts/releases/stores/modules/edit_new/actions.js +++ b/app/assets/javascripts/releases/stores/modules/edit_new/actions.js @@ -1,14 +1,12 @@ -import api from '~/api'; import { deprecatedCreateFlash as createFlash } from '~/flash'; import { redirectTo } from '~/lib/utils/url_utility'; import { s__ } from '~/locale'; -import oneReleaseQuery from '~/releases/queries/one_release.query.graphql'; -import { - releaseToApiJson, - apiJsonToRelease, - gqClient, - convertOneReleaseGraphQLResponse, -} from '~/releases/util'; +import createReleaseMutation from '~/releases/graphql/mutations/create_release.mutation.graphql'; +import createReleaseAssetLinkMutation from '~/releases/graphql/mutations/create_release_link.mutation.graphql'; +import deleteReleaseAssetLinkMutation from '~/releases/graphql/mutations/delete_release_link.mutation.graphql'; +import updateReleaseMutation from '~/releases/graphql/mutations/update_release.mutation.graphql'; +import oneReleaseForEditingQuery from '~/releases/graphql/queries/one_release_for_editing.query.graphql'; +import { gqClient, convertOneReleaseGraphQLResponse } from '~/releases/util'; import * as types from './mutation_types'; export const initializeRelease = ({ commit, dispatch, getters }) => { @@ -24,38 +22,25 @@ export const initializeRelease = ({ commit, dispatch, getters }) => { return Promise.resolve(); }; -export const fetchRelease = ({ commit, state, rootState }) => { +export const fetchRelease = async ({ commit, state }) => { commit(types.REQUEST_RELEASE); - if (rootState.featureFlags?.graphqlIndividualReleasePage) { - return gqClient - .query({ - query: oneReleaseQuery, - variables: { - fullPath: state.projectPath, - tagName: state.tagName, - }, - }) - .then((response) => { - const { data: release } = convertOneReleaseGraphQLResponse(response); - - commit(types.RECEIVE_RELEASE_SUCCESS, release); - }) - .catch((error) => { - commit(types.RECEIVE_RELEASE_ERROR, error); - createFlash(s__('Release|Something went wrong while getting the release details.')); - }); - } - - return api - .release(state.projectId, state.tagName) - .then(({ data }) => { - commit(types.RECEIVE_RELEASE_SUCCESS, apiJsonToRelease(data)); - }) - .catch((error) => { - commit(types.RECEIVE_RELEASE_ERROR, error); - createFlash(s__('Release|Something went wrong while getting the release details.')); + try { + const fetchResponse = await gqClient.query({ + query: oneReleaseForEditingQuery, + variables: { + fullPath: state.projectPath, + tagName: state.tagName, + }, }); + + const { data: release } = convertOneReleaseGraphQLResponse(fetchResponse); + + commit(types.RECEIVE_RELEASE_SUCCESS, release); + } catch (error) { + commit(types.RECEIVE_RELEASE_ERROR, error); + createFlash(s__('Release|Something went wrong while getting the release details.')); + } }; export const updateReleaseTagName = ({ commit }, tagName) => @@ -94,9 +79,9 @@ export const removeAssetLink = ({ commit }, linkIdToRemove) => { commit(types.REMOVE_ASSET_LINK, linkIdToRemove); }; -export const receiveSaveReleaseSuccess = ({ commit }, release) => { +export const receiveSaveReleaseSuccess = ({ commit }, urlToRedirectTo) => { commit(types.RECEIVE_SAVE_RELEASE_SUCCESS); - redirectTo(release._links.self); + redirectTo(urlToRedirectTo); }; export const saveRelease = ({ commit, dispatch, getters }) => { @@ -105,83 +90,130 @@ export const saveRelease = ({ commit, dispatch, getters }) => { dispatch(getters.isExistingRelease ? 'updateRelease' : 'createRelease'); }; -export const createRelease = ({ commit, dispatch, state, getters }) => { - const apiJson = releaseToApiJson( - { - ...state.release, - assets: { - links: getters.releaseLinksToCreate, - }, - }, - state.createFrom, - ); +/** + * Tests a GraphQL mutation response for the existence of any errors-as-data + * (See https://docs.gitlab.com/ee/development/fe_guide/graphql.html#errors-as-data). + * If any errors occurred, throw a JavaScript `Error` object, so that this can be + * handled by the global error handler. + * + * @param {Object} gqlResponse The response object returned by the GraphQL client + * @param {String} mutationName The name of the mutation that was executed + * @param {String} messageIfError An message to build into the error object if something went wrong + */ +const checkForErrorsAsData = (gqlResponse, mutationName, messageIfError) => { + const allErrors = gqlResponse.data[mutationName].errors; + if (allErrors.length > 0) { + const allErrorMessages = JSON.stringify(allErrors); + throw new Error(`${messageIfError}: ${allErrorMessages}`); + } +}; - return api - .createRelease(state.projectId, apiJson) - .then(({ data }) => { - dispatch('receiveSaveReleaseSuccess', apiJsonToRelease(data)); - }) - .catch((error) => { - commit(types.RECEIVE_SAVE_RELEASE_ERROR, error); - createFlash(s__('Release|Something went wrong while creating a new release')); +export const createRelease = async ({ commit, dispatch, state, getters }) => { + try { + const response = await gqClient.mutate({ + mutation: createReleaseMutation, + variables: getters.releaseCreateMutatationVariables, }); + + checkForErrorsAsData( + response, + 'releaseCreate', + `Something went wrong while creating a new release with projectPath "${state.projectPath}" and tagName "${state.release.tagName}"`, + ); + + dispatch('receiveSaveReleaseSuccess', response.data.releaseCreate.release.links.selfUrl); + } catch (error) { + commit(types.RECEIVE_SAVE_RELEASE_ERROR, error); + createFlash(s__('Release|Something went wrong while creating a new release.')); + } }; -export const updateRelease = ({ commit, dispatch, state, getters }) => { - const apiJson = releaseToApiJson({ - ...state.release, - assets: { - links: getters.releaseLinksToCreate, +/** + * Deletes a single release link. + * Throws an error if any network or validation errors occur. + */ +const deleteReleaseLinks = async ({ state, id }) => { + const deleteResponse = await gqClient.mutate({ + mutation: deleteReleaseAssetLinkMutation, + variables: { + input: { id }, }, }); - let updatedRelease = null; - - return ( - api - .updateRelease(state.projectId, state.tagName, apiJson) - - /** - * Currently, we delete all existing links and then - * recreate new ones on each edit. This is because the - * REST API doesn't support bulk updating of Release links, - * and updating individual links can lead to validation - * race conditions (in particular, the "URLs must be unique") - * constraint. - * - * This isn't ideal since this is no longer an atomic - * operation - parts of it can fail while others succeed, - * leaving the Release in an inconsistent state. - * - * This logic should be refactored to use GraphQL once - * https://gitlab.com/gitlab-org/gitlab/-/issues/208702 - * is closed. - */ - .then(({ data }) => { - // Save this response since we need it later in the Promise chain - updatedRelease = data; - - // Delete all links currently associated with this Release - return Promise.all( - getters.releaseLinksToDelete.map((l) => - api.deleteReleaseLink(state.projectId, state.release.tagName, l.id), - ), - ); - }) - .then(() => { - // Create a new link for each link in the form - return Promise.all( - apiJson.assets.links.map((l) => - api.createReleaseLink(state.projectId, state.release.tagName, l), - ), - ); - }) - .then(() => { - dispatch('receiveSaveReleaseSuccess', apiJsonToRelease(updatedRelease)); - }) - .catch((error) => { - commit(types.RECEIVE_SAVE_RELEASE_ERROR, error); - createFlash(s__('Release|Something went wrong while saving the release details')); - }) + checkForErrorsAsData( + deleteResponse, + 'releaseAssetLinkDelete', + `Something went wrong while deleting release asset link for release with projectPath "${state.projectPath}", tagName "${state.tagName}", and link id "${id}"`, ); }; + +/** + * Creates a single release link. + * Throws an error if any network or validation errors occur. + */ +const createReleaseLink = async ({ state, link }) => { + const createResponse = await gqClient.mutate({ + mutation: createReleaseAssetLinkMutation, + variables: { + input: { + projectPath: state.projectPath, + tagName: state.tagName, + name: link.name, + url: link.url, + linkType: link.linkType.toUpperCase(), + }, + }, + }); + + checkForErrorsAsData( + createResponse, + 'releaseAssetLinkCreate', + `Something went wrong while creating a release asset link for release with projectPath "${state.projectPath}" and tagName "${state.tagName}"`, + ); +}; + +export const updateRelease = async ({ commit, dispatch, state, getters }) => { + try { + /** + * Currently, we delete all existing links and then + * recreate new ones on each edit. This is because the + * backend doesn't support bulk updating of Release links, + * and updating individual links can lead to validation + * race conditions (in particular, the "URLs must be unique") + * constraint. + * + * This isn't ideal since this is no longer an atomic + * operation - parts of it can fail while others succeed, + * leaving the Release in an inconsistent state. + * + * This logic should be refactored to take place entirely + * in the backend. This is being discussed in + * https://gitlab.com/gitlab-org/gitlab/-/merge_requests/50300 + */ + const updateReleaseResponse = await gqClient.mutate({ + mutation: updateReleaseMutation, + variables: getters.releaseUpdateMutatationVariables, + }); + + checkForErrorsAsData( + updateReleaseResponse, + 'releaseUpdate', + `Something went wrong while updating release with projectPath "${state.projectPath}" and tagName "${state.tagName}"`, + ); + + // Delete all links currently associated with this Release + await Promise.all( + getters.releaseLinksToDelete.map(({ id }) => deleteReleaseLinks({ state, id })), + ); + + // Create a new link for each link in the form + await Promise.all( + getters.releaseLinksToCreate.map((link) => createReleaseLink({ state, link })), + ); + + dispatch('receiveSaveReleaseSuccess', state.release._links.self); + } catch (error) { + commit(types.RECEIVE_SAVE_RELEASE_ERROR, error); + createFlash(s__('Release|Something went wrong while saving the release details.')); + } +}; diff --git a/app/assets/javascripts/releases/stores/modules/edit_new/getters.js b/app/assets/javascripts/releases/stores/modules/edit_new/getters.js index 831037c8861..d83ec05872a 100644 --- a/app/assets/javascripts/releases/stores/modules/edit_new/getters.js +++ b/app/assets/javascripts/releases/stores/modules/edit_new/getters.js @@ -103,3 +103,39 @@ export const isValid = (_state, getters) => { const errors = getters.validationErrors; return Object.values(errors.assets.links).every(isEmpty) && !errors.isTagNameEmpty; }; + +/** Returns all the variables for a `releaseUpdate` GraphQL mutation */ +export const releaseUpdateMutatationVariables = (state) => { + const name = state.release.name?.trim().length > 0 ? state.release.name.trim() : null; + + // Milestones may be either a list of milestone objects OR just a list + // of milestone titles. The GraphQL mutation requires only the titles be sent. + const milestones = (state.release.milestones || []).map((m) => m.title || m); + + return { + input: { + projectPath: state.projectPath, + tagName: state.release.tagName, + name, + description: state.release.description, + milestones, + }, + }; +}; + +/** Returns all the variables for a `releaseCreate` GraphQL mutation */ +export const releaseCreateMutatationVariables = (state, getters) => { + return { + input: { + ...getters.releaseUpdateMutatationVariables.input, + ref: state.createFrom, + assets: { + links: getters.releaseLinksToCreate.map(({ name, url, linkType }) => ({ + name, + url, + linkType: linkType.toUpperCase(), + })), + }, + }, + }; +}; diff --git a/app/assets/javascripts/releases/stores/modules/index/actions.js b/app/assets/javascripts/releases/stores/modules/index/actions.js index f1add54626a..00be25f089b 100644 --- a/app/assets/javascripts/releases/stores/modules/index/actions.js +++ b/app/assets/javascripts/releases/stores/modules/index/actions.js @@ -1,45 +1,21 @@ -import api from '~/api'; import { deprecatedCreateFlash as createFlash } from '~/flash'; -import { - normalizeHeaders, - parseIntPagination, - convertObjectPropsToCamelCase, -} from '~/lib/utils/common_utils'; import { __ } from '~/locale'; -import allReleasesQuery from '~/releases/queries/all_releases.query.graphql'; -import { PAGE_SIZE } from '../../../constants'; -import { gqClient, convertAllReleasesGraphQLResponse } from '../../../util'; +import { PAGE_SIZE } from '~/releases/constants'; +import allReleasesQuery from '~/releases/graphql/queries/all_releases.query.graphql'; +import { gqClient, convertAllReleasesGraphQLResponse } from '~/releases/util'; import * as types from './mutation_types'; /** - * Gets a paginated list of releases from the server + * Gets a paginated list of releases from the GraphQL endpoint * * @param {Object} vuexParams * @param {Object} actionParams - * @param {Number} [actionParams.page] The page number of results to fetch - * (this parameter is only used when fetching results from the REST API) * @param {String} [actionParams.before] A GraphQL cursor. If provided, - * the items returned will proceed the provided cursor (this parameter is only - * used when fetching results from the GraphQL API). + * the items returned will proceed the provided cursor. * @param {String} [actionParams.after] A GraphQL cursor. If provided, - * the items returned will follow the provided cursor (this parameter is only - * used when fetching results from the GraphQL API). + * the items returned will follow the provided cursor. */ -export const fetchReleases = ({ dispatch, rootGetters }, { page = 1, before, after }) => { - if (rootGetters.useGraphQLEndpoint) { - dispatch('fetchReleasesGraphQl', { before, after }); - } else { - dispatch('fetchReleasesRest', { page }); - } -}; - -/** - * Gets a paginated list of releases from the GraphQL endpoint - */ -export const fetchReleasesGraphQl = ( - { dispatch, commit, state }, - { before = null, after = null }, -) => { +export const fetchReleases = ({ dispatch, commit, state }, { before, after }) => { commit(types.REQUEST_RELEASES); const { sort, orderBy } = state.sorting; @@ -55,7 +31,7 @@ export const fetchReleasesGraphQl = ( paginationParams = { first: PAGE_SIZE, after }; } else { throw new Error( - 'Both a `before` and an `after` parameter were provided to fetchReleasesGraphQl. These parameters cannot be used together.', + 'Both a `before` and an `after` parameter were provided to fetchReleases. These parameters cannot be used together.', ); } @@ -69,33 +45,11 @@ export const fetchReleasesGraphQl = ( }, }) .then((response) => { - const { data, paginationInfo: graphQlPageInfo } = convertAllReleasesGraphQLResponse(response); + const { data, paginationInfo: pageInfo } = convertAllReleasesGraphQLResponse(response); commit(types.RECEIVE_RELEASES_SUCCESS, { data, - graphQlPageInfo, - }); - }) - .catch(() => dispatch('receiveReleasesError')); -}; - -/** - * Gets a paginated list of releases from the REST endpoint - */ -export const fetchReleasesRest = ({ dispatch, commit, state }, { page }) => { - commit(types.REQUEST_RELEASES); - - const { sort, orderBy } = state.sorting; - - api - .releases(state.projectId, { page, sort, order_by: orderBy }) - .then(({ data, headers }) => { - const restPageInfo = parseIntPagination(normalizeHeaders(headers)); - const camelCasedReleases = convertObjectPropsToCamelCase(data, { deep: true }); - - commit(types.RECEIVE_RELEASES_SUCCESS, { - data: camelCasedReleases, - restPageInfo, + pageInfo, }); }) .catch(() => dispatch('receiveReleasesError')); diff --git a/app/assets/javascripts/releases/stores/modules/index/mutations.js b/app/assets/javascripts/releases/stores/modules/index/mutations.js index e1aaa2e2a19..55a8a488be8 100644 --- a/app/assets/javascripts/releases/stores/modules/index/mutations.js +++ b/app/assets/javascripts/releases/stores/modules/index/mutations.js @@ -17,12 +17,11 @@ export default { * @param {Object} state * @param {Object} resp */ - [types.RECEIVE_RELEASES_SUCCESS](state, { data, restPageInfo, graphQlPageInfo }) { + [types.RECEIVE_RELEASES_SUCCESS](state, { data, pageInfo }) { state.hasError = false; state.isLoading = false; state.releases = data; - state.restPageInfo = restPageInfo; - state.graphQlPageInfo = graphQlPageInfo; + state.pageInfo = pageInfo; }, /** @@ -36,8 +35,7 @@ export default { state.isLoading = false; state.releases = []; state.hasError = true; - state.restPageInfo = {}; - state.graphQlPageInfo = {}; + state.pageInfo = {}; }, [types.SET_SORTING](state, sorting) { diff --git a/app/assets/javascripts/releases/stores/modules/index/state.js b/app/assets/javascripts/releases/stores/modules/index/state.js index 164a496d450..5e1aaab7b58 100644 --- a/app/assets/javascripts/releases/stores/modules/index/state.js +++ b/app/assets/javascripts/releases/stores/modules/index/state.js @@ -16,8 +16,7 @@ export default ({ isLoading: false, hasError: false, releases: [], - restPageInfo: {}, - graphQlPageInfo: {}, + pageInfo: {}, sorting: { sort: DESCENDING_ORDER, orderBy: RELEASED_AT, |