Skip to content

Commit 594908e

Browse files
committed
Remove duplicate and update isSHA, based on Atif's comments
Signed-off-by: Keith Chong <kykchong@redhat.com>
1 parent fd91f7d commit 594908e

File tree

2 files changed

+5
-50
lines changed

2 files changed

+5
-50
lines changed

src/gitops/Revision/Revision.tsx

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as React from 'react';
2-
import gitUrlParse, { GitUrl } from 'git-url-parse';
32
import { createRevisionURL } from 'src/gitops/utils/gitops';
43

54
import ExternalLink from '../utils/components/ExternalLink/ExternalLink';
5+
import { isSHA, revisionUrl } from '@gitops/utils/urls';
66

77
interface RevisionProps {
88
repoURL: string;
@@ -42,53 +42,4 @@ const Revision: React.FC<RevisionProps> = ({ repoURL, revision, helm, revisionEx
4242
}
4343
};
4444

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-
9445
export default Revision;

src/gitops/utils/urls.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import * as GitUrlParse from 'git-url-parse';
77
import { GitUrl } from 'git-url-parse';
88

99
export const isSHA = (revision: string) => {
10+
if (revision.startsWith('sha256:')) {
11+
const hashOnly = revision.replace('sha256:', '');
12+
return hashOnly.match(/^[a-f0-9]{8,69}$/) !== null;
13+
}
1014
// https://stackoverflow.com/questions/468370/a-regex-to-match-a-sha1
1115
return revision.match(/^[a-f0-9]{5,40}$/) !== null;
1216
};

0 commit comments

Comments
 (0)