|
| 1 | +import * as Promise from "bluebird"; |
| 2 | +import { Options } from "./options"; |
| 3 | +import { GitHubService, RepoResult, DetailedRepoResult } from "./gitHubService"; |
| 4 | +import { GitService } from "./gitService"; |
| 5 | +import { Logger } from "./logger"; |
| 6 | + |
| 7 | +export class Application { |
| 8 | + constructor( |
| 9 | + public options: Options, |
| 10 | + public logger: Logger, |
| 11 | + public githubService: GitHubService = new GitHubService(options, logger), |
| 12 | + public gitService: GitService = new GitService(options, logger), |
| 13 | + public maxGitHubConcurrency: number = 3, |
| 14 | + public maxGitConcurrency: number = Infinity |
| 15 | + ) { } |
| 16 | + |
| 17 | + main() { |
| 18 | + if (this.options.isValid() === false) { |
| 19 | + throw new Error("Invalid options"); |
| 20 | + } |
| 21 | + |
| 22 | + this.start(); |
| 23 | + } |
| 24 | + |
| 25 | + start() { |
| 26 | + this.getForkedRepositories() |
| 27 | + .then(repos => this.getRepositoryDetails(repos)) |
| 28 | + .then(values => this.syncRepositories(values)) |
| 29 | + .then(() => { |
| 30 | + this.logger.log("Finished", null); |
| 31 | + this.logger.flush(); |
| 32 | + }) |
| 33 | + .catch(err => { |
| 34 | + throw Error(err); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + getForkedRepositories(page: number = 1, repositories: Array<RepoResult> = new Array<RepoResult>()) { |
| 39 | + this.logger.log("Looking for forked repositories at page " + page, null); |
| 40 | + return this.githubService.getRepos(this.options.username, page) |
| 41 | + .then(repos => { |
| 42 | + if (repos.length === 0) { |
| 43 | + return new Promise(resolve => { |
| 44 | + resolve(repositories); |
| 45 | + }); |
| 46 | + } |
| 47 | + for (const repo of repos) { |
| 48 | + if (repo.fork === true) { |
| 49 | + repositories.push(repo); |
| 50 | + } |
| 51 | + } |
| 52 | + return this.getForkedRepositories(page + 1, repositories); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + getRepositoryDetails(repos: Array<RepoResult>) { |
| 57 | + this.logger.log("Found " + repos.length + " forks. Fetching details...", null); |
| 58 | + const queue = new Array<any>(); |
| 59 | + const results = new Array<DetailedRepoResult>(); |
| 60 | + for (const repo of repos) { |
| 61 | + queue.push(new Promise((innerResolve, innerReject) => { |
| 62 | + this.logger.log("Fetching details for repository", repo.full_name); |
| 63 | + this.githubService.getRepo(this.options.username, repo.name) |
| 64 | + .then(repoResult => { |
| 65 | + innerResolve(repoResult); |
| 66 | + }) |
| 67 | + .catch(err => { |
| 68 | + innerReject(err); |
| 69 | + }); |
| 70 | + })); |
| 71 | + } |
| 72 | + return Promise.map(queue, (item: DetailedRepoResult) => { |
| 73 | + return item; |
| 74 | + }, { concurrency: this.maxGitHubConcurrency }); |
| 75 | + } |
| 76 | + |
| 77 | + syncRepositories(repos: Array<DetailedRepoResult>) { |
| 78 | + const queue = new Array<any>(); |
| 79 | + const self = this; |
| 80 | + for (const value of repos) { |
| 81 | + queue.push(new Promise((resolve, reject) => { |
| 82 | + self.syncRepository(value) |
| 83 | + .then(() => { |
| 84 | + resolve(); |
| 85 | + }) |
| 86 | + .catch(err => { |
| 87 | + reject(err); |
| 88 | + }); |
| 89 | + })); |
| 90 | + } |
| 91 | + return Promise.map(queue, (item: DetailedRepoResult) => { |
| 92 | + return item; |
| 93 | + }, { concurrency: this.maxGitConcurrency }); |
| 94 | + } |
| 95 | + |
| 96 | + syncRepository(repo: DetailedRepoResult) { |
| 97 | + return new Promise((resolve, reject) => { |
| 98 | + this.cloneRepository(repo.name, repo.full_name, repo.clone_url) |
| 99 | + .then(() => { |
| 100 | + return this.gitService.setUpstream(repo.name, repo.full_name, repo.parent.clone_url); |
| 101 | + }) |
| 102 | + .then(() => { |
| 103 | + return this.gitService.pullUpstream(repo.name, repo.full_name, repo.default_branch); |
| 104 | + }) |
| 105 | + .then(() => { |
| 106 | + return this.gitService.pushOrigin(repo.name, repo.full_name, repo.default_branch); |
| 107 | + }) |
| 108 | + .then(() => { |
| 109 | + return this.trySyncMasterBranch(repo.name, repo.full_name, repo.default_branch); |
| 110 | + }) |
| 111 | + .then(() => { |
| 112 | + return this.gitService.syncTags(repo.name, repo.full_name); |
| 113 | + }) |
| 114 | + .then(() => { |
| 115 | + resolve(); |
| 116 | + }) |
| 117 | + .catch(err => { |
| 118 | + reject(err); |
| 119 | + }); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + cloneRepository(name: string, fullname: string, cloneUrl: string) { |
| 124 | + this.logger.log("Cloning repository " + cloneUrl, fullname); |
| 125 | + return new Promise((resolve, reject) => { |
| 126 | + this.gitService.clone(name, cloneUrl) |
| 127 | + .then(out => { |
| 128 | + resolve(); |
| 129 | + }) |
| 130 | + .catch(err => { |
| 131 | + if (err.toString().match(/destination path (.*) already exists and is not an empty directory/).length > 0) { |
| 132 | + this.logger.log("Repository folder already existed. Continuing.", fullname); |
| 133 | + resolve(); |
| 134 | + } |
| 135 | + else { |
| 136 | + reject(err); |
| 137 | + } |
| 138 | + }); |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + trySyncMasterBranch(name: string, fullname: string, branch: string) { |
| 143 | + return new Promise((resolve, reject) => { |
| 144 | + if (branch === "master") { |
| 145 | + resolve(); |
| 146 | + return; |
| 147 | + } |
| 148 | + this.logger.log("Trying to sync master branch", fullname); |
| 149 | + this.gitService.pullUpstream(name, fullname, "master") |
| 150 | + .then(() => { |
| 151 | + return this.gitService.pushOrigin(name, fullname, "master"); |
| 152 | + }) |
| 153 | + .then(() => { |
| 154 | + this.logger.log("Master branch synced", fullname); |
| 155 | + resolve(); |
| 156 | + }) |
| 157 | + .catch(err => { |
| 158 | + this.logger.log("Failed syncing master branch for repository", fullname); |
| 159 | + resolve(); |
| 160 | + }); |
| 161 | + }); |
| 162 | + } |
| 163 | +} |
0 commit comments