Giter Club home page Giter Club logo

go-travis's Introduction

This Repository is Unmaintained

This repository is currently unmaintained, please do not use it for new projects. You may be interested in @shuheiktgw's fork, which has been updated to use the Travis v3 API. It is available here: https://github.com/shuheiktgw/go-travis.

go-travis

go-travis is a Go client library for accessing the Travis CI API.

Documentation: GoDoc

go-travis requires Go version 1.1 or greater.

Dive

import (
    "log"
    travis "github.com/Ableton/go-travis"
)

client := travis.NewDefaultClient("")
builds, _, _, resp, err := client.Builds.ListFromRepository("Ableton/go-travis", nil)
if err != nil {
    log.Fatal(err)
}

// Now do something with the builds

Installation

$ go get github.com/Ableton/go-travis

Usage

Interaction with the Travis CI API is done through a Client instance.

import travis "github.com/Ableton/go-travis"

client := travis.NewClient(travis.TRAVIS_API_DEFAULT_URL, "asuperdupertoken")

Constructing it with the NewClient helper requires two arguments:

  • The Travis CI API URL you wish to communicate with. Different Travis CI plans are accessed through different URLs. go-travis exposes constants for these URLs:
    • TRAVIS_API_DEFAULT_URL: default api.travis-ci.org endpoint for the free Travis "Open Source" plan.
    • TRAVIS_API_PRO_URL: the api.travis-ci.com endpoint for the paid Travis pro plans.
  • A Travis CI token with which to authenticate. If you wish to run requests unauthenticated, pass an empty string. It is possible at any time to authenticate the Client instance with a Travis token or a Github token. For more information see Authentication.

Services oriented design

The Client instance's Service attributes provide access to Travis CI API resources.

opt := &travis.BuildListOptions{EventType: "pull request"}
builds, response, err := client.Builds.ListFromRepository("mygithubuser/mygithubrepo", opt)
if err != nil {
        log.Fatal(err)
}

Non exhaustive list of implemented services:

  • Authentication
  • Branches
  • Builds
  • Commits
  • Jobs
  • Logs
  • Repositories
  • Requests
  • Users

(For an up to date exhaustive list, please check out the documentation)

Nota: Service methods will often take an Option (sub-)type instance as input. These types, like BuildListOptions allow narrowing and filtering your requests.

Authentication

The Client instance supports both authenticated and unauthenticated interaction with the Travis CI API. Note that both Pro and Enterprise plans will require almost all API calls to be authenticated.

Unuathenticated

It is possible to use the client unauthenticated. However some resources won't be accesible.

unauthClient := travis.NewClient(travis.TRAVIS_API_DEFAULT_URL, "")
builds, _, _, resp, err := unauthClient.Builds.ListFromRepository("mygithubuser/myopensourceproject", nil)
// Do something with your builds

_, err := unauthClient.Jobs.Cancel(12345)
if err != nil {
        // This operation is unavailable in unauthenticated mode and will
        // throw an error.
}

Authenticated

The Client instance supports authentication with both Travis token and Github token.

authClient := travis.NewClient(travis.TRAVIS_API_DEFAULT_URL, "mytravistoken")
builds, _, _, resp, err := authClient.Builds.ListFromRepository("mygithubuser/myopensourceproject",
nil)
// Do something with your builds

_, err := unauthClient.Jobs.Cancel(12345)
// Your job is succesfully canceled

However, authentication with a Github token will require and extra step (and request).

authWithGithubClient := travis.NewClient(travis.TRAVIS_API_DEFAULT_URL, "")
// authWithGithubClient.IsAuthenticated() will return false

err := authWithGithubClient.Authentication.UsingGithubToken("mygithubtoken")
if err != nil {
        log.Fatal(err)
}
// authWithGithubClient.IsAuthenticated()  will return true

builds, _, _, resp, err := authClient.Builds.ListFromRepository("mygithubuser/myopensourceproject",
nil)
// Do something with your builds

Pagination

The services support resource pagination through the ListOption type. Every services Option type implements the ListOption type.

client := travis.NewClient(travis.TRAVIS_API_DEFAULT_URL, "mysuperdupertoken")
opt := &travis.BuildListOptions{}

for {
        travisBuilds, _, _, _, err := tc.Builds.ListFromRepository(target, opt)
        if err != nil {
                log.Fatal(err)
        }

        // Do something with the builds

        opt.GetNextPage(travisBuilds)
        if opt.AfterNumber <= 1 {  // Travis CI resources are one-indexed (not zero-indexed)
                break
        }
}

Disclaimer

This library design is heavily inspired from the amazing Google's go-github library. Some pieces of code have been directly extracted from there too. Therefore any obvious similarities would not be adventitious.

License

This library is distributed under the BSD-style license found in the LICENSE file.

go-travis's People

Contributors

dmitshur avatar ivanfoo avatar mst-ableton avatar tcr-ableton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-travis's Issues

404 link in README.

The README has a Roadmap section (likely originally taken from go-github):

Roadmap

This library is being initially developed for internal applications at
Ableton. Therefore API methods are implemented in the order that they are
needed by our applications. Eventually, we would like to cover the entire
Travis API, so contributions are of course always welcome.

But the "contributing" link is broken because there's no CONTRIBUTING.md file in the repo.

Imported "testing" outside of a "_test.go" file

Because of this file/line, any executable that imports "go-travis" ends up importing "testing" as well, which adds the following to -help output:

  -test.bench string
        regular expression to select benchmarks to run
  -test.benchmem
        print memory allocations for benchmarks
  -test.benchtime duration
        approximate run time for each benchmark (default 1s)
  -test.blockprofile string
        write a goroutine blocking profile to the named file after execution
  -test.blockprofilerate int
        if >= 0, calls runtime.SetBlockProfileRate() (default 1)
  -test.count n
        run tests and benchmarks n times (default 1)
  -test.coverprofile string
        write a coverage profile to the named file after execution
  -test.cpu string
        comma-separated list of number of CPUs to use for each test
  -test.cpuprofile string
        write a cpu profile to the named file during execution
  -test.memprofile string
        write a memory profile to the named file after execution
  -test.memprofilerate int
        if >=0, sets runtime.MemProfileRate
  -test.outputdir string
        directory in which to write profiles
  -test.parallel int
        maximum test parallelism (default 8)
  -test.run string
        regular expression to select tests and examples to run
  -test.short
        run smaller test suite to save time
  -test.timeout duration
        if positive, sets an aggregate time limit for all tests
  -test.trace string
        write an execution trace to the named file after execution
  -test.v
        verbose: print additional output

Expose a NewDefaultClient constructor

@thoas suggested that exposing a NewDefaultClient constructor, pointing to the default travis api url would make users life easier.

Sounds like both a good suggestion and a good idea!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.