|
| 1 | +#!/usr/bin/env groovy |
| 2 | + |
| 3 | +/** |
| 4 | + * groovy script to migrate (export) GitHub.com repositories to GitHub Enterprise |
| 5 | + * |
| 6 | + * Automates steps in https://github.com/blog/2171-migrate-your-repositories-using-ghe-migrator |
| 7 | + * |
| 8 | + * Run 'groovy MigrateRepositories.groovy' to see the list of command line options |
| 9 | + * |
| 10 | + * First run may take some time as required dependencies have to get downloaded, then it should be quite fast |
| 11 | + * |
| 12 | + * If you do not have groovy yet, run 'brew install groovy' |
| 13 | + */ |
| 14 | + |
| 15 | +@Grab(group='org.kohsuke', module='github-api', version='1.75') |
| 16 | +@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7') |
| 17 | +@Grab('oauth.signpost:signpost-core:1.2.1.2') |
| 18 | +@Grab('oauth.signpost:signpost-commonshttp4:1.2.1.2') |
| 19 | + |
| 20 | +import org.kohsuke.github.GitHub |
| 21 | +import groovyx.net.http.RESTClient |
| 22 | +import static groovyx.net.http.ContentType.* |
| 23 | +import groovy.json.JsonOutput |
| 24 | +import groovy.json.JsonSlurper |
| 25 | +import java.io.File |
| 26 | +import org.apache.http.params.BasicHttpParams |
| 27 | + |
| 28 | + |
| 29 | +opt = parseArgs(args) |
| 30 | + |
| 31 | +if(!opt) { |
| 32 | + return |
| 33 | +} |
| 34 | + |
| 35 | +token = opt.t?opt.t:System.getenv("GITHUB_TOKEN") |
| 36 | +sleepInterval = opt.s?1000*new Long(opt.s):5000 |
| 37 | +outputFileName = opt.f?opt.f:"migration_archive.tgz" |
| 38 | +org = opt.o |
| 39 | +lockRepos = opt.l |
| 40 | + |
| 41 | +repositories = getRepositoriesToMigrate(opt) |
| 42 | + |
| 43 | +if (repositories.empty) { |
| 44 | + println "No repository for org ${org} specified, exiting ..." |
| 45 | + return |
| 46 | +} |
| 47 | +println "Going to export the following repositories "+(lockRepos?"with":"without") + " locking to file ${outputFileName}:" |
| 48 | +repositories.each { println it} |
| 49 | + |
| 50 | + |
| 51 | +RESTClient restClient = getGithubApi("https://api.github.com/" , token) |
| 52 | + |
| 53 | +if (!opt.d) { |
| 54 | + resp = restClient.post( |
| 55 | + path: "orgs/${org}/migrations", |
| 56 | + body: JsonOutput.toJson( [lock_repositories: lockRepos, repositories: repositories]), |
| 57 | + requestContentType: URLENC ) |
| 58 | + |
| 59 | + assert resp.status == 201 |
| 60 | + assert resp.contentType == JSON.toString() |
| 61 | + |
| 62 | + migrationID = resp.data.id |
| 63 | + assert migrationID != null |
| 64 | + |
| 65 | + println "Got migration ID: ${migrationID}, waiting for migration to finish ..." |
| 66 | + |
| 67 | + waitForExportToFinish(sleepInterval, restClient, org, migrationID) |
| 68 | + |
| 69 | + println "Figuring out migration archive URL: /orgs/${org}/migrations/${migrationID}/archive" |
| 70 | + resp = restClient.get(path: "/orgs/${org}/migrations/${migrationID}/archive") |
| 71 | + assert resp.status == 302 |
| 72 | + |
| 73 | + println "Downloading migration archive and storing it as ${outputFileName} ..." |
| 74 | + file = new File(outputFileName).newOutputStream() |
| 75 | + file << new URL(resp.data.toString()).openStream() |
| 76 | + file.close() |
| 77 | + |
| 78 | + deleteMigrationArchive(restClient, org, migrationID) |
| 79 | +} |
| 80 | + |
| 81 | +def OptionAccessor parseArgs(args) { |
| 82 | + CliBuilder cli = new CliBuilder(usage: 'groovy MigrateRepositories.groovy -o <organization> [options] [reps in org]') |
| 83 | + cli.t(longOpt: 'token', 'personal access token (or use GITHUB_TOKEN env variable)', required: false , args: 1 ) |
| 84 | + cli.o(longOpt: 'organization', 'organization, if no repositories are specified, all repositories will be migrated', required: true , args: 1 ) |
| 85 | + cli.l(longOpt: 'lock', 'lock repositories (defaults to false)', required: false , args: 1 ) |
| 86 | + cli.s(longOpt: 'sleep', 'sleep interval between checking export status (defaults to 5 seconds)', required: false , args: 1 ) |
| 87 | + cli.f(longOpt: 'file', 'file to store exported tgz file (defaults to migration_archive.tar.gz)', required: false , args: 1 ) |
| 88 | + cli.d(longOpt: 'dry', 'dry-run only, only print what would happen without performing anything', required: false , args: 1 ) |
| 89 | + |
| 90 | + OptionAccessor opt = cli.parse(args) |
| 91 | + return opt |
| 92 | +} |
| 93 | + |
| 94 | +def getRepositoriesToMigrate(opt) { |
| 95 | + if (opt.arguments().size() != 0) { |
| 96 | + return opt.arguments().findAll { |
| 97 | + it.startsWith(org+"/") |
| 98 | + } |
| 99 | + } else { |
| 100 | + githubCom = GitHub.connectUsingOAuth(token); |
| 101 | + return githubCom.getOrganization(org).listRepositories().collect { it.getFullName(); } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +def RESTClient getGithubApi(url, token) { |
| 106 | + restClient = new RESTClient(url).with { |
| 107 | + headers['User-Agent'] = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" |
| 108 | + headers.'Accept' = 'application/vnd.github.wyandotte-preview+json' |
| 109 | + headers['Authorization'] = "token ${token}" |
| 110 | + it |
| 111 | + } |
| 112 | + // we need to disable redirects as GitHub redirects to Amazon S3 for the download |
| 113 | + restClient.client.setParams(new BasicHttpParams().setParameter("http.protocol.handle-redirects",false)) |
| 114 | + return restClient |
| 115 | +} |
| 116 | + |
| 117 | +def waitForExportToFinish(sleepInterval, restClient, org, migrationID) { |
| 118 | + String status |
| 119 | + while ("exported" != status) { |
| 120 | + println "Sleeping ${sleepInterval} ms ..." |
| 121 | + sleep sleepInterval |
| 122 | + println "Checking migration process ..." |
| 123 | + resp = restClient.get( path: "/orgs/${org}/migrations/${migrationID}" ) |
| 124 | + assert resp.status == 200 |
| 125 | + assert resp.contentType == JSON.toString() |
| 126 | + status=resp.data.state |
| 127 | + println "Migration status: ${status}" |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +def deleteMigrationArchive(restClient, org, migrationID) { |
| 132 | + println "Deleting archive on the server" |
| 133 | + resp = restClient.delete(path: "/orgs/${org}/migrations/${migrationID}/archive") |
| 134 | + assert resp.status == 204 |
| 135 | +} |
0 commit comments