forked from Joxit/docker-registry-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(branching): add new test for custom branching
- Loading branch information
Showing
2 changed files
with
133 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,59 @@ | ||
import { DockerRegistryUIError } from './error.js'; | ||
|
||
const getRepositoryName = (split, max) => { | ||
let repositoryName = ''; | ||
for (let i = 0; i < max; i++) { | ||
for (let i = 0; i < Math.min(max, split.length - 1); i++) { | ||
repositoryName += `${split[i]}/`; | ||
} | ||
return repositoryName; | ||
}; | ||
|
||
export const getBranching = | ||
(min = 1, max = 1) => | ||
(repositories) => | ||
const getLatestRepository = (repo, repoName) => { | ||
if (!repo.images) { | ||
return; | ||
} | ||
if (repo.repo === repoName) { | ||
return repo; | ||
} | ||
for (let i = 0; i < repo.images.length; i++) { | ||
const res = getLatestRepository(repo.images[i], repoName); | ||
if (res) { | ||
return res; | ||
} | ||
} | ||
|
||
if (repoName.startsWith(repo.repo)) { | ||
const newRepo = { repo: repoName, images: [] }; | ||
repo.images.push(newRepo); | ||
return newRepo; | ||
} | ||
}; | ||
|
||
export const getBranching = (min = 1, max = 1) => { | ||
if (min > max) { | ||
throw new DockerRegistryUIError(`min must be inferior to max (min: ${min} <= max: ${max})`); | ||
} else if (max < 0 || min < 0) { | ||
throw new DockerRegistryUIError( | ||
`min and max must be greater than equals to 0 (min: ${min} >= 0 and max: ${max} >= 0)` | ||
); | ||
} | ||
return (repositories) => | ||
repositories.sort().reduce(function (acc, image) { | ||
const split = image.split('/'); | ||
if (split.length > min && min > 0) { | ||
const repoName = getRepositoryName(split, max); | ||
if (acc.length === 0 || acc[acc.length - 1].repo != repoName) { | ||
acc.push({ | ||
let repo = acc.length > 0 && getLatestRepository(acc[acc.length - 1], repoName); | ||
if (!repo) { | ||
repo = { | ||
repo: repoName, | ||
images: [], | ||
}); | ||
}; | ||
acc.push(repo); | ||
} | ||
acc[acc.length - 1].images.push(image); | ||
repo.images.push(image); | ||
return acc; | ||
} | ||
acc.push(image); | ||
return acc; | ||
}, []); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters