Categorygithub.com/codeship/codeship-go
modulepackage
0.0.0-20201103162013-82059492d39f
Repository: https://github.com/codeship/codeship-go.git
Documentation: pkg.go.dev

# README

Codeship API v2 Client for Go

Codeship Status Coverage Status Go Doc Go Report Card GitHub Release

Codeship

Codeship API v2 client for Go.

Documentation

https://godoc.org/github.com/codeship/codeship-go

Usage

go get -u github.com/codeship/codeship-go

Package codeship provides a client for using the Codeship API v2.

import codeship "github.com/codeship/codeship-go"

Create a new API Client:

auth := codeship.NewBasicAuth("username", "password")
client, err := codeship.New(auth)

You must then scope the client to a single Organization that you have access to:

org, err := client.Organization(ctx, "codeship")

You can then perform calls to the API on behalf of an Organization:

projects, err := org.ListProjects(ctx)

Authentication

Authentication is handled automatically via the API Client using the provided authentication mechanism.

If you would like to manually re-authenticate, you may do this by calling the Authenticate method on the client:

err := client.Authenticate(ctx)

Two-Factor Authentication

Codeship now supports Two-Factor Authentication (2FA).

However, it is currently not possible to use 2FA with the API. If you try to authenticate via this client with a user that has 2FA enabled you will get the following error:

authentication failed: your account has two-factor authentication enabled, which is not possible to support with the API. Disable two factor authentication or create a dedicated API user without it enabled.

You must disable 2FA for the user you wish to authenticate with using this client. We hope to support Personal Access Tokens in a future version of the API to mitigate this issue.

Response

All API methods also return a codeship.Response type that contains the actual *http.Response embedded as well as a Links type that contains information to be used for pagination.

Pagination

Pagination is provided for all requests that can return multiple results. The methods that are able to be paginated all take a variable argument of type PaginationOption such as: ListProjects(opts ...PaginationOption).

We have defined two helper functions, Page and PerPage, to make pagination easier.

Usage is as follows:

// defaults to first page with page_size of 30
projects, resp, err := org.ListProjects(ctx)

// paging forwards with 50 results per page
for {
    if resp.IsLastPage() || resp.Next == "" {
        break
    }

    next, _ := resp.NextPage()

    projects, resp, _ = org.ListProjects(ctx, codeship.Page(next), codeship.PerPage(50))
}

// paging backwards with 50 results per page
for {
    if current, _ := resp.CurrentPage(); current == 1 || resp.Previous == "" {
        break
    }

    prev, _ := resp.PreviousPage()

    projects, resp, _ = org.ListProjects(ctx, codeship.Page(prev), codeship.PerPage(50))
}

Logging

You can enable verbose logging of all HTTP requests/responses by configuring the client via the functional option Verbose(verbose bool) when instantiating the client:

auth := codeship.NewBasicAuth("username", "password")
client, err := codeship.New(auth, codeship.Verbose(true))

Bring your own Logger

The default logger logs to STDOUT but can be replaced by any type that fulfills the StdLogger interface:

// StdLogger allows you to bring your own log implementation for logging
type StdLogger interface {
	Println(...interface{})
}

Example:

import "github.com/sirupsen/logrus"

var (
    logger = logrus.New()
    auth   = codeship.NewBasicAuth("username", "password")
)

client, err := codeship.New(auth, codeship.Verbose(true), codeship.Logger(logger))

Contributing

This project follows Codeship's Go best practices. Please review them and make sure your PR follows the guidelines laid out before submitting.

Everyone interacting in the project and its sub-projects' codebases, issue trackers, chat rooms, and mailing lists is expected to follow the Code of Conduct.

Setup

To install all dependencies and external tools, run:

make setup tools

Testing

make test

We aim for > 80% test coverage. You can view the current coverage info by running:

make cover

Linting

make lint

Other

$ make help

setup                          Install all dependencies
tools                          Install external tools
test                           Run all the tests
integration                    Run integration tests
cover                          Run all the tests and opens the coverage report
fmt                            goimports all go files
lint                           Run all the linters

# Packages

Package integration contains integration tests.

# Functions

BaseURL allows overriding of API client baseURL for testing.
Headers allows you to set custom HTTP headers when making API calls (e.g.
HTTPClient accepts a custom *http.Client for making API calls.
Logger allows overriding the default STDOUT logger.
New creates a new Codeship API client.
NewBasicAuth returns a new BasicAuth Authenticator.
Page sets the page of results to be returned in the response.
PerPage sets the number of results to be returned per page in the response.
Verbose allows enabling/disabling internal logging.

# Constants

ProjectTypeBasic represents a Codeship Basic project type.
ProjectTypePro represents a Codeship Pro project type.

# Variables

ErrRateLimitExceeded occurs when Codeship returns 403 Forbidden response.

# Structs

Authentication object holds access token and scope information.
BasicAuth is an Authenticator that implements basic auth.
Build structure of Build object.
BuildLinks structure of BuildLinks object for a Build.
BuildList holds a list of Build objects.
BuildPipeline structure of BuildPipeline object for a Basic Project.
BuildPipelineMetrics structure of BuildPipelineMetrics object for a BuildPipeline.
BuildPipelines holds a list of BuildPipeline objects for a Basic Project.
BuildService structure of BuildService object for a Pro Project.
BuildServices holds a list of BuildService objects for a Pro Project.
BuildStep structure of BuildStep object for a Pro Project.
BuildSteps holds a list of BuildStep objects for a Pro Project.
Client holds information necessary to make a request to the Codeship API.
DeploymentBranch structure for DeploymentBranch object for a Basic Project.
DeploymentPipeline structure for DeploymentPipeline object for a Basic Project.
EnvironmentVariable structure for EnvironmentVariable object for a Basic Project.
ErrBadRequest occurs when Codeship returns a 400 Bad Request response.
ErrNotFound occurs when Codeship returns a 404 Not Found response.
Links contain links for pagination purposes Codeship API docs: https://apidocs.codeship.com/v2/introduction/pagination.
NotificationOptions structure for NotificationOptions object for a Project.
NotificationRule structure for NotificationRule object for a Project.
Organization holds the configuration for the current API client scoped to the Organization.
Project structure for Project object.
ProjectCreateRequest structure for creating a Project.
ProjectList holds a list of Project objects.
ProjectUpdateRequest structure for updating a Project.
Response is a Codeship response.
TestPipeline structure for Project object.

# Interfaces

Authenticator is a strategy for authenticating with the API.
StdLogger allows you to bring your own log implementation for logging.

# Type aliases

ErrUnauthorized occurs when Codeship returns a 401 Unauthorized response.
Option is a functional option for configuring the API client.
PaginationOption is a functional option for providing pagination options.
ProjectType represents Codeship project types (Basic and Pro).