Skip to content

Commit e59b3c6

Browse files
authored
feat: add save and publish pipeline (#5827)
1 parent de61b8b commit e59b3c6

1 file changed

Lines changed: 66 additions & 22 deletions

File tree

frontend/web/components/release-pipelines/CreateReleasePipeline.tsx

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Icon from 'components/Icon'
99
import {
1010
useCreateReleasePipelineMutation,
1111
useGetReleasePipelineQuery,
12+
usePublishReleasePipelineMutation,
1213
useUpdateReleasePipelineMutation,
1314
} from 'common/services/useReleasePipelines'
1415
import { useHistory, useParams } from 'react-router-dom'
@@ -21,6 +22,7 @@ import { useRouteContext } from 'components/providers/RouteContext'
2122
import PlanBasedAccess from 'components/PlanBasedAccess'
2223
import { NEW_PIPELINE_STAGE, NEW_PIPELINE_STAGE_ACTION_TYPE } from './constants'
2324
import { StageActionType } from 'common/types/responses'
25+
import ButtonDropdown from 'components/base/forms/ButtonDropdown'
2426

2527
type CreateReleasePipelineParams = {
2628
id?: string
@@ -62,6 +64,8 @@ function CreateReleasePipeline() {
6264
},
6365
] = useUpdateReleasePipelineMutation()
6466

67+
const [publishReleasePipeline] = usePublishReleasePipelineMutation()
68+
6569
const [pipelineData, setPipelineData] = useState<ReleasePipelineRequest>({
6670
name: '',
6771
project: Number(projectId),
@@ -80,17 +84,32 @@ function CreateReleasePipeline() {
8084
[history, projectId],
8185
)
8286

87+
const [isSavingAndPublishing, setIsSavingAndPublishing] = useState(false)
88+
8389
useEffect(() => {
90+
if (isSavingAndPublishing) {
91+
return
92+
}
93+
8494
if (isCreatingPipelineSuccess) {
8595
return handleSuccess()
8696
}
8797

8898
if (isUpdatingPipelineSuccess) {
8999
return handleSuccess(true)
90100
}
91-
}, [isCreatingPipelineSuccess, isUpdatingPipelineSuccess, handleSuccess])
101+
}, [
102+
isCreatingPipelineSuccess,
103+
isUpdatingPipelineSuccess,
104+
handleSuccess,
105+
isSavingAndPublishing,
106+
])
92107

93108
useEffect(() => {
109+
if (isSavingAndPublishing) {
110+
return
111+
}
112+
94113
if (isCreatingPipelineError) {
95114
toast('Error creating release pipeline', 'danger')
96115
}
@@ -106,6 +125,7 @@ function CreateReleasePipeline() {
106125
createPipelineError,
107126
isUpdatingPipelineError,
108127
updatePipelineError,
128+
isSavingAndPublishing,
109129
])
110130

111131
useEffect(() => {
@@ -158,27 +178,46 @@ function CreateReleasePipeline() {
158178
return pipelineData.stages.every(validateStage)
159179
}
160180

161-
const handleSave = () => {
162-
createReleasePipeline({
181+
const handleSubmit = async (id?: number) => {
182+
if (id) {
183+
return await updateReleasePipeline({
184+
id,
185+
name: pipelineData.name,
186+
project: Number(projectId),
187+
stages: pipelineData.stages.map((stage, index) => ({
188+
...stage,
189+
order: index,
190+
})),
191+
}).unwrap()
192+
}
193+
194+
return await createReleasePipeline({
163195
name: pipelineData.name,
164196
project: Number(projectId),
165197
stages: pipelineData.stages.map((stage, index) => ({
166198
...stage,
167199
order: index,
168200
})),
169-
})
201+
}).unwrap()
170202
}
171203

172-
const handleUpdate = (id: number) => {
173-
updateReleasePipeline({
174-
id,
175-
name: pipelineData.name,
176-
project: Number(projectId),
177-
stages: pipelineData.stages.map((stage, index) => ({
178-
...stage,
179-
order: index,
180-
})),
181-
})
204+
const handleSubmitAndPublish = async (id?: number) => {
205+
try {
206+
setIsSavingAndPublishing(true)
207+
const response = await handleSubmit(id)
208+
await publishReleasePipeline({
209+
pipelineId: response.id,
210+
projectId: Number(projectId),
211+
})
212+
toast(`Release pipeline published successfully`)
213+
} catch (error) {
214+
toast(
215+
`Error ${id ? 'updating' : 'creating'} and publishing pipeline`,
216+
'danger',
217+
)
218+
} finally {
219+
setIsSavingAndPublishing(false)
220+
}
182221
}
183222

184223
const handleRemoveStage = (index: number) => {
@@ -188,6 +227,7 @@ function CreateReleasePipeline() {
188227

189228
const isSaveDisabled =
190229
!validatePipelineData() || isCreatingPipeline || isUpdatingPipeline
230+
191231
const saveButtonText = existingPipeline?.id ? 'Update' : 'Save'
192232

193233
if (isLoadingExistingPipeline) {
@@ -246,16 +286,20 @@ function CreateReleasePipeline() {
246286
</Row>
247287
}
248288
cta={
249-
<Button
289+
<ButtonDropdown
290+
type='button'
291+
data-test='create-update-release-pipeline-btn'
250292
disabled={isSaveDisabled}
251-
onClick={() =>
252-
existingPipeline?.id
253-
? handleUpdate(existingPipeline.id)
254-
: handleSave()
255-
}
293+
dropdownItems={[
294+
{
295+
label: `${saveButtonText} & Publish`,
296+
onClick: () => handleSubmitAndPublish(existingPipeline?.id),
297+
},
298+
]}
299+
onClick={() => handleSubmit(existingPipeline?.id)}
256300
>
257-
{saveButtonText}
258-
</Button>
301+
{saveButtonText} as Draft
302+
</ButtonDropdown>
259303
}
260304
/>
261305
<div className='release-pipeline-container px-2 overflow-auto'>

0 commit comments

Comments
 (0)