import { useAsync } from 'react-use' ; import { Octokit } from '@octokit/rest' ; import { Entity } from '@backstage/catalog-model' ; import { useApi , githubAuthApiRef } from '@backstage/core-plugin-api' ; import { useProjectEntity } from './useProjectEntity' ; import { useEntityGithubScmIntegration } from './useEntityGithubScmIntegration' ; export const useRequest = ( entity , requestName , perPage = 0 , maxResults = 0 , showTotal = false ) => { const auth = useApi ( githubAuthApiRef ) ; const { baseUrl } = useEntityGithubScmIntegration ( entity ) ; const { owner , repo } = useProjectEntity ( entity ) ; const { value , loading , error } = useAsync ( async ( ) : Promise < any > => { const token = await auth . getAccessToken ( [ 'repo' ] ) ; const octokit = new Octokit ( { auth : token } ) ; const response = await octokit . request ( `GET /repos/{owner}/{repo}/${ requestName }` , { baseUrl , owner , repo , ... ( perPage && { per_page : perPage } ) , } ) ; const data = response . data ; if ( showTotal ) { if ( Object . values ( data ) . length === 0 ) return null ; return { data , total : Object . values ( data as Record < string , number > ) . reduce ( ( a , b ) => a + b ) , } ; } return maxResults ? data . slice ( 0 , maxResults ) : data ; } , [ baseUrl ] ) ; return { value , loading , error , } ; } ;