From 37fb63ae2b2c14be68f1dbdd5bef1354dfbdf0b2 Mon Sep 17 00:00:00 2001 From: mo Date: Fri, 29 Apr 2022 06:54:54 -0500 Subject: [PATCH] useGitTags Signed-off-by: mo --- .../components/GitTagTable/GitTagTable.tsx | 4 +- plugins/azure-devops/src/hooks/useGitTags.ts | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 plugins/azure-devops/src/hooks/useGitTags.ts diff --git a/plugins/azure-devops/src/components/GitTagTable/GitTagTable.tsx b/plugins/azure-devops/src/components/GitTagTable/GitTagTable.tsx index 9d7d7db07f..7b4929bfb9 100644 --- a/plugins/azure-devops/src/components/GitTagTable/GitTagTable.tsx +++ b/plugins/azure-devops/src/components/GitTagTable/GitTagTable.tsx @@ -31,7 +31,7 @@ import { AzurePullRequestsIcon } from '../AzurePullRequestsIcon'; import { DateTime } from 'luxon'; import { PullRequestStatusButtonGroup } from '../PullRequestStatusButtonGroup'; import { useEntity } from '@backstage/plugin-catalog-react'; -import { usePullRequests } from '../../hooks/usePullRequests'; +import { useGitTags } from '../../hooks/useGitTags'; const columns: TableColumn[] = [ { @@ -96,7 +96,7 @@ export const GitTagTable = ({ defaultLimit }: PullRequestTableProps) => { useState(PullRequestStatus.Active); const { entity } = useEntity(); - const { items, loading, error } = usePullRequests( + const { items, loading, error } = useGitTags( entity, defaultLimit, pullRequestStatusState, diff --git a/plugins/azure-devops/src/hooks/useGitTags.ts b/plugins/azure-devops/src/hooks/useGitTags.ts new file mode 100644 index 0000000000..b55012c7f0 --- /dev/null +++ b/plugins/azure-devops/src/hooks/useGitTags.ts @@ -0,0 +1,58 @@ +/* + * Copyright 2021 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 { + PullRequest, + PullRequestOptions, + PullRequestStatus, +} from '@backstage/plugin-azure-devops-common'; + +import { AZURE_DEVOPS_DEFAULT_TOP } from '../constants'; +import { Entity } from '@backstage/catalog-model'; +import { azureDevOpsApiRef } from '../api'; +import { useApi } from '@backstage/core-plugin-api'; +import useAsync from 'react-use/lib/useAsync'; +import { useProjectRepoFromEntity } from './useProjectRepoFromEntity'; + +export function useGitTags( + entity: Entity, + defaultLimit?: number, + requestedStatus?: PullRequestStatus, +): { + items?: PullRequest[]; + loading: boolean; + error?: Error; +} { + const top = defaultLimit ?? AZURE_DEVOPS_DEFAULT_TOP; + const status = requestedStatus ?? PullRequestStatus.Active; + const options: PullRequestOptions = { + top, + status, + }; + + const api = useApi(azureDevOpsApiRef); + const { project, repo } = useProjectRepoFromEntity(entity); + + const { value, loading, error } = useAsync(() => { + return api.getPullRequests(project, repo, options); + }, [api, project, repo, top, status]); + + return { + items: value?.items, + loading, + error, + }; +}