function DisableIssue()

in torchci/components/JobLinks.tsx [60:121]


function DisableIssue({ job }: { job: JobData }) {
  const hasFailureClassification = job.failureLine != null;
  const swrKey = hasFailureClassification ? "/api/issue/skipped" : null;
  const { data } = useSWR(swrKey, fetcher, {
    // Set a 60s cache for the request, so that lots of tooltip hovers don't
    // spam the backend. Since actually mutating the state (through filing a
    // disable issue) is a pretty heavy operation, 60s of staleness is fine.
    dedupingInterval: 60 * 1000,
    refreshInterval: 60 * 1000, // refresh every minute
  });

  // Null states. Don't show an issue disable link if:
  // - We don't have a failure classification
  if (!hasFailureClassification) {
    return null;
  }
  // - The failure classification is not a python unittest failure.
  const match = job.failureLine!.match(testFailureRe);
  if (match === null) {
    return null;
  }
  // - If we don't yet have any data, show a loading state.
  if (data === undefined) {
    return <span>{" | "} checking for disable issues.</span>;
  }

  // At this point, we should show something. Search the existing disable issues
  // for a matching one.
  const issueTitle = `DISABLED ${match[1]}`;
  const issues: IssueData[] = data.issues;
  let issueLink;
  let linkText;
  let buttonStyle;
  const matchingIssues = issues.filter((issue) => issue.title === issueTitle);

  if (matchingIssues.length !== 0) {
    // There is a matching issue, show that in the tooltip box.
    const matchingIssue = matchingIssues[0];
    if (matchingIssue.state === "open") {
      linkText = "Test is disabled";
    } else {
      buttonStyle = styles.closedDisableIssueButton;
      linkText = "Previously disabled";
    }
    issueLink = matchingIssues[0].html_url;
  } else {
    // No matching issue, show a link to create one.
    const issueBody = formatIssueBody(job.failureCaptures!);
    linkText = "Disable test";
    issueLink = `https://github.com/pytorch/pytorch/issues/new?title=${issueTitle}&body=${issueBody}`;
    buttonStyle = styles.disableTestButton;
  }

  return (
    <span>
      {" | "}
      <a target="_blank" rel="noreferrer" href={issueLink}>
        <button className={buttonStyle}>{linkText}</button>
      </a>
    </span>
  );
}