Skip to content

Instantly share code, notes, and snippets.

@bran921007
Forked from chustedde/export-gitlab-snippets.ps1
Created December 27, 2018 01:15
Show Gist options
  • Save bran921007/37d297547108175b83ee29f9c3c2316a to your computer and use it in GitHub Desktop.
Save bran921007/37d297547108175b83ee29f9c3c2316a to your computer and use it in GitHub Desktop.
Export snippets from GitLab
# Quick 'n' dirty script to export snippets from GitLab to local files. Useful if you're
# migrating to GitHub or just want to back up your snippets as files. Doesn't catch errors
# or anything like that, but what were you expecting for a Gist? :-) Also, this will only
# export snippets that are visible to you, so other users' private snippets are excluded.
# Tested with GitLab v8.5.1
$username = "username" # Your username goes here
$password = "password" # Your password goes here
$baseURL = "https://your.gitlab.base.url" # Your GitLab base URL goes here
$lastSnippet = 80 # Change the number to the highest snippet number you have
$snippetFolder = "C:\your\snippet\folder" # Your snippet folder goes here
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$false
$ie.navigate("$baseURL/users/sign_in")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("username").value= "$username"
$ie.document.getElementById("password").value = "$password"
$ie.document.getElementById("new_ldap_user").submit() # This is for LDAP logins - change the form ID according to how you log in
start-sleep 10
$i = 0
while ($i -le $lastSnippet) {
$ie.navigate("$baseURL/snippets/$i")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
if ($ie.Document.body.innerHTML -notmatch "HTTP 404") {
$filename = $ie.Document.IHTMLDocument3_getElementsByTagName("strong")
$filename | Select-Object -Property innerHTML -OutVariable filename
$filename = $filename.innerHTML.ToString().Substring(1,($filename.innerHTML.ToString().Length) - 2)
$ie.navigate("$baseURL/snippets/$i/raw")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.Document.body.innerText | Out-File -FilePath "$snippetFolder\$filename"
}
$i++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment