Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,13 @@ encrypt({
}, function (err, blob) {
// do something with the encrypted data blob...
});

// ...if you may want to use github token to login to travis pro.
encrypt({
repo: 'pwmckenna/private-repo',
data: 'EXAMPLE_ENV_VARIABLE=asdf',
token: 'github-token'
}, function (err, blob) {
// do something with the encrypted data blob...
});
```
50 changes: 37 additions & 13 deletions lib/travis-encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ var rsa = require('ursa');
var merge = require('lodash.merge');
var Travis = require('travis-ci');

var TOKEN = 'token';
var CREDS = 'creds';

function isUndefined (arg) {
return typeof arg === 'undefined';
}
Expand All @@ -28,19 +31,39 @@ function getRepoNotFoundError (options) {
return new Error('repository ' + options.owner + '/' + options.repo + ' not found');
}

function encryptTravisProData (options, callback) {
function encryptTravisProData (options, callback, type) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move type to second argument to match callback style always being the last argument

var travis = getTravisClient(true);

travis.authenticate({
username: options.username,
password: options.password
}, function onTravisAuthResponse (err) {
if (err) {
return callback(err);
}
if (type === CREDS) {
travis.authenticate({
username: options.username,
password: options.password
}, function onTravisAuthResponse (err) {
if (err) {
return callback(err);
}

getRepoKeyAndEncrypt(travis, options, callback);
});
getRepoKeyAndEncrypt(travis, options, callback);
});
} else {
travis.auth.github.post({
github_token: options.token
}, function (err, res) {
if (err) {
return callback(err);
}

travis.authenticate({
access_token: res.access_token
}, function (err) {
if (err) {
return callback(err);
}

getRepoKeyAndEncrypt(travis, options, callback);
});
});
}
}

function encryptTravisData (options, callback) {
Expand All @@ -49,7 +72,7 @@ function encryptTravisData (options, callback) {

function getRepoKeyAndEncrypt (client, options, callback) {
client.repos(options.owner, options.repo).key.get(
function onTravisRepoResponse (keyError, res) {
function onTravisRepoResponse (keyError, res) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing spaces here

if (!keyError) {
return encryptData(options.data, res.key, callback);
}
Expand All @@ -76,6 +99,7 @@ function encrypt (options, callback) {

var hasUser = !isUndefined(options.username);
var hasPass = !isUndefined(options.password);
var hasToken = !isUndefined(options.token);

if ((!hasUser && hasPass) || (hasUser && !hasPass)) {
return callback(new Error('insufficient github credentials'));
Expand All @@ -87,8 +111,8 @@ function encrypt (options, callback) {
repo: repo[1]
});

if (hasUser && hasPass) {
encryptTravisProData(opts, callback);
if ((hasUser && hasPass) || hasToken) {
encryptTravisProData(opts, callback, hasToken ? TOKEN : CREDS);
} else {
encryptTravisData(opts, callback);
}
Expand Down