/travis-ci-enable

Enable Travis CI builds for one or more repositories.

Primary LanguageJavaScriptMIT LicenseMIT

Enable

NPM version Build Status Coverage Status Dependencies

Enable Travis CI builds for one or more repositories.

Installation

$ npm install travis-ci-enable

Usage

var enable = require( 'travis-ci-enable' );

enable( repos, options, clbk )

Enable Travis CI builds for one or more repositories. Each provided repository slug should obey the format :owner/:repo.

var repos = [
    'math-io/polygamma',
    'math-io/gammaln',
    'kgryte/utils-deep-set',
    'unknown/repo'
];

var opts = {
    'token': 'tkjorjk34ek3nj4!'
};

enable( repos, opts, clbk )

function clbk( error, results ) {
    if ( error ) {
        throw new Error( error.message );
    }
    console.dir( results );
    /* returns
        {
            "meta": {
                "total": 4,
                "success": 3,
                "failure": 1
            },
            "data": {
                "math-io/polygamma": {
                    "result": true
                },
                "math-io/gammaln": {
                    "result": true
                },
                "kgryte/utils-deep-set": {
                    "result": true
                }
            },
            "failures": {
                "unknown/repo": "Not Found"
            }
        }
    */
}

The function accepts the following options:

  • token: Travis CI access token (required).
  • hostname: endpoint hostname. Default: 'api.travis-ci.org'.
  • sync: boolean indicating whether to sync Travis CI with Github before attempting to enable builds. Default: false.

To authenticate with Travis CI, set the token option.

var opts = {
    'token': 'tkjorjk34ek3nj4!'
};

enable( repos, opts, clbk );

By default, the function contacts the Travis CI API for open source. To use a different Travis CI API endpoint, set the hostname option.

var opts = {
    'token': 'tkjorjk34ek3nj4!',
    'hostname': 'api.travis-ci.com'
};

enable( repos, opts, clbk );

Travis CI requires a manual sync in order to have the most up-to-date repository information for an authenticated user. Hence, unless a sync is triggered, Travis CI is not aware of repositories created since the last sync. To trigger a sync before attempting to enable, set the sync option to true.

var opts = {
    'token': 'tkjorjk34ek3nj4!',
    'hostname': 'api.travis-ci.com',
    'sync': true
};

enable( repos, opts, clbk );

enable.factory( options, clbk )

Creates a reusable function.

var opts = {
    'token': 'tkjorjk34ek3nj4!',
    'hostname': 'api.travis-ci.com',
    'sync': true
};

var run = enable.factory( opts, clbk );

var repos = [ 'math-io/erf', 'math-io/erfc' ];
run( repos );

// Some time later...
repos = [ 'kgryte/utils-copy', 'kgryte/utils-merge' ];
run( repos );

// Some time later...
repos = [ 'dstructs/matrix', 'math-io/evalpoly' ];
run( repos );

// ...

The factory method accepts the same options as enable().

Notes

  • If the module encounters an application-level error while initially querying an endpoint (e.g., no network connection, malformed request, etc), that error is returned immediately to the provided callback.

  • If the module encounters a downstream error (e.g., timeout, reset connection, etc), that error is included in the returned results under the failures field.

  • If possible, avoid repeatedly triggering a sync in close succession, as each sync may entail multiple Github API requests, thus affecting a user's Github rate limit. Hence, if an application requires multiple enable invocations, separately trigger a sync once, rather than setting the sync option to true.

    var sync = require( 'travis-ci-sync' );
    var factory = require( 'travis-ci-enable' ).factory;
    
    var opts = {
        'token': 'tkjorjk34ek3nj4!',
        'sync': false
    };
    
    sync( opts, onSync );
    
    function onSync( error ) {
        var enable;
        var repos;
        if ( error ) {
            throw new Error( error.message );
        }
        enable = factory( opts, onEnable );
    
        repos = [ 'math-io/erf', 'math-io/erfc' ];
        enable( repos );
    
        // Next invocation does not trigger a sync...
        repos = [ 'kgryte/utils-copy', 'kgryte/utils-merge' ];
        enable( repos );
    }
    
    function onEnable( error, results ) {
        if ( error ) {
            return console.error( error.message );
        }
        console.dir( results );
    }

Examples

var enable = require( 'travis-ci-enable' );

var repos = [
    'math-io/erf',
    'math-io/erfc',
    'math-io/erfinv'
];

var opts = {
    'token': 'tkjorjk34ek3nj4!'
};

enable( repos, opts, clbk );

function clbk( error, results ) {
    if ( error ) {
        throw new Error( error.message );
    }
    console.dir( results );
}

To run the example code from the top-level application directory,

$ node ./examples/index.js

Note: in order to run the example, you will need to obtain an access token and modify the token option accordingly.


CLI

Installation

To use the module as a general utility, install the module globally

$ npm install -g travis-ci-enable

Usage

Usage: travisenable [options] slug1 slug2 ...

Options:

  -h,  --help               Print this message.
  -V,  --version            Print the package version.
       --hostname host      Hostname. Default: api.travis-ci.org.
       --token token        Travis CI access token.
       --sync               Sync Travis CI with Github.

Notes

  • If a slug is not provided, the implementation will attempt to infer a slug by executing

    git config remote.origin.url
    

    in the current working directory.

  • In addition to the token option, the token may be specified by a TRAVISCI_TOKEN environment variable. The command-line option always takes precedence.

  • If builds for a repository are successfully enabled, results are written to stdout.

  • If builds for a repository cannot be enabled due to a downstream error (failure), the repository slug (and its associated error) is written to sterr.

  • Output order is not guaranteed to match input order.

Examples

Setting the access token using the command-line option:

$ DEBUG=* travisenable --token <token> math-io/erfinv
# => {...}

Setting the access token using an environment variable:

$ DEBUG=* TRAVISCI_TOKEN=<token> travisenable math-io/erfinv
# => {...}

For local installations, modify the command to point to the local installation directory; e.g.,

$ DEBUG=* ./node_modules/.bin/travisenable --token <token> math-io/erfinv
# => {...}

Or, if you have cloned this repository and run npm install, modify the command to point to the executable; e.g.,

$ DEBUG=* node ./bin/cli --token <token> math-io/erfinv
# => {...}

Tests

Unit

This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

Browser Support

This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:

$ make test-browsers

To view the tests in a local web browser,

$ make view-browser-tests

License

MIT license.

Copyright

Copyright © 2016. Athan Reines.