For whomever might be interested in this functionality, I managed to make it work by creating a custom plugin that I add to the default template in my setup package.
The code is basically this:
module BasicSetup
using PkgTemplates
import PkgTemplates: @with_kw_noshow, priority, prehook, @plugin, Plugin, validate
import LibGit2: LibGit2, GitRepo, ref_list, lookup_remote, set_remote_url, add_push!
export MyTemplate, GitGroup
# We create a simple plugin that modifies the git config to assign a group
@plugin struct GitGroup <: Plugin
name::AbstractString = "<default group name>"
end
priority(::GitGroup, ::typeof(prehook)) = 100 # We make it go after Git
function validate(::GitGroup, t::Template)
# Here we just make sure that Git is a plugin of t,
git_idx = findfirst(x -> x isa Git, t.plugins)
if git_idx isa Nothing
throw(ArgumentError("The GitGroup plugin can only be used if the Git plugin is also loaded"))
end
end
function prehook(g::GitGroup, t::Template, pkg_dir::AbstractString)
repo = LibGit2.GitRepo(pkg_dir)
remote = LibGit2.lookup_remote(repo, "origin")
# We change the remote url to have the group name rather than the user name
new_url = replace(LibGit2.url(remote), "$(t.user)" => "$(g.name)")
set_remote_url(repo, "origin", new_url)
# We add the push upstream
ref = first(ref_list(repo))
add_push!(repo, remote, ref)
nothing
end
function MyTemplate(;
host = "<your host>",
julia = v"1.9.0",
plugins = [],
kwargs...)
default_plugins = [
!TagBot,
ProjectFile(;version = v"0.1.0"),
!GitHubActions,
!CompatHelper,
GitGroup(),
]
Template(;
host,
julia,
plugins = [
plugins...,
default_plugins..., # The first instance of the plugin is retained if multiple are present
],
kwargs...
)
end
end
This way the user just has to load BasicSetup
and create an instance of MyTemplate
with their user and email and the custom Plugin takes care of changing the git repo url to point to the group rather than the user, and also sets up the default upstream so that the user can simply create the new project on the git server (in our case, self-hosted gitlab) without requiring to create a project from the Web GUI first.