use client github auth token

Signed-off-by: Andrew Johnson <ajohnson@gocardless.com>
This commit is contained in:
Andrew Johnson
2021-03-26 16:46:47 +00:00
parent 6e09602f4e
commit d2c148b319
4 changed files with 57 additions and 36 deletions
+21 -11
View File
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createApiRef, DiscoveryApi } from '@backstage/core';
import { ConfigApi, createApiRef, OAuthApi } from '@backstage/core';
import { graphql } from '@octokit/graphql';
export type GithubDeployment = {
@@ -40,8 +40,8 @@ export const githubDeploymentsApiRef = createApiRef<GithubDeploymentsApi>({
});
export type Options = {
discoveryApi: DiscoveryApi;
proxyPath?: string;
configApi: ConfigApi;
githubAuthApi: OAuthApi;
};
const deploymentsQuery = `
@@ -63,14 +63,19 @@ query lastDeployments($owner: String!, $repo: String!, $last: Int) {
`;
export class GithubDeploymentsApiClient implements GithubDeploymentsApi {
private readonly discoveryApi: DiscoveryApi;
private readonly configApi: ConfigApi;
private readonly githubAuthApi: OAuthApi;
constructor(options: Options) {
this.discoveryApi = options.discoveryApi;
this.configApi = options.configApi;
this.githubAuthApi = options.githubAuthApi;
}
private async getProxyUrl() {
return await this.discoveryApi.getBaseUrl('proxy');
private getBaseUrl() {
const providerConfigs =
this.configApi.getOptionalConfigArray('integrations.github') ?? [];
const targetProviderConfig = providerConfigs[0];
return targetProviderConfig?.getOptionalString('apiBaseUrl');
}
async listDeployments(options: {
@@ -78,12 +83,17 @@ export class GithubDeploymentsApiClient implements GithubDeploymentsApi {
repo: string;
last: number;
}): Promise<GithubDeployment[]> {
const proxyUrl = await this.getProxyUrl();
const graphQlWithBaseURL = graphql.defaults({
baseUrl: `${proxyUrl}/github/api`,
const token = await this.githubAuthApi.getAccessToken(['repo']);
const baseUrl = this.getBaseUrl() || 'https://api.github.com';
const graphQLWithAuth = graphql.defaults({
baseUrl,
headers: {
authorization: `token ${token}`,
},
});
const response: any = await graphQlWithBaseURL(deploymentsQuery, options);
const response: any = await graphQLWithAuth(deploymentsQuery, options);
return response.repository?.deployments?.nodes?.reverse() || [];
}
}
@@ -21,6 +21,8 @@ import {
UrlPatternDiscovery,
configApiRef,
ConfigReader,
ConfigApi,
OAuthApi,
} from '@backstage/core';
import { render } from '@testing-library/react';
@@ -39,13 +41,20 @@ jest.mock('@backstage/plugin-catalog-react', () => ({
},
}));
const discoveryApi = UrlPatternDiscovery.compile('http://exampleapi.com');
const errorApiMock = { post: jest.fn(), error$: jest.fn() };
const configApi: ConfigApi = new ConfigReader({});
const githubAuthApi: OAuthApi = {
getAccessToken: async _ => 'access_token',
};
const apis = ApiRegistry.from([
[configApiRef, new ConfigReader({})],
[configApiRef, configApi],
[errorApiRef, errorApiMock],
[githubDeploymentsApiRef, new GithubDeploymentsApiClient({ discoveryApi })],
[
githubDeploymentsApiRef,
new GithubDeploymentsApiClient({ configApi, githubAuthApi }),
],
]);
describe('github-deployments', () => {
+5 -4
View File
@@ -14,10 +14,11 @@
* limitations under the License.
*/
import {
configApiRef,
createApiFactory,
createComponentExtension,
createPlugin,
discoveryApiRef,
githubAuthApiRef,
} from '@backstage/core';
import { githubDeploymentsApiRef, GithubDeploymentsApiClient } from './api';
@@ -26,9 +27,9 @@ export const githubDeploymentsPlugin = createPlugin({
apis: [
createApiFactory({
api: githubDeploymentsApiRef,
deps: { discoveryApi: discoveryApiRef },
factory: ({ discoveryApi }) =>
new GithubDeploymentsApiClient({ discoveryApi }),
deps: { configApi: configApiRef, githubAuthApi: githubAuthApiRef },
factory: ({ configApi, githubAuthApi }) =>
new GithubDeploymentsApiClient({ configApi, githubAuthApi }),
}),
],
});