From 5e79276b53f61cbd727411ed33e71d5b6fa5ca54 Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Wed, 23 May 2018 14:22:38 +0100 Subject: improve API calls by calling internal API to get data render job items (needs improvements to components) --- app/assets/javascripts/api.js | 6 +- .../javascripts/ide/components/pipelines/jobs.vue | 50 ++++++++++++++-- .../ide/stores/modules/pipelines/actions.js | 46 +++++++++------ .../ide/stores/modules/pipelines/getters.js | 6 +- .../ide/stores/modules/pipelines/mutation_types.js | 4 ++ .../ide/stores/modules/pipelines/mutations.js | 67 +++++++++++++--------- .../javascripts/vue_shared/components/tabs/tab.vue | 3 + 7 files changed, 127 insertions(+), 55 deletions(-) (limited to 'app/assets/javascripts') diff --git a/app/assets/javascripts/api.js b/app/assets/javascripts/api.js index eb919241318..06fdc8fe65b 100644 --- a/app/assets/javascripts/api.js +++ b/app/assets/javascripts/api.js @@ -24,7 +24,7 @@ const Api = { branchSinglePath: '/api/:version/projects/:id/repository/branches/:branch', createBranchPath: '/api/:version/projects/:id/repository/branches', pipelinesPath: '/api/:version/projects/:id/pipelines', - pipelineJobsPath: '/api/:version/projects/:id/pipelines/:pipeline_id/jobs', + pipelineJobsPath: '/:project_path/pipelines/:id/builds.json', group(groupId, callback) { const url = Api.buildUrl(Api.groupPath).replace(':id', groupId); @@ -232,8 +232,8 @@ const Api = { pipelineJobs(projectPath, pipelineId, params = {}) { const url = Api.buildUrl(this.pipelineJobsPath) - .replace(':id', encodeURIComponent(projectPath)) - .replace(':pipeline_id', pipelineId); + .replace(':project_path', projectPath) + .replace(':id', pipelineId); return axios.get(url, { params }); }, diff --git a/app/assets/javascripts/ide/components/pipelines/jobs.vue b/app/assets/javascripts/ide/components/pipelines/jobs.vue index d69945b617c..a3a99ad457c 100644 --- a/app/assets/javascripts/ide/components/pipelines/jobs.vue +++ b/app/assets/javascripts/ide/components/pipelines/jobs.vue @@ -1,5 +1,7 @@ @@ -27,11 +32,44 @@ export default { - List all jobs here +
+
+
+ + {{ stage.title }} + + {{ stage.jobs.length }} + + +
+
+
+ + {{ job.name }} #{{ job.id }} +
+
+
+
List all failed jobs here diff --git a/app/assets/javascripts/ide/stores/modules/pipelines/actions.js b/app/assets/javascripts/ide/stores/modules/pipelines/actions.js index 07f7b201f2e..994edd74aef 100644 --- a/app/assets/javascripts/ide/stores/modules/pipelines/actions.js +++ b/app/assets/javascripts/ide/stores/modules/pipelines/actions.js @@ -1,3 +1,4 @@ +import axios from 'axios'; import { __ } from '../../../../locale'; import Api from '../../../../api'; import flash from '../../../../flash'; @@ -21,29 +22,40 @@ export const fetchLatestPipeline = ({ dispatch, rootState }, sha) => { .catch(() => dispatch('receiveLatestPipelineError')); }; -export const requestJobs = ({ commit }) => commit(types.REQUEST_JOBS); -export const receiveJobsError = ({ commit }) => { - flash(__('There was an error loading jobs')); - commit(types.RECEIVE_JOBS_ERROR); +export const requestStages = ({ commit }) => commit(types.REQUEST_STAGES); +export const receiveStagesError = ({ commit }) => { + flash(__('There was an error loading job stages')); + commit(types.RECEIVE_STAGES_ERROR); }; -export const receiveJobsSuccess = ({ commit }, data) => commit(types.RECEIVE_JOBS_SUCCESS, data); +export const receiveStagesSuccess = ({ commit }, data) => + commit(types.RECEIVE_STAGES_SUCCESS, data); + +export const fetchStages = ({ dispatch, state, rootState }) => { + dispatch('requestStages'); -export const fetchJobs = ({ dispatch, state, rootState }, page = '1') => { - dispatch('requestJobs'); + Api.pipelineJobs(rootState.currentProjectId, state.latestPipeline.id) + .then(({ data }) => dispatch('receiveStagesSuccess', data)) + .then(() => state.stages.forEach(stage => dispatch('fetchJobs', stage))) + .catch(() => dispatch('receiveStagesError')); +}; - Api.pipelineJobs(rootState.currentProjectId, state.latestPipeline.id, { - page, - }) - .then(({ data, headers }) => { - const nextPage = headers && headers['x-next-page']; +export const requestJobs = ({ commit }, id) => commit(types.REQUEST_JOBS, id); +export const receiveJobsError = ({ commit }, id) => { + flash(__('There was an error loading jobs')); + commit(types.RECEIVE_JOBS_ERROR, id); +}; +export const receiveJobsSuccess = ({ commit }, { id, data }) => + commit(types.RECEIVE_JOBS_SUCCESS, { id, data }); - dispatch('receiveJobsSuccess', data); +export const fetchJobs = ({ dispatch }, stage) => { + dispatch('requestJobs', stage.id); - if (nextPage) { - dispatch('fetchJobs', nextPage); - } + axios + .get(stage.dropdown_path) + .then(({ data }) => { + dispatch('receiveJobsSuccess', { id: stage.id, data }); }) - .catch(() => dispatch('receiveJobsError')); + .catch(() => dispatch('receiveJobsError', stage.id)); }; export default () => {}; diff --git a/app/assets/javascripts/ide/stores/modules/pipelines/getters.js b/app/assets/javascripts/ide/stores/modules/pipelines/getters.js index e1f55bcd933..99b4554a96e 100644 --- a/app/assets/javascripts/ide/stores/modules/pipelines/getters.js +++ b/app/assets/javascripts/ide/stores/modules/pipelines/getters.js @@ -1,9 +1,9 @@ export const hasLatestPipeline = state => !state.isLoadingPipeline && !!state.latestPipeline; -export const failedJobs = state => +export const failedJobsCount = state => state.stages.reduce( - (acc, stage) => acc.concat(stage.jobs.filter(job => job.status === 'failed')), - [], + (acc, stage) => acc + stage.jobs.filter(j => j.status.label === 'failed').length, + 0, ); export const jobsCount = state => state.stages.reduce((acc, stage) => acc + stage.jobs.length, 0); diff --git a/app/assets/javascripts/ide/stores/modules/pipelines/mutation_types.js b/app/assets/javascripts/ide/stores/modules/pipelines/mutation_types.js index 6b5701670a6..0911b8ee6fb 100644 --- a/app/assets/javascripts/ide/stores/modules/pipelines/mutation_types.js +++ b/app/assets/javascripts/ide/stores/modules/pipelines/mutation_types.js @@ -2,6 +2,10 @@ export const REQUEST_LATEST_PIPELINE = 'REQUEST_LATEST_PIPELINE'; export const RECEIVE_LASTEST_PIPELINE_ERROR = 'RECEIVE_LASTEST_PIPELINE_ERROR'; export const RECEIVE_LASTEST_PIPELINE_SUCCESS = 'RECEIVE_LASTEST_PIPELINE_SUCCESS'; +export const REQUEST_STAGES = 'REQUEST_STAGES'; +export const RECEIVE_STAGES_ERROR = 'RECEIVE_STAGES_ERROR'; +export const RECEIVE_STAGES_SUCCESS = 'RECEIVE_STAGES_SUCCESS'; + export const REQUEST_JOBS = 'REQUEST_JOBS'; export const RECEIVE_JOBS_ERROR = 'RECEIVE_JOBS_ERROR'; export const RECEIVE_JOBS_SUCCESS = 'RECEIVE_JOBS_SUCCESS'; diff --git a/app/assets/javascripts/ide/stores/modules/pipelines/mutations.js b/app/assets/javascripts/ide/stores/modules/pipelines/mutations.js index d28d9ca776d..7115f5e5386 100644 --- a/app/assets/javascripts/ide/stores/modules/pipelines/mutations.js +++ b/app/assets/javascripts/ide/stores/modules/pipelines/mutations.js @@ -18,37 +18,52 @@ export default { }; } }, - [types.REQUEST_JOBS](state) { + [types.REQUEST_STAGES](state) { state.isLoadingJobs = true; }, - [types.RECEIVE_JOBS_ERROR](state) { + [types.RECEIVE_STAGES_ERROR](state) { state.isLoadingJobs = false; }, - [types.RECEIVE_JOBS_SUCCESS](state, jobs) { + [types.RECEIVE_STAGES_SUCCESS](state, stages) { state.isLoadingJobs = false; - state.stages = jobs.reduce((acc, job) => { - let stage = acc.find(s => s.title === job.stage); - - if (!stage) { - stage = { - title: job.stage, - isCollapsed: false, - jobs: [], - }; - - acc.push(stage); - } - - stage.jobs = stage.jobs.concat({ - id: job.id, - name: job.name, - status: job.status, - stage: job.stage, - duration: job.duration, - }); - - return acc; - }, state.stages); + state.stages = stages.map((stage, i) => ({ + ...stage, + id: i, + isCollapsed: false, + isLoading: false, + jobs: [], + })); + }, + [types.REQUEST_JOBS](state, id) { + state.stages = state.stages.reduce( + (acc, stage) => + acc.concat({ + ...stage, + isLoading: id === stage.id ? true : stage.isLoading, + }), + [], + ); + }, + [types.RECEIVE_JOBS_ERROR](state, id) { + state.stages = state.stages.reduce( + (acc, stage) => + acc.concat({ + ...stage, + isLoading: id === stage.id ? false : stage.isLoading, + }), + [], + ); + }, + [types.RECEIVE_JOBS_SUCCESS](state, { id, data }) { + state.stages = state.stages.reduce( + (acc, stage) => + acc.concat({ + ...stage, + isLoading: id === stage.id ? false : stage.isLoading, + jobs: id === stage.id ? data.latest_statuses : stage.jobs, + }), + [], + ); }, }; diff --git a/app/assets/javascripts/vue_shared/components/tabs/tab.vue b/app/assets/javascripts/vue_shared/components/tabs/tab.vue index 2a35d6bc151..0ec7a0199fe 100644 --- a/app/assets/javascripts/vue_shared/components/tabs/tab.vue +++ b/app/assets/javascripts/vue_shared/components/tabs/tab.vue @@ -26,6 +26,9 @@ export default { created() { this.isTab = true; }, + updated() { + this.$parent.$forceUpdate(); + }, }; -- cgit v1.2.3