feat: github actions api type with a mock implementation

This commit is contained in:
Nikita Nek Dudnik
2020-07-10 09:24:32 +02:00
parent eed37376fc
commit 09dfdbad5c
12 changed files with 98 additions and 22 deletions
@@ -0,0 +1,28 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createApiRef } from '@backstage/core';
import { Build, BuildDetails } from './types';
export const githubActionsApiRef = createApiRef<GithubActionsApi>({
id: 'plugin.githubactions.service',
description: 'Used by the Github Actions plugin to make requests',
});
export type GithubActionsApi = {
listBuilds: (_entityUri: string) => Promise<Build[]>;
getBuild: (_buildUri: string) => Promise<BuildDetails>;
};
@@ -14,20 +14,16 @@
* limitations under the License.
*/
import { GithubActionsApi } from './GithubActionsApi';
import { Build, BuildDetails, BuildStatus } from './types';
export class BuildsClient {
static create(): BuildsClient {
return new BuildsClient();
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async listBuilds(_entityUri: string): Promise<Build[]> {
export class MockGithubActionsClient implements GithubActionsApi {
async listBuilds(): Promise<Build[]> {
return [];
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getBuild(_buildUri: string): Promise<BuildDetails> {
async getBuild(): Promise<BuildDetails> {
return {
build: {
commitId: 'TODO',
@@ -0,0 +1,44 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { MockGithubActionsClient } from './MockGithubActionsClient';
import { BuildStatus } from './types';
describe('Github Actions API', () => {
let client: MockGithubActionsClient;
beforeEach(() => {
client = new MockGithubActionsClient();
});
describe('Mock client', () => {
it('gets a list of builds by a project id', async () => {
await expect(client.listBuilds()).resolves.toEqual([]);
});
it('gets a build info by its id', async () => {
await expect(client.getBuild()).resolves.toEqual({
build: {
commitId: 'TODO',
branch: 'TODO',
uri: 'TODO',
status: BuildStatus.Running,
message: 'TODO',
},
author: 'TODO',
logUrl: 'TODO',
overviewUrl: 'TODO',
});
});
});
});
@@ -14,5 +14,6 @@
* limitations under the License.
*/
export { BuildsClient } from './BuildsClient';
export * from './GithubActionsApi';
export * from './MockGithubActionsClient';
export * from './types';
@@ -32,8 +32,9 @@ import {
import React from 'react';
import { useParams } from 'react-router-dom';
import { useAsync } from 'react-use';
import { BuildsClient } from '../../apis/builds';
import { BuildStatusIndicator } from '../BuildStatusIndicator';
import { useApi } from '@backstage/core-api';
import { githubActionsApiRef } from '../../api';
const useStyles = makeStyles<Theme>(theme => ({
root: {
@@ -48,12 +49,11 @@ const useStyles = makeStyles<Theme>(theme => ({
},
}));
const client = BuildsClient.create();
export const BuildDetailsPage = () => {
const api = useApi(githubActionsApiRef);
const classes = useStyles();
const { buildUri } = useParams();
const status = useAsync(() => client.getBuild(buildUri), [buildUri]);
const status = useAsync(() => api.getBuild(buildUri), [buildUri]);
if (status.loading) {
return <LinearProgress />;
@@ -27,10 +27,9 @@ import {
} from '@material-ui/core';
import React from 'react';
import { useAsync } from 'react-use';
import { BuildsClient } from '../../apis/builds';
import { BuildStatusIndicator } from '../BuildStatusIndicator';
const client = BuildsClient.create();
import { githubActionsApiRef } from '../../api';
import { useApi } from '@backstage/core-api';
const useStyles = makeStyles<Theme>(theme => ({
root: {
@@ -43,7 +42,8 @@ const useStyles = makeStyles<Theme>(theme => ({
export const BuildInfoCard = () => {
const classes = useStyles();
const status = useAsync(() => client.listBuilds('entity:spotify:backstage'));
const api = useApi(githubActionsApiRef);
const status = useAsync(() => api.listBuilds('entity:spotify:backstage'));
let content: JSX.Element;
@@ -31,10 +31,9 @@ import {
} from '@material-ui/core';
import React from 'react';
import { useAsync } from 'react-use';
import { BuildsClient } from '../../apis/builds';
import { BuildStatusIndicator } from '../BuildStatusIndicator';
const client = BuildsClient.create();
import { githubActionsApiRef } from '../../api';
import { useApi } from '@backstage/core-api';
const LongText = ({ text, max }: { text: string; max: number }) => {
if (text.length < max) {
@@ -57,8 +56,9 @@ const useStyles = makeStyles<Theme>(theme => ({
}));
const PageContents = () => {
const api = useApi(githubActionsApiRef);
const { loading, error, value } = useAsync(() =>
client.listBuilds('entity:spotify:backstage'),
api.listBuilds('entity:spotify:backstage'),
);
if (loading) {
@@ -21,7 +21,7 @@ import SuccessIcon from '@material-ui/icons/CheckCircle';
import FailureIcon from '@material-ui/icons/Error';
import UnknownIcon from '@material-ui/icons/Help';
import React from 'react';
import { BuildStatus } from '../../apis/builds';
import { BuildStatus } from '../../api/types';
type Props = {
status?: BuildStatus;
+1
View File
@@ -15,3 +15,4 @@
*/
export { plugin } from './plugin';
export * from './api';