Skip to content

Add link to agent a stage is running on to stage summary #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react";

export interface StageNodeLinkProps {
agent: string;
}

function getAgentUrl(name: string) {
// Wrap built-in in brackets
const id = name == "built-in" ? "(built-in)" : name;
const rootPath = document.head.dataset.rooturl
return `${rootPath}/computer/${id}/`;
}

const StageNodeLink = ({agent}: StageNodeLinkProps) => {
const agentName = agent == "built-in" ? "Jenkins" : agent;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem to be working the agent name is coming through as jenkins and not Jenkins:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess jenkins is also fine. What does the link say? (built-in) or also jenkins?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem was the capitalize css class applied to it.

const href = getAgentUrl(agent);
return <>
Running on <a href={href}>{agentName}</a>
</>
};

export default StageNodeLink;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ScheduleIcon from "@mui/icons-material/Schedule";
import TimerIcon from "@mui/icons-material/Timer";
import InfoIcon from "@mui/icons-material/Info";
import LinkIcon from "@mui/icons-material/Link";
import ComputerIcon from "@mui/icons-material/Computer";

import {
StepInfo,
Expand All @@ -14,6 +15,7 @@ import {
LOG_FETCH_SIZE,
} from "./PipelineConsoleModel";
import { ConsoleLogCard } from "./ConsoleLogCard";
import StageNodeLink from "./StageNodeLink";

export interface StageSummaryProps {
stage: StageInfo;
Expand Down Expand Up @@ -81,6 +83,22 @@ const StageSummary = (props: StageSummaryProps) => (
{props.stage.state}
</span>
</div>
{props.stage.agent && (
<div
className="detail-element"
key={`stage-detail-agent-container-${props.stage.id}`}
>
<ComputerIcon
className="detail-icon"
key={`stage-detail-agent-icon-${props.stage.id}`}
/>
<span
key={`stage-detail-agent-text-${props.stage.id}`}
>
<StageNodeLink agent={props.stage.agent} />
</span>
</div>
)}
{props.failedSteps.map((value: StepInfo) => {
console.debug(`Found failed step ${value}`);
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Result, StageType, StageInfo, StepInfo } from "./PipelineConsoleModel";
export const defaultStagesList = [
export const defaultStagesList: StageInfo[] = [
{
name: "Stage A",
title: "",
Expand All @@ -11,6 +11,7 @@ export const defaultStagesList = [
pauseDurationMillis: "",
startTimeMillis: "",
totalDurationMillis: "",
agent: "built-in",
},
{
name: "Stage B",
Expand All @@ -23,6 +24,7 @@ export const defaultStagesList = [
pauseDurationMillis: "",
startTimeMillis: "",
totalDurationMillis: "",
agent: "not-built-in",
},
{
name: "Parent C",
Expand All @@ -43,11 +45,13 @@ export const defaultStagesList = [
pauseDurationMillis: "",
startTimeMillis: "",
totalDurationMillis: "",
agent: "not-built-in",
},
],
pauseDurationMillis: "",
startTimeMillis: "",
totalDurationMillis: "",
agent: "built-in",
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("PipelineGraphLayout", () => {
pauseDurationMillis: "",
startTimeMillis: "",
totalDurationMillis: "",
agent: "built-in",
};

const makeStage = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface StageInfo {
pauseDurationMillis: string;
startTimeMillis: string;
totalDurationMillis: string;
agent: string;
}

interface BaseNodeInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@
import hudson.model.Queue;
import io.jenkins.plugins.pipelinegraphview.treescanner.PipelineNodeGraphAdapter;
import io.jenkins.plugins.pipelinegraphview.utils.legacy.PipelineNodeGraphVisitor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.jenkinsci.plugins.workflow.actions.WorkspaceAction;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -68,9 +66,10 @@
state,
50, // TODO how ???
flowNodeWrapper.getType().name(),
flowNodeWrapper.getDisplayName(), // TODO blue ocean uses timing information: "Passed in 0s"

Check warning on line 69 in src/main/java/io/jenkins/plugins/pipelinegraphview/utils/PipelineGraphApi.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: blue ocean uses timing information: "Passed in 0s"
flowNodeWrapper.isSynthetic(),
flowNodeWrapper.getTiming());
flowNodeWrapper.getTiming(),
getStageNode(flowNodeWrapper));
})
.collect(Collectors.toList());
}
Expand Down Expand Up @@ -197,7 +196,7 @@
stageToChildrenMap.put(stage.getId(), new ArrayList<>());
topLevelStageIds.add(stage.getId());
}
} catch (java.io.IOException ex) {
} catch (IOException ex) {
logger.error("Caught a "
+ ex.getClass().getSimpleName()
+ " when trying to find parent of stage '"
Expand Down Expand Up @@ -233,6 +232,49 @@
return ancestors;
}

private static String getStageNode(FlowNodeWrapper flowNodeWrapper) {
FlowNode flowNode = flowNodeWrapper.getNode();
DepthFirstScanner scan = new DepthFirstScanner();
logger.debug("Checking node {}", flowNode);
FlowExecution execution = flowNode.getExecution();
for (FlowNode n : scan.allNodes(execution)) {
WorkspaceAction ws = n.getAction(WorkspaceAction.class);
if (ws != null) {
logger.debug("Found workspace node: {}", n);
boolean isWorkspaceNode = Objects.equals(n.getId(), flowNode.getId())
|| Objects.equals(n.getEnclosingId(), flowNode.getId())
|| flowNode.getAllEnclosingIds().contains(n.getId());

// Parallel stages have a sub-stage, so we need to check the 3rd parent for a match
if (flowNodeWrapper.getType() == FlowNodeWrapper.NodeType.PARALLEL) {
try {
if (n.getEnclosingId() != null) {
FlowNode p = execution.getNode(n.getEnclosingId());
if (p != null && p.getEnclosingId() != null) {
p = execution.getNode(p.getEnclosingId());
if (p != null && p.getEnclosingId() != null) {
isWorkspaceNode = Objects.equals(flowNode.getId(), p.getEnclosingId());
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

if (isWorkspaceNode) {
logger.debug("Found correct stage node: {}", n.getId());
String node = ws.getNode();
if (node.isEmpty()) {
node = "built-in";
}
return node;
}
}
}
return null;
}

public PipelineGraph createTree() {
return createTree(new PipelineNodeGraphAdapter(run));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class PipelineStage extends AbstractPipelineNode {
private final PipelineStage nextSibling;
private boolean sequential;
private boolean synthetic;
private String agent;

public PipelineStage(
String id,
Expand All @@ -23,13 +24,15 @@ public PipelineStage(
PipelineStage nextSibling,
boolean sequential,
boolean synthetic,
TimingInfo timingInfo) {
TimingInfo timingInfo,
String agent) {
super(id, name, state, completePercent, type, title, timingInfo);
this.children = children;
this.seqContainerName = seqContainerName;
this.nextSibling = nextSibling;
this.sequential = sequential;
this.synthetic = synthetic;
this.agent = agent;
}

public PipelineStage getNextSibling() {
Expand All @@ -53,4 +56,8 @@ public List<PipelineStage> getChildren() {
public boolean isSynthetic() {
return synthetic;
}

public String getAgent() {
return agent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class PipelineStageInternal {
private boolean sequential;
private boolean synthetic;
private TimingInfo timingInfo;
private String agent;

public PipelineStageInternal(
String id,
Expand All @@ -29,7 +30,8 @@ public PipelineStageInternal(
String type,
String title,
boolean synthetic,
TimingInfo times) {
TimingInfo times,
String agent) {
this.id = id;
this.name = name;
this.parents = parents;
Expand All @@ -39,6 +41,7 @@ public PipelineStageInternal(
this.title = title;
this.synthetic = synthetic;
this.timingInfo = times;
this.agent = agent;
}

public boolean isSequential() {
Expand Down Expand Up @@ -121,6 +124,14 @@ public void setSynthetic(boolean synthetic) {
this.synthetic = synthetic;
}

public String getAgent() {
return agent;
}

public void setAgent(String aAgent) {
this.agent = aAgent;
}

public PipelineStage toPipelineStage(List<PipelineStage> children) {
return new PipelineStage(
id,
Expand All @@ -134,6 +145,7 @@ public PipelineStage toPipelineStage(List<PipelineStage> children) {
nextSibling != null ? nextSibling.toPipelineStage(Collections.emptyList()) : null,
sequential,
synthetic,
timingInfo);
timingInfo,
agent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import static org.hamcrest.Matchers.*;

import hudson.model.Result;
import hudson.model.labels.LabelAtom;
import hudson.model.queue.QueueTaskFuture;
import hudson.slaves.DumbSlave;
import io.jenkins.plugins.pipelinegraphview.treescanner.NodeRelationshipFinder;
import io.jenkins.plugins.pipelinegraphview.treescanner.PipelineNodeGraphAdapter;
import io.jenkins.plugins.pipelinegraphview.treescanner.PipelineNodeTreeScanner;
Expand Down Expand Up @@ -408,4 +410,76 @@ public void gh_358_parallelStagesMarkedAsSkipped() throws Exception {
equalTo(
"foo{success},first-parallel{failure}[bar{skipped},baz{failure}],second-parallel{skipped},Post Actions{success}"));
}

@Test
public void getAgentForSingleStagePipeline() throws Exception {
WorkflowRun run = TestUtils.createAndRunJob(
j, "getAgentForSingleStagePipeline", "singleStagePipeline.jenkinsfile", Result.SUCCESS);

List<PipelineStage> stages = new PipelineGraphApi(run).createTree().getStages();

assertThat(stages.size(), equalTo(1));
assertThat(stages.get(0).getAgent(), equalTo("built-in"));
}

@Test
public void getAgentForSingleStagePipelineWithExternalAgent() throws Exception {
var testingLabel = new LabelAtom("external");
DumbSlave agent = j.createSlave(testingLabel);
j.waitOnline(agent);

WorkflowRun run = TestUtils.createAndRunJob(
j,
"getAgentForSingleStagePipelineWithExternalAgent",
"singleStagePipelineWithExternalAgent.jenkinsfile",
Result.SUCCESS);

List<PipelineStage> stages = new PipelineGraphApi(run).createTree().getStages();

assertThat(stages.size(), equalTo(1));
assertThat(stages.get(0).getAgent(), equalTo(agent.getNodeName()));
}

@Test
public void getAgentForParallelPipelineWithExternalAgent() throws Exception {
var testingLabel = new LabelAtom("external");
DumbSlave agent = j.createSlave(testingLabel);
j.waitOnline(agent);

WorkflowRun run = TestUtils.createAndRunJob(
j,
"getAgentForParallelPipelineWithExternalAgent",
"parallelPipelineWithExternalAgent.jenkinsfile",
Result.SUCCESS);

List<PipelineStage> stages = new PipelineGraphApi(run).createTree().getStages();

// Parallel pipeline structure:
// name: Parallel, type: STAGE
// name: Parallel, type: PARALLEL_BLOCK

// name: Builtin, type: PARALLEL
// name: Stage : Start, type: STEPS_BLOCK
// name: Builtin, type: STAGE
// name: Allocate node : Start, type: STEPS_BLOCK

assertThat(stages.size(), equalTo(1));
assertThat(stages.get(0).getType(), equalTo("STAGE"));
assertThat(stages.get(0).getName(), equalTo("Parallel"));
assertThat(stages.get(0).getAgent(), equalTo(null));

List<PipelineStage> children = stages.get(0).getChildren();

assertThat(children.size(), equalTo(2));

PipelineStage builtinStage = children.get(0);
assertThat(builtinStage.getType(), equalTo("PARALLEL"));
assertThat(builtinStage.getName(), equalTo("Builtin"));
assertThat(builtinStage.getAgent(), equalTo("built-in"));

PipelineStage externalStage = children.get(1);
assertThat(externalStage.getType(), equalTo("PARALLEL"));
assertThat(externalStage.getName(), equalTo("External"));
assertThat(externalStage.getAgent(), equalTo(agent.getNodeName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pipeline {
agent none
stages {
stage('Parallel') {
parallel {
stage('Builtin') {
agent { label 'built-in' }
steps {
echo "Hello, from home"
}
}
stage('External') {
agent { label 'external' }
steps {
echo "Hello, from far away"
}
}
}
}
}
}
Loading
Loading