Skip to content

Commit

Permalink
fallback to rest api if pipeline iid is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
tonka3000 committed Apr 4, 2022
1 parent 82741ef commit b7ef455
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
78 changes: 60 additions & 18 deletions extensions/gitlab/src/components/jobs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,17 @@ export function JobListItem(props: { job: Job; projectFullPath: string; onRefres
);
}

export function JobList(props: { projectFullPath: string; pipelineIID: string }): JSX.Element {
const { stages, error, isLoading, refresh } = useSearch("", props.projectFullPath, props.pipelineIID);
export function JobList(props: {
projectFullPath: string;
pipelineID: string;
pipelineIID?: string | undefined;
}): JSX.Element {
const { stages, error, isLoading, refresh } = useSearch(
"",
props.projectFullPath,
props.pipelineID,
props.pipelineIID
);
useInterval(() => {
refresh();
}, getCIRefreshInterval());
Expand All @@ -171,10 +180,18 @@ export function JobList(props: { projectFullPath: string; pipelineIID: string })
);
}

interface RESTJob {
id: number;
status: string;
stage: string;
name: string;
}

export function useSearch(
query: string | undefined,
projectFullPath: string,
pipelineIID: string
pipelineID: string,
pipelineIID?: string | undefined
): {
stages: Record<string, Job[]>;
error?: string;
Expand Down Expand Up @@ -204,22 +221,41 @@ export function useSearch(
setError(undefined);

try {
const data = await gitlabgql.client.query({
query: GET_PIPELINE_JOBS,
variables: { fullPath: projectFullPath, pipelineIID: pipelineIID },
fetchPolicy: "network-only",
});
const stages: Record<string, Job[]> = {};
for (const stage of data.data.project.pipeline.stages.nodes) {
if (!stages[stage.name]) {
stages[stage.name] = [];
if (pipelineIID) {
const data = await gitlabgql.client.query({
query: GET_PIPELINE_JOBS,
variables: { fullPath: projectFullPath, pipelineIID: pipelineIID },
fetchPolicy: "network-only",
});
const stages: Record<string, Job[]> = {};
for (const stage of data.data.project.pipeline.stages.nodes) {
if (!stages[stage.name]) {
stages[stage.name] = [];
}
for (const job of stage.jobs.nodes) {
stages[stage.name].push({ id: job.id, name: job.name, status: job.status });
}
}
for (const job of stage.jobs.nodes) {
stages[stage.name].push({ id: job.id, name: job.name, status: job.status });
if (!didUnmount) {
setStages(stages);
}
} else if (pipelineID) {
const projectUE = encodeURIComponent(projectFullPath);
const jobs: RESTJob[] = await gitlab
.fetch(`projects/${projectUE}/pipelines/${pipelineID}/jobs`)
.then((data) => {
return data.map((j: any) => j as RESTJob);
});
const stages: Record<string, Job[]> = {};
for (const job of jobs) {
if (!stages[job.stage]) {
stages[job.stage] = [];
}
stages[job.stage].push({ id: `${job.id}`, name: job.name, status: job.status });
}
if (!didUnmount) {
setStages(stages);
}
}
if (!didUnmount) {
setStages(stages);
}
} catch (e) {
if (!didUnmount) {
Expand Down Expand Up @@ -273,7 +309,13 @@ export function PipelineJobsListByCommit(props: { project: Project; sha: string
return <List isLoading={isLoading} searchBarPlaceholder="Loading" />;
} else {
if (commit.last_pipeline) {
return <JobList projectFullPath={props.project.fullPath} pipelineIID={`${commit.last_pipeline.iid}`} />;
return (
<JobList
projectFullPath={props.project.fullPath}
pipelineID={`${commit.last_pipeline.id}`}
pipelineIID={commit.last_pipeline.iid ? `${commit.last_pipeline.iid}` : undefined}
/>
);
} else {
return (
<List>
Expand Down
4 changes: 3 additions & 1 deletion extensions/gitlab/src/components/pipelines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export function PipelineListItem(props: {
<ActionPanel.Section>
<Action.Push
title="Show Jobs"
target={<JobList projectFullPath={props.projectFullPath} pipelineIID={pipeline.iid} />}
target={
<JobList projectFullPath={props.projectFullPath} pipelineID={pipeline.id} pipelineIID={pipeline.iid} />
}
icon={{ source: Icon.Terminal, tintColor: Color.PrimaryText }}
/>
<GitLabOpenInBrowserAction url={pipeline.webUrl} />
Expand Down

0 comments on commit b7ef455

Please sign in to comment.