|
1 | 1 | import * as React from 'react'; |
2 | | -import gitUrlParse, { GitUrl } from 'git-url-parse'; |
3 | 2 | import { createRevisionURL } from 'src/gitops/utils/gitops'; |
4 | 3 |
|
5 | 4 | import ExternalLink from '../utils/components/ExternalLink/ExternalLink'; |
| 5 | +import { isSHA, revisionUrl } from '@gitops/utils/urls'; |
6 | 6 |
|
7 | 7 | interface RevisionProps { |
8 | 8 | repoURL: string; |
@@ -42,53 +42,4 @@ const Revision: React.FC<RevisionProps> = ({ repoURL, revision, helm, revisionEx |
42 | 42 | } |
43 | 43 | }; |
44 | 44 |
|
45 | | -export const isSHA = (revision: string) => { |
46 | | - if (revision.startsWith('sha256:')) { |
47 | | - const hashOnly = revision.replace('sha256:', ''); |
48 | | - return hashOnly.match(/^[a-f0-9]{8,69}$/) !== null; |
49 | | - } |
50 | | - // https://stackoverflow.com/questions/468370/a-regex-to-match-a-sha1 |
51 | | - return revision.match(/^[a-f0-9]{5,40}$/) !== null; |
52 | | -}; |
53 | | - |
54 | | -function supportedSource(parsed: GitUrl): boolean { |
55 | | - return ( |
56 | | - parsed.resource.startsWith('github') || |
57 | | - ['gitlab.com', 'bitbucket.org'].indexOf(parsed.source) >= 0 |
58 | | - ); |
59 | | -} |
60 | | - |
61 | | -function protocol(proto: string): string { |
62 | | - return proto === 'ssh' ? 'https' : proto; |
63 | | -} |
64 | | - |
65 | | -function revisionUrl(url: string, revision: string, forPath: boolean): string { |
66 | | - let parsed; |
67 | | - try { |
68 | | - parsed = gitUrlParse(url); |
69 | | - } catch { |
70 | | - return null; |
71 | | - } |
72 | | - let urlSubPath = isSHA(revision) ? 'commit' : 'tree'; |
73 | | - |
74 | | - if (url.indexOf('bitbucket') >= 0) { |
75 | | - // The reason for the condition of 'forPath' is that when we build nested path, we need to use 'src' |
76 | | - urlSubPath = isSHA(revision) && !forPath ? 'commits' : 'src'; |
77 | | - } |
78 | | - |
79 | | - // Gitlab changed the way urls to commit look like |
80 | | - // Ref: https://docs.gitlab.com/ee/update/deprecations.html#legacy-urls-replaced-or-removed |
81 | | - if (parsed.source === 'gitlab.com') { |
82 | | - urlSubPath = '-/' + urlSubPath; |
83 | | - } |
84 | | - |
85 | | - if (!supportedSource(parsed)) { |
86 | | - return null; |
87 | | - } |
88 | | - |
89 | | - return `${protocol(parsed.protocol)}://${parsed.resource}/${parsed.owner}/${ |
90 | | - parsed.name |
91 | | - }/${urlSubPath}/${revision || 'HEAD'}`; |
92 | | -} |
93 | | - |
94 | 45 | export default Revision; |
0 commit comments