Skip to content

Commit ee5d2da

Browse files
author
yang.guo
authored
Merge pull request #199 from FlowCI/feature/154
feature/154
2 parents b153fb6 + fa8c70a commit ee5d2da

File tree

4 files changed

+55
-37
lines changed

4 files changed

+55
-37
lines changed

platform-api/src/main/java/com/flow/platform/api/envs/JobEnvs.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,25 @@ public enum JobEnvs implements EnvKey {
2525

2626
FLOW_JOB_BUILD_NUMBER,
2727

28+
/**
29+
* Job build category (trigger type)
30+
*/
2831
FLOW_JOB_BUILD_CATEGORY,
2932

33+
/**
34+
* Agent zone and name
35+
*/
3036
FLOW_JOB_AGENT_INFO,
3137

32-
FLOW_JOB_LOG_PATH;
38+
/**
39+
* Job log download url
40+
*/
41+
FLOW_JOB_LOG_PATH,
42+
43+
/**
44+
* The last step status: values in {@code NodeStatus}
45+
*/
46+
FLOW_JOB_LAST_STATUS;
3347

3448
private boolean readonly;
3549

platform-api/src/main/java/com/flow/platform/api/service/job/JobServiceImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,15 @@ private void run(Node node, Job job) {
371371
NodeResult rootResult = nodeResultService.find(tree.root().getPath(), job.getId());
372372
envVars.putAll(rootResult.getOutputs());
373373

374+
// pass last step noed status
375+
Node prev = tree.prev(node.getPath());
376+
if (prev != null) {
377+
NodeResult prevResult = nodeResultService.find(prev.getPath(), job.getId());
378+
if (prevResult != null) {
379+
envVars.putEnv(JobEnvs.FLOW_JOB_LAST_STATUS, prevResult.getStatus().toString());
380+
}
381+
}
382+
374383
// to run node with customized cmd id
375384
try {
376385
NodeResult nodeResult = nodeResultService.find(node.getPath(), job.getId());
@@ -379,7 +388,7 @@ private void run(Node node, Job job) {
379388
EnvUtil.keepNewlineForEnv(credentialEnvs, null);
380389
envVars.putAll(credentialEnvs);
381390

382-
CmdInfo cmd = cmdService.runShell(job, node, nodeResult.getCmdId(), envVars);
391+
cmdService.runShell(job, node, nodeResult.getCmdId(), envVars);
383392
} catch (IllegalStatusException e) {
384393
CmdInfo rawCmd = (CmdInfo) e.getData();
385394
rawCmd.setStatus(CmdStatus.EXCEPTION);

platform-api/src/main/java/com/flow/platform/api/util/NodeUtil.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static void recurse(final Node root, final Consumer<Node> onNode) {
9999
* get all nodes includes flow and steps
100100
*
101101
* @param node the parent node
102-
* @return List<Node> include parent node
102+
* @return {@code List<Node>} include parent node
103103
*/
104104
public static List<Node> flat(final Node node) {
105105
final List<Node> flatted = new LinkedList<>();
@@ -122,21 +122,17 @@ public static Node findRootNode(Node node) {
122122
* get prev node from flow
123123
*/
124124
public static Node prev(Node node, List<Node> ordered) {
125-
Integer index = -1;
126-
Node target = null;
125+
Integer index = ordered.indexOf(node);
127126

128-
for (int i = 0; i < ordered.size(); i++) {
129-
Node item = ordered.get(i);
130-
if (item.equals(node)) {
131-
index = i;
132-
}
127+
if (index == -1) {
128+
return null;
133129
}
134130

135-
if (index >= 1 && index != -1 && index < ordered.size() - 1) {
136-
target = ordered.get(index - 1);
131+
if (index >= 1 && index <= ordered.size() - 1) {
132+
return ordered.get(index - 1);
137133
}
138134

139-
return target;
135+
return null;
140136
}
141137

142138
/**
@@ -147,29 +143,18 @@ public static Node prev(Node node, List<Node> ordered) {
147143
* @return next node of current
148144
*/
149145
public static Node next(Node node, List<Node> ordered) {
150-
Integer index = -1;
151-
Node target = null;
152-
153-
//find node
154-
for (int i = 0; i < ordered.size(); i++) {
155-
Node item = ordered.get(i);
156-
if (item.equals(node)) {
157-
index = i;
158-
break;
159-
}
146+
Integer index = ordered.indexOf(node);
147+
148+
if (index == -1) {
149+
return null;
160150
}
161151

162152
// find node
163-
if (index != -1) {
164-
try {
165-
target = ordered.get(index + 1);
166-
} catch (Throwable ignore) {
167-
//not found ignore
168-
target = null;
169-
}
153+
try {
154+
return ordered.get(index + 1);
155+
} catch (Throwable ignore) {
156+
return null;
170157
}
171-
172-
return target;
173158
}
174159

175160
/**

platform-api/src/test/java/com/flow/platform/api/test/service/JobServiceTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package com.flow.platform.api.test.service;
1818

19+
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.exactly;
21+
import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath;
22+
import static com.github.tomakehurst.wiremock.client.WireMock.moreThan;
23+
import static com.github.tomakehurst.wiremock.client.WireMock.moreThanOrExactly;
1924
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
2025
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
2126
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
@@ -157,15 +162,15 @@ public void should_run_job_with_success_status() throws Throwable {
157162
Job job = createMockJob(root.getPath());
158163

159164
// then: check cmd request to create session
160-
CountMatchingStrategy countStrategy = new CountMatchingStrategy(CountMatchingStrategy.EQUAL_TO, 1);
161-
verify(countStrategy, postRequestedFor(urlEqualTo("/cmd/queue/send?priority=1&retry=5")));
165+
verify(exactly(1), postRequestedFor(urlEqualTo("/cmd/queue/send?priority=1&retry=5")));
162166

163167
// when: simulate cc callback for create session
164168
Cmd cmd = new Cmd("default", null, CmdType.CREATE_SESSION, null);
165169
cmd.setSessionId(sessionId);
166170
cmd.setStatus(CmdStatus.SENT);
167171
CmdCallbackQueueItem createSessionItem = new CmdCallbackQueueItem(job.getId(), cmd);
168172
jobService.callback(createSessionItem);
173+
verify(exactly(1), postRequestedFor(urlEqualTo("/cmd/send")));
169174

170175
// then: check job status should be running
171176
job = reload(job);
@@ -178,6 +183,7 @@ public void should_run_job_with_success_status() throws Throwable {
178183
Assert.assertEquals(7, steps.size());
179184

180185
// when: simulate callback for all steps
186+
int numOfRequestForCmdSend = 2;
181187
for (Node step : steps) {
182188
NodeResult stepResult = nodeResultService.find(step.getPath(), job.getId());
183189

@@ -204,16 +210,20 @@ public void should_run_job_with_success_status() throws Throwable {
204210
stepCmd.getCmdResult().setFinishTime(finish);
205211

206212
// build mock identifier
207-
208213
CmdCallbackQueueItem runStepShellItem = new CmdCallbackQueueItem(job.getId(), stepCmd);
209214
jobService.callback(runStepShellItem);
210215
stepResult = nodeResultService.find(step.getPath(), job.getId());
211216
Assert.assertEquals(NodeStatus.SUCCESS, stepResult.getStatus());
217+
218+
verify(exactly(numOfRequestForCmdSend++), postRequestedFor(urlEqualTo("/cmd/send")));
219+
220+
verify(moreThanOrExactly(1),
221+
postRequestedFor(urlEqualTo("/cmd/send"))
222+
.withRequestBody(matchingJsonPath("$.inputs[?(@.FLOW_JOB_LAST_STATUS == 'SUCCESS')]")));
212223
}
213224

214225
// then: check num of cmd request to run shell, 5 steps + 1 del session requests
215-
countStrategy = new CountMatchingStrategy(CountMatchingStrategy.EQUAL_TO, 6);
216-
verify(countStrategy, postRequestedFor(urlEqualTo("/cmd/send")));
226+
verify(exactly(6), postRequestedFor(urlEqualTo("/cmd/send")));
217227

218228
// then: check job status
219229
job = reload(job);

0 commit comments

Comments
 (0)