-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
show commit status in commit list row
- Loading branch information
Mohammed Saud
committed
Jan 4, 2023
1 parent
0f1e378
commit 8aca367
Showing
8 changed files
with
123 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useK8sWatchResource } from '@openshift/dynamic-plugin-sdk-utils'; | ||
import '@testing-library/jest-dom'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { pipelineWithCommits } from '../__data__/pipeline-with-commits'; | ||
import { useCommitStatus } from '../commit-status'; | ||
|
||
jest.mock('@openshift/dynamic-plugin-sdk-utils', () => ({ | ||
useK8sWatchResource: jest.fn(), | ||
})); | ||
|
||
const watchResourceMock = useK8sWatchResource as jest.Mock; | ||
|
||
describe('useCommitStatus', () => { | ||
it('returns empty status if pipelineruns are not loaded', () => { | ||
watchResourceMock.mockReturnValue([null, false]); | ||
const { result } = renderHook(() => useCommitStatus('app', 'commit')); | ||
expect(result.current).toEqual(['-', false, undefined]); | ||
}); | ||
|
||
it('returns empty status if pipelineruns for given commit are not found', () => { | ||
watchResourceMock.mockReturnValue([pipelineWithCommits, true]); | ||
const { result } = renderHook(() => useCommitStatus('app', 'commit123')); | ||
expect(result.current).toEqual(['-', true, undefined]); | ||
}); | ||
|
||
it('returns correct status if pipelineruns are loaded', () => { | ||
watchResourceMock.mockReturnValue([pipelineWithCommits, true]); | ||
const { result } = renderHook(() => useCommitStatus('purple-mermaid-app', 'commit14rt')); | ||
expect(result.current).toEqual(['Succeeded', true, undefined]); | ||
}); | ||
|
||
it('returns status from the latest started pipelinerun', () => { | ||
watchResourceMock.mockReturnValue([pipelineWithCommits, true]); | ||
const { result } = renderHook(() => useCommitStatus('purple-mermaid-app', 'commit123')); | ||
expect(result.current).toEqual(['Failed', true, undefined]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import { useK8sWatchResource } from '@openshift/dynamic-plugin-sdk-utils'; | ||
import { PipelineRunLabel } from '../../consts/pipelinerun'; | ||
import { PipelineRunGroupVersionKind } from '../../models'; | ||
import { pipelineRunFilterReducer } from '../../shared'; | ||
import { PipelineRunKind } from '../../types'; | ||
import { statuses } from '../../utils/commits-utils'; | ||
import { useNamespace } from '../../utils/namespace-context-utils'; | ||
|
||
export const useCommitStatus = ( | ||
application: string, | ||
commit: string, | ||
): [string, boolean, unknown] => { | ||
const namespace = useNamespace(); | ||
const [pipelineruns, loaded, loadErr] = useK8sWatchResource<PipelineRunKind[]>({ | ||
groupVersionKind: PipelineRunGroupVersionKind, | ||
isList: true, | ||
namespace, | ||
}); | ||
|
||
const plrsForCommit = React.useMemo( | ||
() => | ||
pipelineruns | ||
?.filter( | ||
(plr) => | ||
plr.metadata.labels[PipelineRunLabel.APPLICATION] === application && | ||
plr.metadata.labels[PipelineRunLabel.COMMIT_LABEL] === commit, | ||
) | ||
.sort((a, b) => parseInt(a.status?.startTime, 10) - parseInt(b.status?.startTime, 10)), | ||
[application, commit, pipelineruns], | ||
); | ||
|
||
const commitStatus = React.useMemo(() => { | ||
if (!loaded || loadErr) { | ||
return '-'; | ||
} | ||
|
||
const plrStatus = pipelineRunFilterReducer(plrsForCommit[plrsForCommit.length - 1]); | ||
if (statuses.includes(plrStatus)) { | ||
return plrStatus; | ||
} | ||
return '-'; | ||
}, [loaded, loadErr, plrsForCommit]); | ||
|
||
return [commitStatus, loaded, loadErr]; | ||
}; |