@imqueue/cli
Version:
Command Line Interface for IMQ
100 lines • 3.58 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.travisEncrypt = travisEncrypt;
exports.trySyncBuilds = trySyncBuilds;
exports.enableBuilds = enableBuilds;
/*!
* IMQ-CLI library: travis
*
* I'm Queue Software Project
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* If you want to use this code in a closed source (commercial) project, you can
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
const NodeRSA = require("node-rsa");
const travis_1 = require("@imqueue/travis");
const node_1 = require("./node");
/**
* Returns encrypted secure key for travis sensitive data.
*
* @see https://docs.travis-ci.com/user/encryption-keys/
* @param {string} repository - git repository owner/name
* @param {string} data - sensitive data to encrypt
* @param {string} github_token - token if auth required (pro mode)
* @return {Promise<string>}
*/
async function travisEncrypt(repository, data, github_token) {
const travis = new travis_1.TravisClient({ pro: !!github_token });
// istanbul ignore next
if (github_token) {
await travis.authenticate({ github_token });
}
const [owner, repo] = repository.split('/');
const pem = await travis.repos(owner, repo).key.get();
const rsa = new NodeRSA();
rsa.setOptions({ encryptionScheme: 'pkcs1' });
rsa.importKey(pem.key);
return rsa.encrypt(Buffer.from(data, 'utf8'), 'base64');
}
// istanbul ignore next
/**
* Tries perform travis sync
*
* @param {TravisClient} travis - authenticated client
* @param {number} [retry] - current retry
* @param {number} maxRetries - max number of retries
* @param {number} delay - delay in milliseconds before result return
*/
async function trySyncBuilds(travis, retry = 0, maxRetries = 3, delay = 2000) {
try {
await travis.users.sync.post();
await (0, node_1.sleep)(delay);
}
catch (err) {
if (retry < maxRetries) {
await (0, node_1.sleep)(delay);
return trySyncBuilds(travis, ++retry, maxRetries);
}
return false;
}
return true;
}
// istanbul ignore next
/**
* Enables builds for a given repository
*
* @param {string} owner
* @param {string} repo
* @param {string} github_token
* @return {Promise<void>}
*/
async function enableBuilds(owner, repo, github_token, isPrivate) {
const travis = new travis_1.TravisClient({ pro: isPrivate });
await travis.authenticate({ github_token });
await trySyncBuilds(travis);
const hook = (await travis.hooks.get()).hooks.find((hook) => hook.owner_name === owner && hook.name === repo);
if (!hook) {
return false;
}
else if (hook.active) {
return true;
}
await travis.hooks(hook.id).put({ hook: { id: hook.id, active: true } });
return true;
}
//# sourceMappingURL=travis.js.map
;