pullRequestLink.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import styled from '@emotion/styled';
  2. import Button from 'sentry/components/button';
  3. import ExternalLink from 'sentry/components/links/externalLink';
  4. import {IconBitbucket, IconGithub, IconGitlab} from 'sentry/icons';
  5. import space from 'sentry/styles/space';
  6. import {PullRequest, Repository} from 'sentry/types';
  7. function renderIcon(repo: Repository) {
  8. if (!repo.provider) {
  9. return null;
  10. }
  11. const {id} = repo.provider;
  12. const providerId = id.includes(':') ? id.split(':').pop() : id;
  13. switch (providerId) {
  14. case 'github':
  15. return <IconGithub size="xs" data-test-id="pull-request-github" />;
  16. case 'gitlab':
  17. return <IconGitlab size="xs" data-test-id="pull-request-gitlab" />;
  18. case 'bitbucket':
  19. return <IconBitbucket size="xs" />;
  20. default:
  21. return null;
  22. }
  23. }
  24. type Props = {
  25. pullRequest: PullRequest;
  26. repository: Repository;
  27. inline?: boolean;
  28. };
  29. function PullRequestLink({pullRequest, repository, inline}: Props) {
  30. const displayId = `${repository.name} #${pullRequest.id}: ${pullRequest.title}`;
  31. if (!pullRequest.externalUrl) {
  32. return <span>{displayId}</span>;
  33. }
  34. return !inline ? (
  35. <Button
  36. external
  37. href={pullRequest.externalUrl}
  38. size="sm"
  39. icon={renderIcon(repository)}
  40. >
  41. {displayId}
  42. </Button>
  43. ) : (
  44. <ExternalPullLink href={pullRequest.externalUrl}>
  45. {renderIcon(repository)}
  46. {displayId}
  47. </ExternalPullLink>
  48. );
  49. }
  50. const ExternalPullLink = styled(ExternalLink)`
  51. display: inline-flex;
  52. align-items: center;
  53. gap: ${space(0.5)};
  54. `;
  55. export default PullRequestLink;