Skip to content

Instantly share code, notes, and snippets.

@chustedde
Created July 14, 2016 21:38
Show Gist options
  • Save chustedde/256c2ee56791d8cd507f8fc2628c3f60 to your computer and use it in GitHub Desktop.
Save chustedde/256c2ee56791d8cd507f8fc2628c3f60 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++
}
@chustedde
Copy link
Author

Known issue: Will overwrite snippets if you happen to have more than one with the same name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment