Documentation ¶
Overview ¶
go-travis is a Go client library for accessing the Travis CI API.
Installation ¶
go-travis requires Go version 1.1 or greater.
$ go get github.com/Ableton/go-travis
Usage ¶
Interaction with the Travis CI API is done through a Client instance.
import travis "github.com/AbletonAppDev/go-travis" client := travis.NewClient(travis.TRAVIS_API_DEFAULT_URL, "AQFvXR7r88s2Db5-dMYo3g")
Constructing it with the NewClient helper requires two arguments:
First, 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.
Second, 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 ¶
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) }
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 interactions with the Travis CI API. Note that both Pro and Enterprise will require almost all API calls to be authenticated.
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 at you. }
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 } }
Index ¶
- Constants
- type AccessToken
- type AuthenticationService
- type Branch
- type BranchesService
- type Build
- type BuildListOptions
- type BuildsService
- func (bs *BuildsService) Cancel(id uint) (*http.Response, error)
- func (bs *BuildsService) Get(id uint) (*Build, []Job, *Commit, *http.Response, error)
- func (bs *BuildsService) List(opt *BuildListOptions) ([]Build, []Job, []Commit, *http.Response, error)
- func (bs *BuildsService) ListFromRepository(slug string, opt *BuildListOptions) ([]Build, []Job, []Commit, *http.Response, error)
- func (bs *BuildsService) Restart(id uint) (*http.Response, error)
- type Client
- type Commit
- type CommitsService
- type Config
- type ErrorResponse
- type Job
- type JobFindOptions
- type JobsService
- func (js *JobsService) Cancel(id uint) (*http.Response, error)
- func (js *JobsService) Find(opt *JobFindOptions) ([]Job, *http.Response, error)
- func (js *JobsService) Get(id uint) (*Job, *http.Response, error)
- func (js *JobsService) ListFromBuild(buildId uint) ([]Job, *http.Response, error)
- func (js *JobsService) Restart(id uint) (*http.Response, error)
- type ListOptions
- type Log
- type LogsService
- type NotificationMap
- type Paginator
- type RepositoriesService
- type Repository
- type RepositoryListOptions
- type Request
- type RequestsListOptions
- type RequestsService
- type User
- type UsersService
Constants ¶
const ( TRAVIS_USER_AGENT string = "go-travis/1.0.0" TRAVIS_REQUEST_ACCEPT_HEADER string = "application/vnd.travis-ci.2+json" TRAVIS_REQUEST_CONTENT_TYPE string = "application/json" TRAVIS_API_DEFAULT_URL string = "https://api.travis-ci.org/" TRAVIS_API_PRO_URL string = "https://api.travis-ci.com/" TRAVIS_RESPONSE_PER_PAGE uint64 = 25 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AccessToken ¶
type AccessToken string
type AuthenticationService ¶
type AuthenticationService struct {
// contains filtered or unexported fields
}
BuildsService handles communication with the builds related methods of the Travis CI API.
func (*AuthenticationService) UsingGithubToken ¶
func (as *AuthenticationService) UsingGithubToken(githubToken string) (AccessToken, *http.Response, error)
UsingGithubToken will generate a Travis CI API authentication token and call the UsingTravisToken method with it, leaving your client authenticated and ready to use.
func (*AuthenticationService) UsingTravisToken ¶
func (as *AuthenticationService) UsingTravisToken(travisToken string) error
UsingTravisToken will format and write provided travisToken in the AuthenticationService client's headers.
type Branch ¶
type Branch struct { Id uint `json:"id,omitempty"` RepositoryId uint `json:"repository_id,omitempty"` CommitId uint `json:"commit_id,omitempty"` Number string `json:"number,omitempty"` // Config Config `json:"config,omitempty"` State string `json:"state,omitempty"` StartedAt string `json:"started_at,omitempty"` FinishedAt string `json:"finished_at,omitempty"` Duration uint `json:"duration,omitempty"` JobIds []uint `json:"job_ids,omitempty"` PullRequest bool `json:"pull_request,omitempty"` }
Branch represents a Travis CI build
type BranchesService ¶
type BranchesService struct {
// contains filtered or unexported fields
}
BranchesService handles communication with the branches related methods of the Travis CI API.
func (*BranchesService) Get ¶
Get fetches a branch based on the provided repository slug and it's id.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (*BranchesService) GetFromSlug ¶
func (bs *BranchesService) GetFromSlug(repoSlug string, branchSlug string) (*Branch, *http.Response, error)
Get fetches a branch based on the provided repository slug and its name.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (*BranchesService) ListFromRepository ¶
List the branches of a given repository.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
type Build ¶
type Build struct { Id uint `json:"id,omitempty"` RepositoryId uint `json:"repository_id,omitempty"` Slug string `json:"slug,omitempty"` CommitId uint `json:"commit_id,omitempty"` Number string `json:"number,omitempty"` // Config Config `json:"config,omitempty"` PullRequest bool `json:"pull_request,omitempty"` PullRequestTitle string `json:"pull_request_title,omitempty"` PullRequestNumber uint `json:"pull_request_number,omitempty"` State string `json:"state,omitempty"` StartedAt string `json:"started_at,omitempty"` FinishedAt string `json:"finished_at,omitempty"` Duration uint `json:"duration,omitempty"` JobIds []uint `json:"job_ids,omitempty"` AfterNumber uint `json:"after_number,omitempty"` EventType string `json:"event_type,omitempty"` }
Build represents a Travis CI build
type BuildListOptions ¶
type BuildListOptions struct { ListOptions Ids []uint `url:"ids,omitempty"` RepositoryId uint `url:"repository_id,omitempty"` Slug string `url:"slug,omitempty"` Number string `url:"number,omitempty"` EventType string `url:"event_type,omitempty"` }
BuildListOptions specifies the optional parameters to the BuildsService.List method.
type BuildsService ¶
type BuildsService struct {
// contains filtered or unexported fields
}
BuildsService handles communication with the builds related methods of the Travis CI API.
func (*BuildsService) Cancel ¶
func (bs *BuildsService) Cancel(id uint) (*http.Response, error)
Cancel build with the provided id.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (*BuildsService) Get ¶
Get fetches a build based on the provided id.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (*BuildsService) List ¶
func (bs *BuildsService) List(opt *BuildListOptions) ([]Build, []Job, []Commit, *http.Response, error)
List the builds for the authenticated user.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (*BuildsService) ListFromRepository ¶
func (bs *BuildsService) ListFromRepository(slug string, opt *BuildListOptions) ([]Build, []Job, []Commit, *http.Response, error)
List a repository builds based on it's provided slug.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (*BuildsService) Restart ¶
func (bs *BuildsService) Restart(id uint) (*http.Response, error)
Restart build with the provided id.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
type Client ¶
type Client struct { // Headers to attach to every requests made with the client. // As a default, Headers will be used to provide Travis API authentication // token and other necessary headers. // However these could be updated per-request through a parameters. Headers map[string]string // Base URL for api requests. Defaults to the public Travis API, but // can be set to an alternative endpoint to use with Travis Pro or Enterprise. // BaseURL should alway be terminated by a slash. BaseURL *url.URL // User agent used when communicating with the Travis API UserAgent string // Services used to manipulate API entities Authentication *AuthenticationService Repositories *RepositoriesService Builds *BuildsService Jobs *JobsService Branches *BranchesService Logs *LogsService Commits *CommitsService Requests *RequestsService Users *UsersService // contains filtered or unexported fields }
A Client manages communication with the Travis CI API.
func NewClient ¶
NewClient returns a new Travis API client. If travisToken is not provided, the client can be authenticated at any time, using it's Authentication exposed service.
func NewDefaultClient ¶
NewDefaultClient returns a new Travis API client bound to the public travis API. If travisToken is not provided, the client can be authenticated at any time, using it's Authentication exposed service.
func (*Client) Do ¶
Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, the raw response body will be written to v, without attempting to first decode it.
func (*Client) IsAuthenticated ¶
IsAuthenticated indicates if Authorization headers were found in Client.Headers mapping.
func (*Client) NewRequest ¶
func (c *Client) NewRequest(method, urlStr string, body interface{}, headers map[string]string) (*http.Request, error)
NewRequest creates an API request. A relative URL can be provided in urlStr, in which case it is resolved relative to the BaseURL of the Client. Relative URLs should always be specified without a preceding slash. If specified, the value pointed to by body is JSON encoded and included as the request body. If specified, the map provided by headers will be used to udate request headers.
type Commit ¶
type Commit struct { Id uint `json:"id,omitempty"` Sha string `json:"sha,omitempty"` Branch string `json:"branch,omitempty"` Message string `json:"message,omitempty"` CommittedAt string `json:"committed_at,omitempty"` AuthorName string `json:"author_name,omitempty"` AuthorEmail string `json:"author_email,omitempty"` CommitterName string `json:"committer_name,omitempty"` CommitterEmail string `json:"committer_email,omitempty"` CompareUrl string `json:"compare_url,omitempty"` }
Commit represents a VCS commit as seen by Travis CI
type CommitsService ¶
type CommitsService struct {
// contains filtered or unexported fields
}
CommitsService handles communication with the commits related parts of the Travis CI API. As commits are not directly exposed as an endpoint, most of this service methods will fetch commits through the builds or jobs endpoint.
func (*CommitsService) GetFromBuild ¶
Get fetches the commit that triggered a build based on the build id.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (*CommitsService) ListFromRepository ¶
func (cs *CommitsService) ListFromRepository(repositorySlug string) ([]Commit, *http.Response, error)
List last commits attached to a repository builds.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
type Config ¶
type Config struct { Os string `json:"os,omitempty"` Language string `json:"language,omitempty"` Branches map[string][]string `json:"branches,omitemtpy"` BeforeScript []string `json:"before_script,omitempty"` Script []string `json:"script,omitempty"` AfterScript []string `json:"after_script,omitempty"` BeforeInstall []string `json:"before_install,omitempty"` Install []string `json:"install,omitempty"` AfterSuccess []string `json:"after_success,omitempty"` AfterFailure []string `json:"after_failure,omitempty"` Addons map[string]string `json:"addons,omitempty"` Notifications NotificationMap `json:"notifications,omitempty"` }
Config represents a Travis CI build config
As the endpoints responses fields can change types according to the request, it is not possible to represent them easily in a statically language like go. Therefore Its implementation is purposedly left in an incomplete and very limited state.
type ErrorResponse ¶
type ErrorResponse struct { // HTTP response that caused this error Response *http.Response // Error message produced by Travis API Message string `json:"error"` }
ErrorResponse reports an error caused by an API request. ErrorResponse implemented the Error interface.
func (*ErrorResponse) Error ¶
func (er *ErrorResponse) Error() string
type Job ¶
type Job struct { Id uint `json:"id,omitempty"` BuildId uint `json:"build_id,omitempty"` RepositoryId uint `json:"repository_id,omitempty"` CommitId uint `json:"commit_id,omitempty"` LogId uint `json:"log_id,omitempty"` Number string `json:"number,omitempty"` // Config Config `json:"config,omitempty"` State string `json:"state,omitempty"` StartedAt string `json:"started_at,omitempty"` FinishedAt string `json:"finished_at,omitempty"` Duration uint `json:"duration,omitempty"` Queue string `json:"queue,omitempty"` AllowFailure bool `json:"allow_failure,omitempty"` AnnotationIds []uint `json:"annotation_ids,omitempty"` }
Job represents a Travis CI job
type JobFindOptions ¶
type JobFindOptions struct { ListOptions // List of job ids Ids []uint `url:"ids,omitempty"` // Job state to filter by State string `url:"state,omitempty"` // Job queue to filter by Queue string `url:"queue,omitempty"` }
JobListOptions specifies the optional parameters to the JobsService.List method. You need to provide exactly one of the below attribute. If you provide State or Queue, a maximum of 250 jobs will be returned.
func (*JobFindOptions) IsValid ¶
func (jfo *JobFindOptions) IsValid() bool
IsValid asserts the JobFindOptions instance has one and only one value set to a non-zero value.
This method is particularly useful to check a JobFindOptions instance before passing it to JobsService.Find method.
type JobsService ¶
type JobsService struct {
// contains filtered or unexported fields
}
JobsService handles communication with the jobs related methods of the Travis CI API.
func (*JobsService) Cancel ¶
func (js *JobsService) Cancel(id uint) (*http.Response, error)
Cancel job with the provided id.
Travis CI API docs: http://docs.travis-ci.com/api/#jobs
func (*JobsService) Find ¶
func (js *JobsService) Find(opt *JobFindOptions) ([]Job, *http.Response, error)
Find jobs using the provided options. You need to provide exactly one of the opt fields value. If you provide State or Queue, a maximum of 250 jobs will be returned.
Travis CI API docs: http://docs.travis-ci.com/api/#jobs
func (*JobsService) Get ¶
Get fetches job with the provided id.
Travis CI API docs: http://docs.travis-ci.com/api/#jobs
func (*JobsService) ListFromBuild ¶
ListByBuild retrieve a build jobs from its provided id.
Travis CI API docs: http://docs.travis-ci.com/api/#jobs
func (*JobsService) Restart ¶
func (js *JobsService) Restart(id uint) (*http.Response, error)
Restart job with the provided id.
Travis CI API docs: http://docs.travis-ci.com/api/#jobs
type ListOptions ¶
type ListOptions struct {
AfterNumber uint `url:"after_number,omitempty"`
}
func (*ListOptions) GetNextPage ¶
func (into *ListOptions) GetNextPage(from interface{}) error
GetNextPage provided a collection of resources (Builds or Jobs), will update the ListOptions to fetch the next resource page on next call.
type Log ¶
type Log struct { Id uint `json:"id,omitempty"` JobId uint `json:"job_id,omitempty"` Type string `json:"type,omitempty"` Body string `json:"body,omitempty"` }
Log represents a Travis CI job log
type LogsService ¶
type LogsService struct {
// contains filtered or unexported fields
}
LogssService handles communication with the logs related methods of the Travis CI API.
func (*LogsService) Get ¶
Get fetches a log based on the provided id.
Travis CI API docs: http://docs.travis-ci.com/api/#logs
func (*LogsService) GetByJob ¶
Get a job's log based on it's provided id.
Travis CI API docs: http://docs.travis-ci.com/api/#logs
type NotificationMap ¶
type RepositoriesService ¶
type RepositoriesService struct {
// contains filtered or unexported fields
}
RepositoriesService handles communication with the builds related methods of the Travis CI API.
func (*RepositoriesService) Find ¶
func (rs *RepositoriesService) Find(opt *RepositoryListOptions) ([]Repository, *http.Response, error)
Find repositories using the provided options. If no options are provided, a list of repositories with recent activity for the authenticated user is returned.
Travis CI API docs: http://docs.travis-ci.com/api/#repositories
func (*RepositoriesService) Get ¶
func (rs *RepositoriesService) Get(id uint) (*Repository, *http.Response, error)
Get fetches a repository based on the provided id.
Travis CI API docs: http://docs.travis-ci.com/api/#repositories
func (*RepositoriesService) GetFromSlug ¶
func (rs *RepositoriesService) GetFromSlug(slug string) (*Repository, *http.Response, error)
GetBySlug fetches a repository based on the provided slug.
Travis CI API docs: http://docs.travis-ci.com/api/#repositories
type Repository ¶
type Repository struct { Id uint `json:"id"` Slug string `json:"slug"` Description string `json:"description"` LastBuildId uint `json:"last_build_id"` LastBuildNumber string `json:"last_build_number"` LastBuildState string `json:"last_build_state"` LastBuildDuration uint `json:"last_build_duration"` LastBuildStartedAt string `json:"last_build_started_at"` LastBuildFinishedAt string `json:"last_build_finished_at"` GithubLanguage string `json:"github_language"` }
Repository represents a Travis CI repository
type RepositoryListOptions ¶
type RepositoryListOptions struct { // list of repository ids to fetch, cannot be combined with other parameters Ids []uint `url:"ids,omitempty"` // filter by user that has access to it (github login) Member string `url:"member,omitempty"` // filter by owner name (first segment of slug) OwnerName string `url:"owner_name,omitempty"` // filter by slug Slug string `url:"slug,omitempty"` // filter by search term Search string `url:"search,omitempty"` // if true, will only return repositories that are enabled Active bool `url:"active,omitempty"` }
RepositoryListOptions specifies the optional parameters to the RepositoriesService.Findmethod.
type Request ¶
type Request struct { Id uint `json:"id,omitempty"` RepositoryId uint `json:"repository_id,omitempty"` CommitId uint `json:"commit_id,omitempty"` CreatedAt string `json:"created_at,omitempty"` OwnerId uint `json:"owner_id"` OwnerType string `json:"owner_type,omitempty"` EventType string `json:"event_type,omitempty"` Result string `json:"result,omitempty"` PullRequest bool `json:"pull_request,omitempty"` Branch string `json:"branch,omitempty"` }
Request represents a Travis CI request. They can be used to see if and why a GitHub even has or has not triggered a new build.
type RequestsListOptions ¶
type RequestsListOptions struct { // repository id the requests belong to RepositoryId uint `url:"repository_id,omitempty"` // repository slug the requests belong to Slug string `url:"slug,omitempty"` // maximum number of requests to return (cannot be larger than 100) Limit uint `url:"limit,omitempty"` // list requests before older_than (with older_than being a request id) OlderThan uint `url:"older_than,omitempty"` }
RequestsListOptions specifies the optional parameters to the RequestsService.List method.
You have to either provide RepositoryId or Slug
type RequestsService ¶
type RequestsService struct {
// contains filtered or unexported fields
}
RequestsService handles communication with the requests related methods of the Travis CI API.
func (*RequestsService) Get ¶
Get fetches the request with the provided id from the Travis CI API.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
func (*RequestsService) ListFromRepository ¶
func (rs *RequestsService) ListFromRepository(slug string, opt *RequestsListOptions) ([]Request, []Commit, *http.Response, error)
List requests triggered (or not) by a repository's builds.
Travis CI API docs: http://docs.travis-ci.com/api/#builds
type User ¶
type User struct { Id uint `json:"id,omitempty"` Name string `json:"name,omitempty"` Login string `json:"commit_id,omitempty"` Email string `json:"email,omitempty"` GravatarId string `json:"gravatar_id,omitempty"` IsSyncing bool `json:"is_syncing,omitempty"` SyncedAt string `json:"synced_at,omitempty"` CorrectScopes bool `json:"correct_scopes,omitempty"` CreatedAt string `json:"created_at,omitempty"` }
User represents a Travis CI user.
type UsersService ¶
type UsersService struct {
// contains filtered or unexported fields
}
UsersService handles communication with the users related methods of the Travis CI API.
func (*UsersService) Get ¶
Get fetches the user with the provided id from the Travis CI API.
Travis CI API docs: http://docs.travis-ci.com/api/#users
func (*UsersService) GetAuthenticated ¶
func (us *UsersService) GetAuthenticated() (*User, *http.Response, error)
GetAuthenticated fetches the currently authenticated user from Travis CI API. This request always needs to be authenticated.
Travis CI API docs: http://docs.travis-ci.com/api/#users
func (*UsersService) Sync ¶
func (us *UsersService) Sync() (*http.Response, error)
Sync triggers a new sync with GitHub. Might return status 409 if the user is currently syncing. This request always needs to be authenticated.
Travis CI API docs: http://docs.travis-ci.com/api/#users