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

Bug 1830181: Hide 'Start Last Run' button on topology overview page when no PLR present #5258

Merged
merged 1 commit into from May 5, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -26,7 +26,7 @@ export const useUserLabelForManualStart = (): LabelMap => {
export const usePipelineRunWithUserLabel = (plr: PipelineRun): PipelineRun => {
const labels = useUserLabelForManualStart();

return mergeLabelsWithResource(labels, plr);
return plr && mergeLabelsWithResource(labels, plr);
};

export const useMenuActionsWithUserLabel = (menuActions: KebabAction[]): KebabAction[] => {
Expand Down
Expand Up @@ -52,7 +52,7 @@ const PipelinesOverview: React.FC<PipelinesOverviewProps> = ({
/>
</FlexItem>
<FlexItem>
<TriggerLastRunButton pipelineRuns={pipelineRuns} />
<TriggerLastRunButton pipelineRuns={pipelineRuns} namespace={namespace} />
</FlexItem>
</Flex>
</li>
Expand Down
@@ -1,30 +1,48 @@
import * as React from 'react';
import * as _ from 'lodash';
import { connect } from 'react-redux';
import { Button } from '@patternfly/react-core';
import { impersonateStateToProps } from '@console/internal/reducers/ui';
import { useAccessReview } from '@console/internal/components/utils';
import { Button } from '@patternfly/react-core';
import { AccessReviewResourceAttributes } from '@console/internal/module/k8s';
import { rerunPipelineAndStay } from '../../../utils/pipeline-actions';
import { PipelineModel } from '../../../models';
import { PipelineRunModel } from '../../../models';
import { usePipelineRunWithUserLabel } from '../../pipelineruns/triggered-by';
import { getLatestRun, PipelineRun } from '../../../utils/pipeline-augment';

type TriggerLastRunButtonProps = {
pipelineRuns: PipelineRun[];
namespace: string;
impersonate?;
};

const TriggerLastRunButton: React.FC<TriggerLastRunButtonProps> = ({
pipelineRuns,
namespace,
impersonate,
}) => {
const latestRun = usePipelineRunWithUserLabel(
Copy link
Contributor

Choose a reason for hiding this comment

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

While debugging the solution... I noticed a bug I accidentally made, can correct at the same time here?

In pipelineruns/triggered-by/hooks.ts:

// Today
  return mergeLabelsWithResource(labels, plr);
// Suggested Change
  return plr && mergeLabelsWithResource(labels, plr);

That way it doesn't merge a label onto nothing creating an invalid k8s object.

getLatestRun({ data: pipelineRuns }, 'startTimestamp'),
);
const { label, callback, accessReview } = rerunPipelineAndStay(PipelineModel, latestRun);
const { label, callback, accessReview: utilityAccessReview } = rerunPipelineAndStay(
PipelineRunModel,
latestRun,
);
const defaultAccessReview: AccessReviewResourceAttributes = {
group: PipelineRunModel.apiGroup,
resource: PipelineRunModel.plural,
namespace,
verb: 'create',
};
const accessReview = _.isEmpty(utilityAccessReview) ? defaultAccessReview : utilityAccessReview;
const isAllowed = useAccessReview(accessReview, impersonate);
return (
isAllowed && (
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also change the button logic to be more deterministic?

<Button variant="secondary" onClick={callback} isDisabled={pipelineRuns.length === 0 && !callback}>

Making sure we have a callback to use before enabling the button will help if there is ever an issue with the callback, we don't have a button enabled without a callback.

EDIT: Updated the logic, thanks for catching the mistake

<Button variant="secondary" onClick={callback} isDisabled={pipelineRuns.length === 0}>
<Button
variant="secondary"
onClick={callback}
isDisabled={pipelineRuns.length === 0 && !callback}
>
{label}
</Button>
)
Expand Down