This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #5 * feat: add buildkite support for env vars for #5 * include buildkite in provider index * add simple url test * test update environment variables on buildkite * describe supported methods in readme
- Loading branch information
Showing
8 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports['Buildkite API wrapper pipeline url forms pipeline url 1'] = ` | ||
/organizations/org/pipelines/project | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
axios = require("axios") | ||
R = require("ramda") | ||
la = require("lazy-ass") | ||
check = require("check-more-types") | ||
debug = require("debug")("bumper") | ||
|
||
toData = R.prop("data") | ||
env = R.prop("env") | ||
|
||
buildkiteApiUrl = "https://api.buildkite.com/v2" | ||
|
||
getPipelineUrl = (organization, project) -> | ||
la(check.unemptyString(organization), "missing organization", organization) | ||
la(check.unemptyString(project), "missing project", project) | ||
"/organizations/#{organization}/pipelines/#{project}" | ||
|
||
module.exports = { | ||
url: buildkiteApiUrl | ||
|
||
create: (token) -> | ||
throw new Error("Buildkite requires an access token!") if !token | ||
|
||
api = axios.create({ | ||
baseURL: buildkiteApiUrl | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': "Bearer #{token}" | ||
} | ||
}) | ||
|
||
# returns merged variable object from pipeline | ||
setEnvironmentVariables = (organization, project, variables = {}) -> | ||
url = getPipelineUrl(organization, project) | ||
api.patch(url, { | ||
env: variables | ||
}) | ||
.then toData | ||
.then env | ||
|
||
## public API | ||
api.getPipelineUrl = getPipelineUrl | ||
|
||
api.getPipeline = (organization, project) -> | ||
url = getPipelineUrl(organization, project) | ||
debug("getting pipeline", url) | ||
api.get(url) | ||
.then toData | ||
|
||
api.setPipeline = (organization, project, pipeline) -> | ||
la(check.object(pipeline), "missing pipeline to set", pipeline) | ||
debug("setting pipeline for", organization, project) | ||
url = getPipelineUrl(organization, project) | ||
api.post(url, pipeline) | ||
.then toData | ||
|
||
api.updateEnvironmentVariables = (organization, project, variables) -> | ||
api.getPipeline(organization, project) | ||
.then env | ||
.then (oldVariables) -> | ||
merged = R.merge(oldVariables, variables) | ||
setEnvironmentVariables(organization, project, merged) | ||
|
||
api.triggerNewBuild = (organization, project) -> | ||
throw new Error "Not implemented for Buildkite yet" | ||
# https://buildkite.com/docs/rest-api/builds#create-a-build | ||
|
||
return api | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
_ = require("lodash") | ||
Promise = require("bluebird") | ||
providerApi = require("./buildkite-api") | ||
debug = require("debug")("bumper") | ||
|
||
api = | ||
configure: (options={}) -> | ||
api = providerApi.create(options.buildkiteToken) | ||
|
||
return { | ||
updateProjectEnv: (organization, project, variables) -> | ||
debug("updating Buildkite variables for", organization, project) | ||
debug(variables) | ||
api.updateEnvironmentVariables(organization, project, variables) | ||
|
||
runProject: (organization, project) -> | ||
api.triggerNewBuild(organization, project) | ||
} | ||
|
||
module.exports = api | ||
|
||
if !module.parent | ||
console.log("Buildkite demo") | ||
if !process.env.support__ci_json | ||
throw new Error("Missing support__ci_json in environment") | ||
tokens = JSON.parse(process.env.support__ci_json) | ||
ci = api.configure(tokens) | ||
ci.updateProjectEnv("cypress-io", "bumpercar-test", { | ||
foo: "foo" | ||
}).catch(console.error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require("./spec_helper") | ||
|
||
buildkiteApi = require("../lib/providers/buildkite-api") | ||
snapshot = require("snap-shot-it") | ||
la = require("lazy-ass") | ||
check = require("check-more-types") | ||
R = require("ramda") | ||
nock = require('nock') | ||
|
||
describe "Buildkite API wrapper", -> | ||
it "has API base url", -> | ||
la(check.url(buildkiteApi.url)) | ||
|
||
context "pipeline url", -> | ||
api = buildkiteApi.create('fake-test-token') | ||
|
||
it "forms pipeline url", -> | ||
url = api.getPipelineUrl('org', 'project') | ||
snapshot(url) | ||
|
||
context "update environment variables", -> | ||
api = buildkiteApi.create('fake-test-token') | ||
organization = "foo" | ||
project = "p1" | ||
oldVariables = { | ||
key: "value", | ||
life: 20 | ||
} | ||
pipeline = { | ||
env: oldVariables | ||
} | ||
|
||
beforeEach () -> | ||
pipelineUrl = api.getPipelineUrl(organization, project) | ||
|
||
nock(buildkiteApi.url) | ||
.get(pipelineUrl) | ||
.reply(200, pipeline) | ||
|
||
nock(buildkiteApi.url) | ||
.patch(pipelineUrl) | ||
.reply(200, (uri, requestBody, cb) -> | ||
pipeline.env = R.merge(pipeline.env, requestBody.env) | ||
cb(null, [200, pipeline]) | ||
) | ||
|
||
it "is a function", -> | ||
la(check.fn(api.updateEnvironmentVariables)) | ||
|
||
it "updates pipeline variables", -> | ||
newVariables = { life: 42 } | ||
api.updateEnvironmentVariables(organization, project, newVariables) | ||
.then (allVariables) -> | ||
expected = R.merge(oldVariables, newVariables) | ||
la(R.equals(allVariables, expected), allVariables, "expected", expected) |