A little time ago we had a question whether it is possible to import a subset of the repositories
in an old v1 instance of SCM-Manager to an up-to-date SCM-Manager v2. Normally, this should be
done using the migration wizzard, but in this case we had different old SCM-Managers and an already running v2 instance.
So we implemented two scripts that can be run using the script plugin. With this plugin, you can do anything that can be done by SCM-Manager, and we thought this is a good use case to show you some of its power.
The first script is used to get all the available repositories from the legacy instance to prepare a list of the repositories that shall be imported. This list then can be used by the second script.
Right below you will find the first script. You just have to set the URL and the credentials for the old v1 SCM-Manager instance and it will create a CVS output that you can edit with Excel or any text edior of your liking. The script uses the same suggestions for the namespace and the name of the imported repository as the migration wizzard would have done.
def scmmV1 = "http://localhost:8082/scm/"
def user = "scmadmin"
def passwd = "scmadmin"
String authString = new String(Base64.getEncoder().encode((user + ":" + passwd).getBytes()))
def get = new URL(scmmV1 + "api/rest/repositories.json").openConnection()
get.setRequestProperty("Authorization", "Basic " + authString)
def getSuggestion(repoName) {
def nameComponents = repoName.split("/")
switch (nameComponents.length) {
case 1: return ["git", nameComponents[0]]
case 2: return nameComponents
default:
def firstParts = nameComponents.take(nameComponents.length - 1);
def namespace = String.join("_", firstParts)
def name = nameComponents[nameComponents.length - 1]
return [namespace, name]
}
}
def rc = get.getResponseCode()
if (rc.equals(200)) {
def json = get.getInputStream().getText()
def jsonSlurper = new groovy.json.JsonSlurper()
def repos = jsonSlurper.parseText(json)
println "found ${repos.size} repositories"
println ""
println "Name v1,URL,Namespace,Name"
repos.each { repo ->
(namespace, name) = getSuggestion(repo.name)
println "${repo.name},${repo.url},${namespace},${name}"
}
} else {
println "connection failed; return code: " + rc
}
You can adapt this list and suggestions for namespace and name as you like. Repositories you don’t want to import can simply be deleted from the list. When you have finished your selection, simply replace the example in the second script below, set the credentials for the old SCM-Manager instance once more and you are ready to start the import. Please keep in mind, that the imports might take some time and therefore the output may take some time to appear.
def user = "scmadmin"
def passwd = "scmadmin"
def repositories = """
Name v1,URL,Namespace,Name
simple,http://localhost:8082/scm/git/simple,git,simple
just/one,http://localhost:8082/scm/git/just/one,just,one
very/complex/path/isnt/it,http://localhost:8082/scm/git/very/complex/path/isnt/it,very_complex_path_isnt,it2
"""
def importer = injector.getInstance(sonia.scm.importexport.FromUrlImporter.class)
def lines = repositories.split "\n"
lines.each { line ->
if (!line.empty && !line.startsWith("Name v1")) {
(oldName, url, namespace, name) = line.split(",")
println "importing ${namespace}/${name} from ${url}"
def parameters = new sonia.scm.importexport.FromUrlImporter.RepositoryImportParameters()
parameters.importUrl = url.trim()
parameters.username = user
parameters.password = passwd
try {
importer.importFromUrl(
parameters,
new sonia.scm.repository.Repository(null, "git", namespace.trim(), name.trim())
)
} catch (Exception e) {
println " Failed: " + e.message
}
}
}
Maybe you will find this useful for some reason or another. Even if you do not have to import repositories from legacy instances, you may see this as a small demonstration of the Groovy programming language used by the script plugin. Don’t hesitate to try something on your own, but be careful and keep in mind, that you can create mess. Better start your experiments with a test or a developer instance before going into production.
We would love to hear back from you whether you find the script plugin or this little example helpful for your work.
Keep on coding!