diff --git a/.changeset/heavy-terms-vanish.md b/.changeset/heavy-terms-vanish.md new file mode 100644 index 0000000000..e7f7303e59 --- /dev/null +++ b/.changeset/heavy-terms-vanish.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-github-actions': patch +--- + +Add missing token on job list call to Github API diff --git a/packages/app/src/components/catalog/EntityPage.test.tsx b/packages/app/src/components/catalog/EntityPage.test.tsx index 9860792ea0..c33adf6c4a 100644 --- a/packages/app/src/components/catalog/EntityPage.test.tsx +++ b/packages/app/src/components/catalog/EntityPage.test.tsx @@ -44,6 +44,7 @@ describe('EntityPage Test', () => { getWorkflow: jest.fn(), getWorkflowRun: jest.fn(), reRunWorkflow: jest.fn(), + listJobsForWorkflowRun: jest.fn(), downloadJobLogsForWorkflowRun: jest.fn(), } as jest.Mocked; diff --git a/plugins/github-actions/src/api/GithubActionsApi.ts b/plugins/github-actions/src/api/GithubActionsApi.ts index 637bb23216..1a3a5ed562 100644 --- a/plugins/github-actions/src/api/GithubActionsApi.ts +++ b/plugins/github-actions/src/api/GithubActionsApi.ts @@ -77,6 +77,23 @@ export type GithubActionsApi = { repo: string; runId: number; }) => Promise; + listJobsForWorkflowRun: ({ + hostname, + owner, + repo, + id, + pageSize, + page, + }: { + hostname?: string; + owner: string; + repo: string; + id: number; + pageSize?: number; + page?: number; + }) => Promise< + RestEndpointMethodTypes['actions']['listJobsForWorkflowRun']['response']['data'] + >; downloadJobLogsForWorkflowRun: ({ hostname, owner, diff --git a/plugins/github-actions/src/api/GithubActionsClient.ts b/plugins/github-actions/src/api/GithubActionsClient.ts index a667ddc4ad..7803a3149e 100644 --- a/plugins/github-actions/src/api/GithubActionsClient.ts +++ b/plugins/github-actions/src/api/GithubActionsClient.ts @@ -128,6 +128,33 @@ export class GithubActionsClient implements GithubActionsApi { }); return run.data; } + async listJobsForWorkflowRun({ + hostname, + owner, + repo, + id, + pageSize = 100, + page = 0, + }: { + hostname?: string; + owner: string; + repo: string; + id: number; + pageSize?: number; + page?: number; + }): Promise< + RestEndpointMethodTypes['actions']['listJobsForWorkflowRun']['response']['data'] + > { + const octokit = await this.getOctokit(hostname); + const jobs = await octokit.actions.listJobsForWorkflowRun({ + owner, + repo, + run_id: id, + per_page: pageSize, + page, + }); + return jobs.data; + } async downloadJobLogsForWorkflowRun({ hostname, owner, diff --git a/plugins/github-actions/src/api/types.ts b/plugins/github-actions/src/api/types.ts index 00d622cccf..1c249dfdac 100644 --- a/plugins/github-actions/src/api/types.ts +++ b/plugins/github-actions/src/api/types.ts @@ -29,7 +29,7 @@ export type Job = { conclusion: string; started_at: string; completed_at: string; - id: string; + id: number; name: string; steps: Step[]; }; diff --git a/plugins/github-actions/src/components/WorkflowRunDetails/WorkflowRunDetails.tsx b/plugins/github-actions/src/components/WorkflowRunDetails/WorkflowRunDetails.tsx index 0624438ffe..f29fbd80bb 100644 --- a/plugins/github-actions/src/components/WorkflowRunDetails/WorkflowRunDetails.tsx +++ b/plugins/github-actions/src/components/WorkflowRunDetails/WorkflowRunDetails.tsx @@ -168,7 +168,7 @@ export const WorkflowRunDetails = ({ entity }: { entity: Entity }) => { )[0].host; const [owner, repo] = projectName.value ? projectName.value.split('/') : []; const details = useWorkflowRunsDetails({ hostname, owner, repo }); - const jobs = useWorkflowRunJobs(details.value?.jobs_url); + const jobs = useWorkflowRunJobs({ hostname, owner, repo }); const error = projectName.error || (projectName.value && details.error); const classes = useStyles(); diff --git a/plugins/github-actions/src/components/WorkflowRunDetails/useWorkflowRunJobs.ts b/plugins/github-actions/src/components/WorkflowRunDetails/useWorkflowRunJobs.ts index f471ce1875..4191732c2b 100644 --- a/plugins/github-actions/src/components/WorkflowRunDetails/useWorkflowRunJobs.ts +++ b/plugins/github-actions/src/components/WorkflowRunDetails/useWorkflowRunJobs.ts @@ -14,19 +14,30 @@ * limitations under the License. */ import { useAsync } from 'react-use'; -import { Jobs } from '../../api/types'; +import { useApi, useRouteRefParams } from '@backstage/core-api'; +import { githubActionsApiRef } from '../../api'; +import { buildRouteRef } from '../../routes'; -export const useWorkflowRunJobs = (jobsUrl?: string) => { - const jobs = useAsync(async (): Promise => { - if (jobsUrl === undefined) { - return { - total_count: 0, - jobs: [], - }; - } - - const data = await fetch(jobsUrl).then(d => d.json()); - return data; - }, [jobsUrl]); +export const useWorkflowRunJobs = ({ + hostname, + owner, + repo, +}: { + hostname?: string; + owner: string; + repo: string; +}) => { + const api = useApi(githubActionsApiRef); + const { id } = useRouteRefParams(buildRouteRef); + const jobs = useAsync(async () => { + return repo && owner + ? api.listJobsForWorkflowRun({ + hostname, + owner, + repo, + id: parseInt(id, 10), + }) + : Promise.reject('No repo/owner provided'); + }, [repo, owner, id]); return jobs; }; diff --git a/plugins/github-actions/src/components/WorkflowRunLogs/WorkflowRunLogs.tsx b/plugins/github-actions/src/components/WorkflowRunLogs/WorkflowRunLogs.tsx index fe1c774dad..f24b6cea17 100644 --- a/plugins/github-actions/src/components/WorkflowRunLogs/WorkflowRunLogs.tsx +++ b/plugins/github-actions/src/components/WorkflowRunLogs/WorkflowRunLogs.tsx @@ -106,7 +106,7 @@ export const WorkflowRunLogs = ({ inProgress, }: { entity: Entity; - runId: string; + runId: number; inProgress: boolean; }) => { const config = useApi(configApiRef); diff --git a/plugins/github-actions/src/components/WorkflowRunLogs/useDownloadWorkflowRunLogs.ts b/plugins/github-actions/src/components/WorkflowRunLogs/useDownloadWorkflowRunLogs.ts index 0acf67a344..ac53f01cfe 100644 --- a/plugins/github-actions/src/components/WorkflowRunLogs/useDownloadWorkflowRunLogs.ts +++ b/plugins/github-actions/src/components/WorkflowRunLogs/useDownloadWorkflowRunLogs.ts @@ -27,7 +27,7 @@ export const useDownloadWorkflowRunLogs = ({ hostname?: string; owner: string; repo: string; - id: string; + id: number; }) => { const api = useApi(githubActionsApiRef); const details = useAsync(async () => { @@ -36,7 +36,7 @@ export const useDownloadWorkflowRunLogs = ({ hostname, owner, repo, - runId: parseInt(id, 10), + runId: id, }) : Promise.reject('No repo/owner provided'); }, [repo, owner, id]);