debian-mirror-gitlab/app/assets/javascripts/environments/graphql/resolvers.js

168 lines
5.7 KiB
JavaScript
Raw Normal View History

2021-12-11 22:18:48 +05:30
import axios from '~/lib/utils/axios_utils';
2022-01-26 12:08:38 +05:30
import { s__ } from '~/locale';
import {
convertObjectPropsToCamelCase,
parseIntPagination,
normalizeHeaders,
} from '~/lib/utils/common_utils';
import pollIntervalQuery from './queries/poll_interval.query.graphql';
import environmentToRollbackQuery from './queries/environment_to_rollback.query.graphql';
2022-03-02 08:16:31 +05:30
import environmentToStopQuery from './queries/environment_to_stop.query.graphql';
2022-01-26 12:08:38 +05:30
import environmentToDeleteQuery from './queries/environment_to_delete.query.graphql';
2022-04-04 11:22:00 +05:30
import environmentToChangeCanaryQuery from './queries/environment_to_change_canary.query.graphql';
2022-05-07 20:08:51 +05:30
import isEnvironmentStoppingQuery from './queries/is_environment_stopping.query.graphql';
2022-01-26 12:08:38 +05:30
import pageInfoQuery from './queries/page_info.query.graphql';
const buildErrors = (errors = []) => ({
errors,
__typename: 'LocalEnvironmentErrors',
});
2021-12-11 22:18:48 +05:30
const mapNestedEnvironment = (env) => ({
...convertObjectPropsToCamelCase(env, { deep: true }),
__typename: 'NestedLocalEnvironment',
});
const mapEnvironment = (env) => ({
2022-06-21 17:19:12 +05:30
...convertObjectPropsToCamelCase(env, { deep: true }),
2021-12-11 22:18:48 +05:30
__typename: 'LocalEnvironment',
});
export const resolvers = (endpoint) => ({
Query: {
2022-11-25 23:54:43 +05:30
environmentApp(_context, { page, scope, search }, { cache }) {
return axios.get(endpoint, { params: { nested: true, page, scope, search } }).then((res) => {
2022-01-26 12:08:38 +05:30
const headers = normalizeHeaders(res.headers);
const interval = headers['POLL-INTERVAL'];
const pageInfo = { ...parseIntPagination(headers), __typename: 'LocalPageInfo' };
if (interval) {
cache.writeQuery({ query: pollIntervalQuery, data: { interval: parseFloat(interval) } });
} else {
cache.writeQuery({ query: pollIntervalQuery, data: { interval: undefined } });
}
cache.writeQuery({
query: pageInfoQuery,
data: { pageInfo },
});
return {
availableCount: res.data.available_count,
environments: res.data.environments.map(mapNestedEnvironment),
reviewApp: {
...convertObjectPropsToCamelCase(res.data.review_app),
__typename: 'ReviewApp',
},
stoppedCount: res.data.stopped_count,
__typename: 'LocalEnvironmentApp',
};
});
2021-12-11 22:18:48 +05:30
},
2022-11-25 23:54:43 +05:30
folder(_, { environment: { folderPath }, scope, search }) {
return axios.get(folderPath, { params: { scope, search, per_page: 3 } }).then((res) => ({
2021-12-11 22:18:48 +05:30
availableCount: res.data.available_count,
environments: res.data.environments.map(mapEnvironment),
stoppedCount: res.data.stopped_count,
__typename: 'LocalEnvironmentFolder',
}));
},
2022-01-26 12:08:38 +05:30
isLastDeployment(_, { environment }) {
2022-03-02 08:16:31 +05:30
return environment?.lastDeployment?.isLast;
2022-01-26 12:08:38 +05:30
},
2021-12-11 22:18:48 +05:30
},
2022-01-26 12:08:38 +05:30
Mutation: {
2022-05-07 20:08:51 +05:30
stopEnvironment(_, { environment }, { client }) {
client.writeQuery({
query: isEnvironmentStoppingQuery,
variables: { environment },
data: { isEnvironmentStopping: true },
});
2022-01-26 12:08:38 +05:30
return axios
.post(environment.stopPath)
.then(() => buildErrors())
.catch(() => {
2022-05-07 20:08:51 +05:30
client.writeQuery({
query: isEnvironmentStoppingQuery,
variables: { environment },
data: { isEnvironmentStopping: false },
});
2022-01-26 12:08:38 +05:30
return buildErrors([
s__('Environments|An error occurred while stopping the environment, please try again'),
]);
});
2021-12-11 22:18:48 +05:30
},
deleteEnvironment(_, { environment: { deletePath } }) {
2022-01-26 12:08:38 +05:30
return axios
.delete(deletePath)
.then(() => buildErrors())
.catch(() =>
buildErrors([
s__(
'Environments|An error occurred while deleting the environment. Check if the environment stopped; if not, stop it and try again.',
),
]),
);
},
rollbackEnvironment(_, { environment, isLastDeployment }) {
return axios
.post(environment?.retryUrl)
.then(() => buildErrors())
.catch(() => {
buildErrors([
isLastDeployment
? s__(
'Environments|An error occurred while re-deploying the environment, please try again',
)
: s__(
'Environments|An error occurred while rolling back the environment, please try again',
),
]);
});
},
2022-03-02 08:16:31 +05:30
setEnvironmentToStop(_, { environment }, { client }) {
client.writeQuery({
query: environmentToStopQuery,
data: { environmentToStop: environment },
});
},
action(_, { action: { playPath } }) {
return axios
.post(playPath)
.then(() => buildErrors())
.catch(() =>
buildErrors([s__('Environments|An error occurred while making the request.')]),
);
},
2022-01-26 12:08:38 +05:30
setEnvironmentToDelete(_, { environment }, { client }) {
client.writeQuery({
query: environmentToDeleteQuery,
data: { environmentToDelete: environment },
});
2021-12-11 22:18:48 +05:30
},
2022-01-26 12:08:38 +05:30
setEnvironmentToRollback(_, { environment }, { client }) {
client.writeQuery({
query: environmentToRollbackQuery,
data: { environmentToRollback: environment },
});
2021-12-11 22:18:48 +05:30
},
2022-04-04 11:22:00 +05:30
setEnvironmentToChangeCanary(_, { environment, weight }, { client }) {
client.writeQuery({
query: environmentToChangeCanaryQuery,
data: { environmentToChangeCanary: environment, weight },
});
},
cancelAutoStop(_, { autoStopUrl }) {
2022-01-26 12:08:38 +05:30
return axios
2022-04-04 11:22:00 +05:30
.post(autoStopUrl)
2022-01-26 12:08:38 +05:30
.then(() => buildErrors())
.catch((err) =>
buildErrors([
err?.response?.data?.message ||
s__('Environments|An error occurred while canceling the auto stop, please try again'),
]),
);
2021-12-11 22:18:48 +05:30
},
},
});