-
-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This PR sends segments to CR drafts when you use the `add to draft` button. It also adds the logic for which environment to pick. Segments sent to the API are added to drafts, but not displayed in the UI yet. CRs with only segment changes are also not listed in the table. This'll be covered in upcoming work.
- Loading branch information
1 parent
7a32eac
commit 452f394
Showing
4 changed files
with
121 additions
and
22 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
66 changes: 66 additions & 0 deletions
66
frontend/src/hooks/useHighestPermissionChangeRequestEnvironment.test.ts
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,66 @@ | ||
import { getHighestChangeRequestEnv } from './useHighestPermissionChangeRequestEnvironment'; | ||
|
||
describe('Get the right change request env', () => { | ||
it('gets a production env if present', () => { | ||
const data = [ | ||
{ | ||
environment: 'x', | ||
type: 'development', | ||
changeRequestEnabled: true, | ||
requiredApprovals: 1, | ||
}, | ||
{ | ||
environment: 'y', | ||
type: 'production', | ||
changeRequestEnabled: true, | ||
requiredApprovals: 1, | ||
}, | ||
]; | ||
|
||
const result = getHighestChangeRequestEnv(data)(); | ||
|
||
expect(result).toBe('y'); | ||
}); | ||
|
||
it('gets a non-production env if no production envs have change requests enabled', () => { | ||
const data = [ | ||
{ | ||
environment: 'x', | ||
type: 'development', | ||
changeRequestEnabled: true, | ||
requiredApprovals: 1, | ||
}, | ||
{ | ||
environment: 'y', | ||
type: 'production', | ||
changeRequestEnabled: false, | ||
requiredApprovals: 1, | ||
}, | ||
]; | ||
|
||
const result = getHighestChangeRequestEnv(data)(); | ||
|
||
expect(result).toBe('x'); | ||
}); | ||
|
||
it('returns undefined if no envs have change requests enabled', () => { | ||
const data = [ | ||
{ | ||
environment: 'x', | ||
type: 'development', | ||
changeRequestEnabled: false, | ||
requiredApprovals: 1, | ||
}, | ||
{ | ||
environment: 'y', | ||
type: 'production', | ||
changeRequestEnabled: false, | ||
requiredApprovals: 1, | ||
}, | ||
]; | ||
|
||
const result = getHighestChangeRequestEnv(data)(); | ||
|
||
expect(result).toBe(undefined); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
frontend/src/hooks/useHighestPermissionChangeRequestEnvironment.ts
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,24 @@ | ||
import { IChangeRequestEnvironmentConfig } from 'component/changeRequest/changeRequest.types'; | ||
import React from 'react'; | ||
import { useChangeRequestConfig } from './api/getters/useChangeRequestConfig/useChangeRequestConfig'; | ||
|
||
export const getHighestChangeRequestEnv = | ||
(data: IChangeRequestEnvironmentConfig[]) => (): string | undefined => { | ||
const changeRequestEnvs = data.filter(env => env.changeRequestEnabled); | ||
|
||
const env = | ||
changeRequestEnvs.find(env => env.type === 'production') ?? | ||
changeRequestEnvs[0]; | ||
|
||
return env?.environment; | ||
}; | ||
|
||
export const useHighestPermissionChangeRequestEnvironment = ( | ||
projectId?: string | ||
) => { | ||
const { data } = useChangeRequestConfig(projectId || ''); | ||
|
||
return React.useCallback(getHighestChangeRequestEnv(data), [ | ||
JSON.stringify(data), | ||
]); | ||
}; |