@roadiehq/backstage-plugin-travis-ci
Version:
72 lines (69 loc) • 2.19 kB
JavaScript
import { createApiRef } from '@backstage/core-plugin-api';
const travisCIApiRef = createApiRef({
id: "plugin.travisci.service"
});
class TravisCIApiClient {
baseUrl;
discoveryApi;
identityApi;
constructor(options) {
this.baseUrl = "/travisci/api";
this.discoveryApi = options.discoveryApi;
this.identityApi = options.identityApi;
}
async retry(buildNumber) {
const { token } = await this.identityApi.getCredentials();
return fetch(`${await this.getApiUrl()}/build/${buildNumber}/restart`, {
method: "post",
headers: token ? new Headers({
"Travis-API-Version": "3",
Authorization: `Bearer ${token}`
}) : void 0
});
}
async getBuilds({ limit = 10, offset = 0, repoSlug }) {
const { token } = await this.identityApi.getCredentials();
const response = await fetch(
`${await this.getApiUrl()}/repo/${encodeURIComponent(
repoSlug
)}/builds?offset=${offset}&limit=${limit}`,
{
headers: token ? new Headers({
"Travis-API-Version": "3",
Authorization: `Bearer ${token}`
}) : void 0
}
);
if (!response.ok) {
throw new Error(
`error while fetching travis builds: ${response.status}: ${response.statusText}`
);
}
return (await response.json()).builds;
}
async getUser() {
const { token } = await this.identityApi.getCredentials();
return await (await fetch(`${await this.getApiUrl()}/user`, {
headers: token ? new Headers({
"Travis-API-Version": "3",
Authorization: `Bearer ${token}`
}) : void 0
})).json();
}
async getBuild(buildId) {
const { token } = await this.identityApi.getCredentials();
const response = await fetch(`${await this.getApiUrl()}/build/${buildId}`, {
headers: token ? new Headers({
"Travis-API-Version": "3",
Authorization: `Bearer ${token}`
}) : void 0
});
return response.json();
}
async getApiUrl() {
const proxyUrl = await this.discoveryApi.getBaseUrl("proxy");
return proxyUrl + this.baseUrl;
}
}
export { TravisCIApiClient, travisCIApiRef };
//# sourceMappingURL=index.esm.js.map