Services-js
Client Library to communicate with different Codeship Services for internal usage.
Contents
Run the specs
jest
Generate code coverage
yarn coverage
Run it in the browser
yarn start
API
Builds Service
Methods
getBuildGroupById(buildGroupId)
Returns a build group by a build group id.
getBuildGroupsByProjectIds(projectIds[, params])
Returns a list of build groups given an array of project ids. Optional parameters may be passed in the second argument:
user_id
: allows you to filter the build groups by a user idpage_token
: the specific page to loadlimit
: the number of results
getBuildById(projectId, buildId)
Returns a build given a project id and a build id.
Usage
import BuildsService from '@cloudbees/codeship-services-js/builds'
const token = 12345
const baseUrl = 'http://example.com'
const buildsService = new BuildsService(token, baseUrl)
const projectIds = [
'0076ff3b-f97d-41d1-9bdf-b810b3135d0d',
'ac1e852c-7ecc-40a7-86ea-374d4801f73e',
'46058f72-0770-406a-80fd-8d0ed27c9de6'
]
const params = {
user_id: 'd5a032cd-f26b-4c96-8831-0e6a26e5fc9d'
}
try {
const { data: buildGroups } = await buildsService.getBuildGroupsByProjectIds(projectIds, params)
console.log(buildGroups)
} catch (error) {
console.error(error)
}
Log Service
Methods
subscribe(buildId, projectId, component[, dataCallback, [ closeCallback]])
Starts a log stream given a build id, project id, and a component. The callback is optional, but it returns two arguments respectively:
error
: a server error, if it existsresponse
: the server response
Additionally, the closeCallback
is optional, and it provides the close event as it's only argument.
Only one stream may be subscribed to per class. All previous streams will be closed when the subscribe
method is called.
Usage
import LogService from '@cloudbees/codeship-services-js/logs'
const token = 12345
const baseUrl = 'http://example.com'
const logService = new LogService(token, baseUrl)
const buildId = '56129bd7-3708-45fb-bfcb-422aef2e8bfb'
const projectId = 'a3d49ed2-c8e8-42df-8983-bdf115a4ea97'
logService.subscribe(buildId, projectId, 'default', (error, response) => {
if (!error) {
console.log(response)
}
})
Mocks
The mocks package exposes mock services and functions that return random sets of mock data.
Mock Builds Service
Usage
import { MockBuildService } from '@cloudbees/codeship-services-js/mock'
try {
const mockBuildService = new MockBuildService()
const id = 12345
const { data } = await mockBuildService.getBuildGroupById(id)
const { pagination, build_groups: buildGroups } = data
console.log(pagination, buildGroups)
} catch (error) {
console.error(error)
}
Options
An options
object can be passed in as the second parameter in any mock API call.
import { MockBuildService } from '@cloudbees/codeship-services-js/mock'
const mockBuildService = new MockBuildService()
const buildGroupId = 12345
const options = {
buildGroupCount: 5
}
const { data } = await mockBuildService.getBuildsByBuildGroupId(buildGroupId, options)
console.log(data)
Functions
Returns a mock object. Each function takes an object as its only parameter, allowing for the overwriting of specified values.
Available Mocks
- Classes
new MockLogService(token, baseUrl)
new MockBuildService(token, baseUrl)
- Services
mockService([params])
serviceConclusions[]
serviceStatuses[]
- Steps
mockStep([params])
mockStepTraits([params])
stepStatuses[]
serviceGroupTypes[]
stepTypes[]
stepConclusions[]
- Build Groups
mockBuildGroup([params])
mockCommit([params])
repositoryHostTypes[]
buildGroupTypes[]
- Builds
mockBuild([params])
mockRunningBuild([params])
mockStoppedBuild([params])
mockApprovableBuild([params])
mockBuildEvent([params])
mockBuildResponse([params])
buildStatuses[]
buildConclusions[]
- Pagination
- Logs
mockLogResponse([params])
mockLog([params])
mockLogContext([params])
Usage
import { mockBuild } from '@cloudbees/codeship-services-js/mock'
const id = '4bbaf6e7-6729-4168-a5de-0a7ecdd370ae'
const build = mockBuild({ id })
Command Line
In order to access the provided cli implementation you can use node
to access it directly like this:
node bin/local
If you prefer to access the script from anywhere by name
please run the npm link
command first. This will create a global reference and make it available under services-js
npm link
services-js
Usage
To get a list of commands:
services-js --help
To pass in a token, baseUrl, and a function with arguments:
services-js --token 12345 --baseUrl http://localhost:8080 getBuildGroupById 5
To start a log stream:
services-js -u http://127.0.0.1:8080 -v -t $TOKEN logSubscribe $BUILD_ID $PROJECT_ID service_test
Make sure to pass in the protocol (http/https)!
Decision Archive
Question [08/16/2018]: Why was @babel/register
used for the local CLI, but a bundle is generated for the global CLI?
Answer: @babel/register
has issues applying runtime transformations to global modules because it's technically in a node_modules
folder. To get around this, a bundle is generated and the CLI tool is transpiled to es5. On the other hand, the babel runtimes speed up local development.
Question [08/27/2018]: Why aren't type the declarations split into multiple entry points like the various service classes?
Answer: Typescript + Webpack doesn't lend itself well to multiple type outputs, so that's why there's a root delcaration file.
Question [02/04/2019]: Why is the default axios adapter changed in the specs?
Answer: Axios creates a preflight OPTIONS request which causes specs to fail.