Cloud build plugin list results by entity metatadata name or annotations

Signed-off-by: PurseChicken <chris@tschantz.name>
This commit is contained in:
PurseChicken
2023-11-17 12:07:40 -08:00
parent bffbaeb27a
commit e6a9b2dead
7 changed files with 60 additions and 3 deletions
+2
View File
@@ -58,6 +58,7 @@ export const CLOUDBUILD_ANNOTATION = 'google.com/cloudbuild-project-slug';
export type CloudbuildApi = {
listWorkflowRuns: (options: {
projectId: string;
cloudBuildFilter: string;
}) => Promise<ActionsListWorkflowRunsForRepoResponseData>;
getWorkflow: (options: {
projectId: string;
@@ -94,6 +95,7 @@ export class CloudbuildClient implements CloudbuildApi {
// (undocumented)
listWorkflowRuns(options: {
projectId: string;
cloudBuildFilter: string;
}): Promise<ActionsListWorkflowRunsForRepoResponseData>;
// (undocumented)
reRunWorkflow(options: { projectId: string; runId: string }): Promise<void>;
@@ -29,6 +29,7 @@ export const cloudbuildApiRef = createApiRef<CloudbuildApi>({
export type CloudbuildApi = {
listWorkflowRuns: (options: {
projectId: string;
cloudBuildFilter: string;
}) => Promise<ActionsListWorkflowRunsForRepoResponseData>;
getWorkflow: (options: {
projectId: string;
@@ -45,11 +45,12 @@ export class CloudbuildClient implements CloudbuildApi {
async listWorkflowRuns(options: {
projectId: string;
cloudBuildFilter: string;
}): Promise<ActionsListWorkflowRunsForRepoResponseData> {
const workflowRuns = await fetch(
`https://cloudbuild.googleapis.com/v1/projects/${encodeURIComponent(
options.projectId,
)}/builds`,
)}/builds?filter=${encodeURIComponent(options.cloudBuildFilter)}`,
{
headers: new Headers({
Accept: '*/*',
@@ -22,6 +22,7 @@ import { WorkflowRunStatus } from '../WorkflowRunStatus';
import { Theme, makeStyles, LinearProgress } from '@material-ui/core';
import ExternalLinkIcon from '@material-ui/icons/Launch';
import { CLOUDBUILD_ANNOTATION } from '../useProjectName';
import { getCloudbuildFilter } from '../useCloudBuildFilter';
import {
InfoCard,
@@ -79,9 +80,11 @@ export const LatestWorkflowRunCard = (props: { branch: string }) => {
const { entity } = useEntity();
const errorApi = useApi(errorApiRef);
const projectId = entity?.metadata.annotations?.[CLOUDBUILD_ANNOTATION] || '';
const cloudBuildFilter = getCloudbuildFilter(entity);
const [{ runs, loading, error }] = useWorkflowRuns({
projectId,
cloudBuildFilter,
});
const lastRun = runs?.[0] ?? ({} as WorkflowRun);
useEffect(() => {
@@ -26,6 +26,7 @@ import { buildRouteRef } from '../../routes';
import { DateTime } from 'luxon';
import { Table, TableColumn, Link } from '@backstage/core-components';
import { useRouteRef } from '@backstage/core-plugin-api';
import { getCloudbuildFilter } from '../useCloudBuildFilter';
const generatedColumns: TableColumn[] = [
{
@@ -162,9 +163,11 @@ export const WorkflowRunsTableView = ({
export const WorkflowRunsTable = (props: { entity: Entity }) => {
const { value: projectName, loading } = useProjectName(props.entity);
const [projectId] = (projectName ?? '/').split('/');
const cloudBuildFilter = getCloudbuildFilter(props.entity);
const [tableProps, { retry, setPage, setPageSize }] = useWorkflowRuns({
projectId,
cloudBuildFilter,
});
return (
@@ -0,0 +1,43 @@
/*
* Copyright 2020 The Backstage Authors
*
* 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 { Entity } from '@backstage/catalog-model';
const CLOUDBUILD_FILTER_REPO_STRING = 'substitutions.REPO_NAME=';
const CLOUDBUILD_FILTER_REPO_ANNOTATION = 'google.com/cloudbuild-repo-name';
const CLOUDBUILD_FILTER_TRIGGER_STRING = 'substitutions.TRIGGER_NAME=';
const CLOUDBUILD_FILTER_TRIGGER_ANNOTATION =
'google.com/cloudbuild-trigger-name';
/** @public */
export const getCloudbuildFilter = (entity: Entity) => {
const repoAnnotation =
entity?.metadata.annotations?.[CLOUDBUILD_FILTER_REPO_ANNOTATION] ?? '';
const triggerAnnotation =
entity?.metadata.annotations?.[CLOUDBUILD_FILTER_TRIGGER_ANNOTATION] ?? '';
if (repoAnnotation) {
const cloudbuildFilter = CLOUDBUILD_FILTER_REPO_STRING + repoAnnotation;
return cloudbuildFilter;
} else if (triggerAnnotation) {
const cloudbuildFilter =
CLOUDBUILD_FILTER_TRIGGER_STRING + triggerAnnotation;
return cloudbuildFilter;
}
const entityName = entity?.metadata.name ?? '';
const cloudbuildFilter = CLOUDBUILD_FILTER_REPO_STRING + entityName;
return cloudbuildFilter;
};
@@ -33,8 +33,11 @@ export type WorkflowRun = {
rerun: () => void;
};
export function useWorkflowRuns(options: { projectId: string }) {
const { projectId } = options;
export function useWorkflowRuns(options: {
projectId: string;
cloudBuildFilter: string;
}) {
const { projectId, cloudBuildFilter } = options;
const api = useApi(cloudbuildApiRef);
const errorApi = useApi(errorApiRef);
@@ -51,6 +54,7 @@ export function useWorkflowRuns(options: { projectId: string }) {
return api
.listWorkflowRuns({
projectId,
cloudBuildFilter,
})
.then(
(