Skip to content

Commit 354a0fb

Browse files
committed
Add hashed storage path calculations
1 parent 6f9973d commit 354a0fb

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

manifests/custom_hook.pp

+37-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,29 @@
88
# source => 'puppet:///modules/my_module/post-receive',
99
# }
1010
#
11+
# @example Calculate hashed storage path
12+
# gitlab::custom_hook { 'my_custom_hook':
13+
# project => 93,
14+
# type => 'post-receive',
15+
# source => 'puppet:///modules/my_module/post-receive',
16+
# }
17+
# # Hook path will be `@hashed/6e/40/6e4001871c0cf27c7634ef1dc478408f642410fd3a444e2a88e301f5c4a35a4d`
18+
#
19+
# @param project The GitLab project name, or the hashed directory name or project ID number
1120
# @param namespace The GitLab group namespace for the project.
12-
# @param project The GitLab project name.
1321
# @param type The custom hook type. Should be one of pre-receive, post-receive, or update.
1422
# @param content Specify the custom hook contents either as a string or using the template function. If this paramter is specified source parameter must not be present.
1523
# @param source Specify a file source path to populate the custom hook contents. If this paramter is specified content parameter must not be present.
1624
# @param repos_path The GitLab shell repos path. This defaults to '/var/opt/gitlab/git-data/repositories' if not present.
25+
# @param hashed_storage Whether to treat the project name as a hashed storage directory name or ID number
1726
define gitlab::custom_hook (
18-
String $namespace,
19-
String $project,
27+
Variant[String,Integer] $project,
2028
Enum['update', 'post-receive', 'pre-receive'] $type,
29+
Optional[String] $namespace = undef,
2130
Optional[String] $content = undef,
2231
Optional[String] $source = undef,
2332
Optional[Stdlib::Absolutepath] $repos_path = undef,
33+
Optional[Boolean] $hashed_storage = false,
2434
) {
2535
if $repos_path {
2636
$_repos_path = $repos_path
@@ -38,7 +48,30 @@
3848
fail("gitlab::custom_hook[${name}]: Must specify either content or source, but not both")
3949
}
4050

41-
$hook_path = "${_repos_path}/${namespace}/${project}.git/custom_hooks"
51+
if ! ($hashed_storage) and ! ($namespace) {
52+
fail("gitlab::custom_hook[${name}]: Must specify either namespace or hashed_storage")
53+
}
54+
55+
if ($hashed_storage) and ($namespace) {
56+
fail("gitlab::custom_hook[${name}]: Must specify either namespace or hashed_storage, but not both")
57+
}
58+
59+
60+
if ($namespace) {
61+
$hook_path = "${_repos_path}/${namespace}/${project}.git/custom_hooks"
62+
} elseif ($hashed_storage) {
63+
if ($project.is_a(Integer)) {
64+
$_project_hash = sha256(String($project))
65+
} else {
66+
$_project_hash = $project
67+
}
68+
69+
if ($_project_hash.length != 64) {
70+
fail("gitlab::custom_hook[${name}]: Invalid project hash ${_project_hash}")
71+
}
72+
73+
$hook_path = "${_repos_path}/@hashed/${_project_hash[0,2]}/${_project_hash[2,2]}/${_project_hash}.git/custom_hooks"
74+
}
4275

4376
File {
4477
owner => $gitlab::service_user,

0 commit comments

Comments
 (0)