go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/monitoring/components/disable_button/disable_button.tsx (about)

     1  // Copyright 2024 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import CheckIcon from '@mui/icons-material/Check';
    16  import ContentCopyIcon from '@mui/icons-material/ContentCopy';
    17  import { Alert, Button } from '@mui/material';
    18  import { useState } from 'react';
    19  
    20  import { Bug } from '@/monitoring/util/server_json';
    21  
    22  interface DisableTestButtonProps {
    23    failureBBID: string;
    24    testID: string;
    25    bug?: Bug;
    26  }
    27  
    28  export const DisableTestButton = ({
    29    bug,
    30    failureBBID,
    31    testID,
    32  }: DisableTestButtonProps) => {
    33    const [wasCopied, setWasCopied] = useState(false);
    34    const [error, setError] = useState('');
    35    const handleClick = () => {
    36      let command = 'tools/disable_tests/disable';
    37      if (bug) {
    38        // Add the bug ID if present. Only do it if there's exactly one. If there
    39        // are more than one we don't know which one to use.
    40        command += ' -b ' + bug.number;
    41      }
    42  
    43      command += ` ${failureBBID} '${testID}'`;
    44  
    45      navigator.clipboard.writeText(command).catch(function (err) {
    46        setError(err);
    47      });
    48  
    49      setWasCopied(true);
    50      setTimeout(() => setWasCopied(false), 1500);
    51    };
    52    return (
    53      <>
    54        <Button
    55          size="small"
    56          id="copy-disable-command-button"
    57          title="Copy a command to run from the root of a chromium/src checkout to disable this test."
    58          onClick={handleClick}
    59          startIcon={wasCopied ? <CheckIcon /> : <ContentCopyIcon />}
    60        >
    61          {wasCopied ? 'Copied' : 'Disable'}
    62        </Button>
    63        {error ? (
    64          <Alert severity="error" onClose={() => setError('')}>
    65            {error}
    66          </Alert>
    67        ) : null}
    68      </>
    69    );
    70  };