all files / lib/ travis-encrypt.js

100% Statements 47/47
100% Branches 22/22
100% Functions 11/11
100% Lines 47/47
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 9810× 10× 10×   20×                                                                     16× 14×     12×     10× 10×   10×                     10×  
var rsa = require('ursa');
var merge = require('lodash.merge');
var Travis = require('travis-ci');
 
function isUndefined (arg) {
  return typeof arg === 'undefined';
}
 
function encryptData (data, key, callback) {
  var pem = key.replace(/RSA PUBLIC KEY/g, 'PUBLIC KEY');
  try {
    var publicKey = rsa.createPublicKey(pem);
    var cipherText = publicKey.encrypt(data, undefined, undefined, rsa.RSA_PKCS1_PADDING);
    return callback(null, cipherText.toString('base64'));
  } catch (err) {
    return callback(err);
  }
}
 
function getTravisClient (pro) {
  return new Travis({
    version: '2.0.0',
    pro: pro
  });
}
 
function getRepoNotFoundError (options) {
  return new Error('repository ' + options.owner + '/' + options.repo + ' not found');
}
 
function encryptTravisProData (options, callback) {
  var travis = getTravisClient(true);
 
  travis.authenticate({
    username: options.username,
    password: options.password
  }, function onTravisAuthResponse (err) {
    if (err) {
      return callback(err);
    }
 
    getRepoKeyAndEncrypt(travis, options, callback);
  });
}
 
function encryptTravisData (options, callback) {
  getRepoKeyAndEncrypt(getTravisClient(), options, callback);
}
 
function getRepoKeyAndEncrypt (client, options, callback) {
  client.repos(options.owner, options.repo).key.get(
    function onTravisRepoResponse (keyError, res) {
      if (!keyError) {
        return encryptData(options.data, res.key, callback);
      }
 
      client.repos(options.owner, options.repo).get(
        function onKeylessTravisRepoResponse (reposError) {
          callback(reposError ? getRepoNotFoundError(options) : keyError);
        }
      );
    }
  );
}
 
function encrypt (options, callback) {
  if (typeof options.repo !== 'string') {
    return callback(new TypeError('`repo` must be a string'));
  } else if (!options.repo.match(/.\/./)) {
    return callback(new Error('`repo` must be in `owner/repo` form'));
  }
 
  if (typeof options.data !== 'string') {
    return callback(new TypeError('`data` must be a string'));
  }
 
  var hasUser = !isUndefined(options.username);
  var hasPass = !isUndefined(options.password);
 
  if ((!hasUser && hasPass) || (hasUser && !hasPass)) {
    return callback(new Error('insufficient github credentials'));
  }
 
  var repo = options.repo.split('/', 2);
  var opts = merge(options, {
    owner: repo[0],
    repo: repo[1]
  });
 
  if (hasUser && hasPass) {
    encryptTravisProData(opts, callback);
  } else {
    encryptTravisData(opts, callback);
  }
}
 
module.exports = encrypt;