import { createApiRef , DiscoveryApi } from '@backstage/core-plugin-api' ; export const buildKiteApiRef = createApiRef < BuildkiteApi > ( { id : 'plugin.buildkite.service' , } ) ; const DEFAULT_PROXY_PATH = '/buildkite/api' ; type Options = { discoveryApi : DiscoveryApi ; /**
     * Path to use for requests via the proxy, defaults to /buildkite/api
     */ proxyPath ? : string ; } ; export class BuildkiteApi { private readonly discoveryApi : DiscoveryApi ; private readonly proxyPath : string ; constructor ( options ) { this . discoveryApi = options . discoveryApi ; this . proxyPath = options . proxyPath ?? DEFAULT_PROXY_PATH ; } private async getApiUrl ( ) { const proxyUrl = await this . discoveryApi . getBaseUrl ( 'proxy' ) ; return `${ proxyUrl }${ this . proxyPath }` ; } async getBuilds ( orgSlug , pipelineSlug , page , per_page ) { const ApiUrl = await this . getApiUrl ( ) ; const request = await fetch ( `${ ApiUrl }/organizations/${ orgSlug }/pipelines/${ pipelineSlug }/builds?page=${ page }&per_page=${ per_page }` ) ; if ( ! request . ok ) { throw new Error ( `failed to fetch data, status ${ request . status }: ${ request . statusText }` ) ; } return request . json ( ) ; } async restartBuild ( requestUrl ) { const ApiUrl = await this . getApiUrl ( ) ; const request = await fetch ( `${ ApiUrl }/${ requestUrl }/rebuild` , { method : 'PUT' , } ) ; if ( ! request . ok ) { throw new Error ( `failed to fetch data, status ${ request . status }: ${ request . statusText }` ) ; } return request . json ( ) ; } async getSingleBuild ( orgSlug , pipelineSlug , buildNumber ) { const ApiUrl = await this . getApiUrl ( ) ; const request = await fetch ( `${ ApiUrl }/organizations/${ orgSlug }/pipelines/${ pipelineSlug }/builds/${ buildNumber }` ) ; if ( ! request . ok ) { throw new Error ( `failed to fetch data, status ${ request . status }: ${ request . statusText }` ) ; } return request . json ( ) ; } async getLog ( url ) { const ApiUrl = await this . getApiUrl ( ) ; const request = await fetch ( `${ ApiUrl }/${ url }` ) ; if ( ! request . ok ) { throw new Error ( `failed to fetch data, status ${ request . status }: ${ request . statusText }` ) ; } return request . json ( ) ; } }