Skip to content

feat(Codecov): add storage solution #90693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {useEffect} from 'react';

import type {CodecovContextData} from 'sentry/components/codecov/context/codecovContext';
import {CodecovContext} from 'sentry/components/codecov/context/codecovContext';
import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
import {useLocation} from 'sentry/utils/useLocation';
import useOrganization from 'sentry/utils/useOrganization';

type CodecovQueryParamsProviderProps = {
children?: NonNullable<React.ReactNode>;
};

export default function CodecovQueryParamsProvider({
children,
}: CodecovQueryParamsProviderProps) {
const organization = useOrganization();

const location = useLocation();
const [localStorageState, setLocalStorageState] = useLocalStorageState(
`codecov-selection:${organization.slug}`,
{}
);

useEffect(() => {
const validEntries = {
repository: location.query.repository,
integratedOrg: location.query.integratedOrg,
branch: location.query.branch,
codecovPeriod: location.query.codecovPeriod,
};

for (const [key, value] of Object.entries(validEntries)) {
if (!value || typeof value !== 'string') {
delete validEntries[key as keyof CodecovContextData];
}
}

setLocalStorageState(prev => ({
...prev,
...validEntries,
}));
}, [setLocalStorageState, location.query]);

// Repository, org and branch default to null as its value to the option not being selected.
// These only represent the unselected values and shouldn't be used when fetching backend data.
const params: CodecovContextData = {
repository:
typeof location.query.repository === 'string'
? decodeURIComponent(location.query.repository)
: 'repository' in localStorageState
? (localStorageState.repository as string)
: null,
integratedOrg:
typeof location.query.integratedOrg === 'string'
? decodeURIComponent(location.query.integratedOrg)
: 'integratedOrg' in localStorageState
? (localStorageState.integratedOrg as string)
: null,
branch:
typeof location.query.branch === 'string'
? decodeURIComponent(location.query.branch)
: 'branch' in localStorageState
? (localStorageState.branch as string)
: null,
codecovPeriod:
typeof location.query.codecovPeriod === 'string'
? decodeURIComponent(location.query.codecovPeriod)
: 'codecovPeriod' in localStorageState
? (localStorageState.codecovPeriod as string)
: '24h',
};

return <CodecovContext.Provider value={params}>{children}</CodecovContext.Provider>;
}
17 changes: 17 additions & 0 deletions static/app/components/codecov/context/codecovContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {createContext, useContext} from 'react';

export type CodecovContextData = {
branch: string | null;
codecovPeriod: string | null;
integratedOrg: string | null;
repository: string | null;
};

export const CodecovContext = createContext<CodecovContextData | undefined>(undefined);

export function useCodecovContext() {
const context = useContext(CodecovContext);
if (context === undefined)
throw new Error('useCodecovContext was called outside of CodecovProvider');
return context;
}
Loading