Skip to content
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

Namespace detail - fix Sign all #3652

Merged
merged 6 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGES/2308.bug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Namespace detail: sign all collections only signs current repo now
3 changes: 2 additions & 1 deletion src/api/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export function findDistroBasePathByRepo(distributions, repository) {
const distro = distributions.find(
(distro) => distro.name === repository.name,
);
return distro ? distro.base_path : distro[0].base_path;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh :D, sorry for this 🤦


return distro ? distro.base_path : distributions[0].base_path;
}

function filterContents(contents) {
Expand Down
42 changes: 30 additions & 12 deletions src/api/sign-collections.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { t } from '@lingui/macro';
import {
AnsibleDistributionAPI,
AnsibleRepositoryAPI,
CollectionVersionSearch,
findDistroBasePathByRepo,
} from 'src/api';
import { HubAPI } from './hub';

interface SignNamespace {
signing_service?: string;
repository: CollectionVersionSearch['repository'];
repository?: CollectionVersionSearch['repository'];
repository_name?: string;
namespace: string;
}

Expand All @@ -24,19 +26,35 @@ type SignProps = SignNamespace | SignCollection | SignVersion;
class API extends HubAPI {
apiPath = this.getUIPath('collection_signing/');

async sign(data: SignProps) {
const { repository, ...args } = data;
const distros = await AnsibleDistributionAPI.list({
repository: repository.pulp_href,
});
async sign({ repository, repository_name: name, ...args }: SignProps) {
if (!repository && name) {
repository = (await AnsibleRepositoryAPI.list({ name }))?.data
?.results?.[0];

const distroBasePath = findDistroBasePathByRepo(
distros.data.results,
repository,
);
if (!repository) {
return Promise.reject({
response: { status: t`Failed to find repository ${name}` },
});
}
}

const distribution = (
await AnsibleDistributionAPI.list({
repository: repository?.pulp_href,
})
)?.data?.results?.[0];

if (!distribution) {
const name = repository.name;
return Promise.reject({
response: {
status: t`Failed to find a distribution for repository ${name}`,
},
});
}

const updatedData = {
distro_base_path: distroBasePath,
distro_base_path: distribution.base_path,
...args,
};

Expand Down
10 changes: 5 additions & 5 deletions src/components/collection-list/collection-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ export const CollectionFilter = (props: IProps) => {
<ToolbarItem>
<AppliedFilters
niceNames={{
is_signed: t`sign state`,
tags: t`tags`,
keywords: t`keywords`,
repository_name: t`repository`,
namespace: t`namespace`,
is_signed: t`Sign state`,
tags: t`Tags`,
keywords: t`Keywords`,
repository_name: t`Repository`,
namespace: t`Namespace`,
}}
niceValues={{
is_signed: {
Expand Down
84 changes: 49 additions & 35 deletions src/containers/namespace-detail/namespace-detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
]);

params['namespace'] = props.routeParams.namespace;
if (props.routeParams.repo && !params['repository_name']) {
params['repository_name'] = props.routeParams.repo;
}

this.state = {
canSign: false,
Expand Down Expand Up @@ -147,19 +150,29 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
}

componentDidUpdate(prevProps) {
if (prevProps.location.search !== this.props.location.search) {
const params = ParamHelper.parseParamString(this.props.location.search, [
'page',
'page_size',
]);
const params = ParamHelper.parseParamString(this.props.location.search, [
'page',
'page_size',
]);

if (prevProps.location.search !== this.props.location.search) {
params['namespace'] = this.props.routeParams.namespace;

this.setState({
params,
group: this.filterGroup(params['group'], this.state.namespace.groups),
});
}

if (
prevProps.routeParams.repo !== this.props.routeParams.repo &&
this.props.routeParams.repo &&
(!params['repository_name'] ||
params['repository_name'] === prevProps.routeParams.repo)
) {
params['repository_name'] = this.props.routeParams.repo;
this.setState({ params });
}
}

filterGroup(groupId, groups) {
Expand Down Expand Up @@ -275,7 +288,6 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {

const ignoredParams = [
'namespace',
'repository__name',
'page',
'page_size',
'sort',
Expand All @@ -295,6 +307,8 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
const tabParams = { ...params };
delete tabParams.group;

const repository = params['repository_name'] || null;

return (
<React.Fragment>
<AlertList alerts={alerts} closeAlert={(i) => this.closeAlert(i)} />
Expand Down Expand Up @@ -555,12 +569,8 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
<SignAllCertificatesModal
name={this.state.namespace.name}
isOpen={this.state.isOpenSignModal}
onSubmit={() => {
this.signAllCertificates(namespace);
}}
onCancel={() => {
this.setState({ isOpenSignModal: false });
}}
onSubmit={() => this.signAllCertificates(namespace, repository)}
onCancel={() => this.setState({ isOpenSignModal: false })}
/>
)}
</React.Fragment>
Expand Down Expand Up @@ -626,38 +636,32 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
);
}

private signAllCertificates(namespace: NamespaceType) {
// get the repository from first collection
// all collections are in same repo, so this should be fine.
const [collection] = this.state.collections;

private signAllCertificates(namespace: NamespaceType, repository: string) {
const errorAlert = (status: string | number = 500): AlertType => ({
variant: 'danger',
title: t`Failed to sign all collections.`,
description: t`API Error: ${status}`,
});

this.setState({
alerts: [
...this.state.alerts,
{
id: 'loading-signing',
variant: 'success',
title: t`Signing started for all collections in namespace "${namespace.name}".`,
},
],
isOpenSignModal: false,
});

const { name } = collection.collection_version;

SignCollectionAPI.sign({
signing_service: this.context.settings.GALAXY_COLLECTION_SIGNING_SERVICE,
repository: collection.repository,
repository_name: repository,
namespace: namespace.name,
collection: name,
})
.then((result) => {
// FIXME: use taskAlert
this.setState({
alerts: [
...this.state.alerts,
{
id: 'loading-signing',
variant: 'success',
title: t`Signing started for all collections in namespace "${namespace.name}".`,
},
],
isOpenSignModal: false,
});

waitForTask(result.data.task_id)
.then(() => {
this.load();
Expand All @@ -679,6 +683,7 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
// The request failed in the first place
this.setState({
alerts: [...this.state.alerts, errorAlert(error.response.status)],
isOpenSignModal: false,
});
});
}
Expand Down Expand Up @@ -762,6 +767,7 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
const { can_upload_signatures } = this.context.featureFlags;
const { ai_deny_index } = this.context.featureFlags;
const { hasPermission } = this.context;
const repository = this.state.params['repository_name'] || null;

const dropdownItems = [
<DropdownItem
Expand Down Expand Up @@ -819,15 +825,23 @@ export class NamespaceDetail extends React.Component<RouteProps, IState> {
/>,
canSign &&
!can_upload_signatures &&
this.state.collections.length >= 1 && (
(repository ? (
<DropdownItem
key='sign-collections'
data-cy='sign-all-collections-button'
onClick={() => this.setState({ isOpenSignModal: true })}
>
{t`Sign all collections in ${repository}`}
</DropdownItem>
) : (
<DropdownItem
key='sign-collections'
isDisabled
description={t`Please select a repository filter`}
>
{t`Sign all collections`}
</DropdownItem>
),
)),
ai_deny_index && (
<DropdownItem
key='wisdom-settings'
Expand Down