clone: function clone()

in assets/app/models/Github.js [265:334]


  clone: function clone(source, destination, done) {
    var model = this;
    var method = model.clone;
    var err;

    if (!source || !source.owner || !source.repository || !destination) {
      err = new Error('Missing source or destination');
      return done ? done(err) : err;
    }

    method.checkSource = function checkDestination(done) {
      var url = model.url({
        root: true, owner: source.owner, repository: source.repository
      });

      $.ajax({
        dataType: 'json',
        url: url,
        success: done.bind(this, null),
        error: done
      });
    };

    method.createRepo = function createRepo(done) {
      var route = destination.organization ?
        'orgs/' + destination.organization : 'user';
      var url = model.url({ route: route, method: 'repos' });
      var data = { name: destination.repository };

      $.ajax({
        method: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(data),
        url: url,
        success: done.bind(this, null),
        error: done
      });

    };

    method.cloneRepo = function cloneRepo(done) {
      var url = '/v0/site/clone';
      var data = {
        sourceOwner: source.owner,
        sourceRepo: source.repository,
        destinationOrg: destination.organization,
        destinationRepo: destination.repository,
        destinationBranch: destination.branch || this.branch,
        engine: destination.engine || 'jekyll'
      };
      $.ajax({
        method: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(data),
        url: url,
        success: done.bind(this, null),
        error: done
      });
    };

    if (done) async.series([
      method.checkSource.bind(this),
      method.createRepo.bind(this),
      method.cloneRepo.bind(this)
    ], done);

    return this;
  },