UNPKG

@roadiehq/backstage-plugin-github-pull-requests

Version:
112 lines (109 loc) 2.61 kB
import { useState, useEffect } from 'react'; import { useAsyncRetry } from 'react-use'; import { githubPullRequestsApiRef } from '../api/GithubPullRequestsApi.esm.js'; import '@backstage/integration'; import '@octokit/rest'; import { DateTime } from 'luxon'; import { useApi } from '@backstage/core-plugin-api'; import { useEntity } from '@backstage/plugin-catalog-react'; import { getHostname } from '../utils/githubUtils.esm.js'; function usePullRequests({ search, owner, repo, branch }) { const api = useApi(githubPullRequestsApiRef); const { entity } = useEntity(); const hostname = getHostname(entity); const [total, setTotal] = useState(0); const [totalResults, setTotalResults] = useState(0); const [page, setPage] = useState(0); const [pageSize, setPageSize] = useState(5); const getElapsedTime = (start) => { return DateTime.fromISO(start).toRelative(); }; const handleTotal = (total_count) => { setTotal(total_count > 1e3 ? 1e3 : total_count); setTotalResults(total_count); }; const { loading, value: prData, retry, error } = useAsyncRetry(async () => { if (!repo) { return []; } return api.listPullRequests({ search, owner, repo, pageSize, page: page + 1, branch, hostname }).then( ({ pullRequestsData: { total_count, items } }) => { if (total_count >= 0) { handleTotal(total_count); } return items.map( ({ id, html_url, title, number, created_at, updated_at, user, state: pr_state, draft, pull_request: { merged_at }, body }) => ({ url: html_url, id, number, title, body, state: pr_state, draft, merged: merged_at, creatorNickname: user?.login, creatorProfileLink: user?.html_url, createdTime: getElapsedTime(created_at), updatedTime: getElapsedTime(updated_at) }) ); } ); }, [page, pageSize, repo, owner]); useEffect(() => { setPage(0); retry(); }, [search]); return [ { page, pageSize, loading, prData, projectName: `${owner}/${repo}`, total, totalResults, error }, { prData, setPage, setPageSize, retry } ]; } export { usePullRequests }; //# sourceMappingURL=usePullRequests.esm.js.map