Skip to content

Commit

Permalink
Provide better stage phase assigning (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikRehmTT authored Oct 22, 2024
1 parent 548ff30 commit 9e200ef
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/sync-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
- name: Commit and push documentation
run: |
if [[ `git status --porcelain docs` ]]; then
git config user.name "$GITHUB_ACTOR"
git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
git config user.name "TT-CX-User"
git config user.email "90439695+TT-CX-User@users.noreply.github.com"
git add docs
git commit --signoff -m "Sync documentation"
git push
Expand Down
4 changes: 2 additions & 2 deletions docs/index-all.html
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,6 @@ <h2 class="title">G</h2>
</dt><dd> <div class="block">Returns the ANSI code for a given color.</div></dd>
<dt><span class="strong"><a href="vars/pipeline2ATX.html#getConsoleLog(def)" title="Method in pipeline2ATX">getConsoleLog(def)</a></span> - Method in <a href="vars/pipeline2ATX.html">pipeline2ATX</a>
</dt><dd> <div class="block">Gets the console log.</div></dd>
<dt><span class="strong"><a href="vars/pipeline2ATX.html#getCurrentPhase(def, def)" title="Method in pipeline2ATX">getCurrentPhase(def, def)</a></span> - Method in <a href="vars/pipeline2ATX.html">pipeline2ATX</a>
</dt><dd> <div class="block">Determines the respective phase of the stage being examined.</div></dd>
<dt><span class="strong"><a href="vars/pipeline2ATX.html#getCurrentResult(def)" title="Method in pipeline2ATX">getCurrentResult(def)</a></span> - Method in <a href="vars/pipeline2ATX.html">pipeline2ATX</a>
</dt><dd> <div class="block">Gets the current build result.</div></dd>
<dt><span class="strong"><a href="vars/pipeline2ATX.html#getDescription(def)" title="Method in pipeline2ATX">getDescription(def)</a></span> - Method in <a href="vars/pipeline2ATX.html">pipeline2ATX</a>
Expand All @@ -240,6 +238,8 @@ <h2 class="title">G</h2>
<dt><span class="strong"><a href="vars/pipeline2ATX.html#getRawBuild(java.lang.String, int)" title="Method in pipeline2ATX">getRawBuild(String, int)</a></span> - Method in <a href="vars/pipeline2ATX.html">pipeline2ATX</a>
</dt><dd> <div class="block">Gets the pipeline raw build by given job name and build number,.
</div></dd>
<dt><span class="strong"><a href="vars/pipeline2ATX.html#getStagePhase(def)" title="Method in pipeline2ATX">getStagePhase(def)</a></span> - Method in <a href="vars/pipeline2ATX.html">pipeline2ATX</a>
</dt><dd> <div class="block">Determines the respective phase of the stage being examined.</div></dd>
<dt><span class="strong"><a href="vars/log.html#getStyleCode(java.lang.String)" title="Method in log">getStyleCode(String)</a></span> - Method in <a href="vars/log.html">log</a>
</dt><dd> <div class="block">Returns the ANSI code for a given style.</div></dd>
<dt><span class="strong"><a href="vars/pipeline2ATX.html#getTestStepName(def)" title="Method in pipeline2ATX">getTestStepName(def)</a></span> - Method in <a href="vars/pipeline2ATX.html">pipeline2ATX</a>
Expand Down
35 changes: 17 additions & 18 deletions docs/vars/pipeline2ATX.html
Original file line number Diff line number Diff line change
Expand Up @@ -224,38 +224,38 @@ <h3>Methods Summary</h3>
<tr class="rowColor">
<td class="colFirst"><code></code></td>
<td class="colLast"><code>java.lang.Object</code></td>
<td class="colLast"><code><strong><a href="#getCurrentPhase(def, def)">getCurrentPhase</a></strong>(java.lang.Object stageName, java.lang.Object currentPhase)</code><br>Determines the respective phase of the stage being examined.</td>
<td class="colLast"><code><strong><a href="#getCurrentResult(def)">getCurrentResult</a></strong>(java.lang.Object build)</code><br>Gets the current build result.</td>
</tr>

<tr class="altColor">
<td class="colFirst"><code></code></td>
<td class="colLast"><code>java.lang.Object</code></td>
<td class="colLast"><code><strong><a href="#getCurrentResult(def)">getCurrentResult</a></strong>(java.lang.Object build)</code><br>Gets the current build result.</td>
<td class="colLast"><code><strong><a href="#getDescription(def)">getDescription</a></strong>(java.lang.Object row)</code><br>Create a description from the current pipeline step.</td>
</tr>

<tr class="rowColor">
<td class="colFirst"><code></code></td>
<td class="colLast"><code>java.lang.Object</code></td>
<td class="colLast"><code><strong><a href="#getDescription(def)">getDescription</a></strong>(java.lang.Object row)</code><br>Create a description from the current pipeline step.</td>
<td class="colLast"><code><strong><a href="#getExecutionSteps(def, def)">getExecutionSteps</a></strong>(java.lang.Object build, java.lang.Object appendLogs)</code><br>Create test execution steps from the current pipeline build.</td>
</tr>

<tr class="altColor">
<td class="colFirst"><code></code></td>
<td class="colLast"><code>java.lang.Object</code></td>
<td class="colLast"><code><strong><a href="#getExecutionSteps(def, def)">getExecutionSteps</a></strong>(java.lang.Object build, java.lang.Object appendLogs)</code><br>Create test execution steps from the current pipeline build.</td>
<td class="colLast"><code><strong><a href="#getLogText(def)">getLogText</a></strong>(java.lang.Object node)</code><br>Get the log of a node.</td>
</tr>

<tr class="rowColor">
<td class="colFirst"><code></code></td>
<td class="colLast"><code>java.lang.Object</code></td>
<td class="colLast"><code><strong><a href="#getLogText(def)">getLogText</a></strong>(java.lang.Object node)</code><br>Get the log of a node.</td>
<td class="colLast"><code><strong><a href="#getRawBuild(java.lang.String, int)">getRawBuild</a></strong>(java.lang.String jobName, int buildNumber)</code><br>Gets the pipeline raw build by given job name and build number,.
</td>
</tr>

<tr class="altColor">
<td class="colFirst"><code></code></td>
<td class="colLast"><code>java.lang.Object</code></td>
<td class="colLast"><code><strong><a href="#getRawBuild(java.lang.String, int)">getRawBuild</a></strong>(java.lang.String jobName, int buildNumber)</code><br>Gets the pipeline raw build by given job name and build number,.
</td>
<td class="colLast"><code><strong><a href="#getStagePhase(def)">getStagePhase</a></strong>(java.lang.Object stageName)</code><br>Determines the respective phase of the stage being examined.</td>
</tr>

<tr class="rowColor">
Expand Down Expand Up @@ -542,17 +542,6 @@ <h4>java.lang.Object <strong>getConsoleLog</strong>(java.lang.Object build)</h4>
</li>
</ul>

<a name="getCurrentPhase(def, def)"><!-- --></a>
<ul class="blockListLast">
<li class="blockList">
<h4>java.lang.Object <strong>getCurrentPhase</strong>(java.lang.Object stageName, java.lang.Object currentPhase)</h4>
<p> Determines the respective phase of the stage being examined.
<DL><DT><B>Parameters:</B></DT><DD><code>stageName
</code> - the name of the current stage</DD><DD><code>currentPhase
</code> - the corresponding phase in which the current stage is located</DD></DL><DL><DT><B>Returns:</B></DT><DD>the current phase</DD></DL></p>
</li>
</ul>

<a name="getCurrentResult(def)"><!-- --></a>
<ul class="blockListLast">
<li class="blockList">
Expand Down Expand Up @@ -608,6 +597,16 @@ <h4>java.lang.Object <strong>getRawBuild</strong>(java.lang.String jobName, int
</li>
</ul>

<a name="getStagePhase(def)"><!-- --></a>
<ul class="blockListLast">
<li class="blockList">
<h4>java.lang.Object <strong>getStagePhase</strong>(java.lang.Object stageName)</h4>
<p> Determines the respective phase of the stage being examined.
<DL><DT><B>Parameters:</B></DT><DD><code>stageName
</code> - the name of the stage</DD></DL><DL><DT><B>Returns:</B></DT><DD>the current phase</DD></DL></p>
</li>
</ul>

<a name="getTestStepName(def)"><!-- --></a>
<ul class="blockListLast">
<li class="blockList">
Expand Down
27 changes: 20 additions & 7 deletions test/vars/Pipeline2ATXTest.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 - 2023 tracetronic GmbH
* Copyright (c) 2021 - 2024 tracetronic GmbH
*
* SPDX-License-Identifier: MIT
*/
Expand Down Expand Up @@ -103,12 +103,25 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
['key':'TOOL_VERSION', 'value': ['1.2.3','42']]]
}

def 'Get Stage Phase'() {
expect:
result == pipeline2ATX.getStagePhase(stageName)

where:
stageName | result
'stage (Pre: Test)' | 'setup'
'stage (Declarative: SCM Checkout)' | 'setup'
'stage (Run Tests)' | 'execution'
'stage (Declarative: Post Actions)' | 'teardown'
'stage (Post: export logs)' | 'teardown'
}

def 'Calculate metric times - error free and non pushed based build'() {
given: 'pipeline stages'
def mockPipelineStages = [
['name': 'stage (Declarative)', 'duration': 250],
['name': 'execution phase', 'duration': 500],
['name': 'stage (Declarative: Post', 'duration': 250]
['name': 'stage (Pre: Start Environment)', 'duration': 250],
['name': 'Run tests', 'duration': 500],
['name': 'stage (Declarative: Post Actions)', 'duration': 250]
]
def build = GroovyMock(Run)
build.getStartTimeInMillis() >> 2000
Expand Down Expand Up @@ -138,9 +151,9 @@ class Pipeline2ATXTest extends PipelineSpockTestBase {
def 'Calculate metric times - Time from commit to start for pushed based builds'() {
given: 'a build and a mocked time from commit to start'
def mockPipelineStages = [
['name': 'stage (Declarative)', 'duration': 250],
['name': 'execution phase', 'duration': 500],
['name': 'stage (Declarative: Post', 'duration': 250]
['name': 'stage (Pre: Start Environment)', 'duration': 250],
['name': 'Run tests', 'duration': 500],
['name': 'stage (Declarative: Post Actions)', 'duration': 250]
]
def build = GroovyMock(Run)
def currentBuild = GroovyMock(RunWrapper)
Expand Down
46 changes: 17 additions & 29 deletions vars/pipeline2ATX.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 tracetronic GmbH
* Copyright (c) 2023 - 2024 tracetronic GmbH
*
* SPDX-License-Identifier: MIT
*/
Expand Down Expand Up @@ -254,7 +254,6 @@ def generateJsonReport(build, attributes, constants, executionTestSteps, paramet
* @return a map containing the calculated durations in seconds and percentages
*/
def calculateTime(executionTestSteps, build) {
def currentPhase = 'setup'
def setupDuration = 0.0
def executionDuration = 0.0
def teardownDuration = 0.0
Expand All @@ -263,15 +262,14 @@ def calculateTime(executionTestSteps, build) {
def fromCommitToStartTime = currentBuild.getBuildCauses('jenkins.branch.BranchEventCause').isEmpty() ? null : getTimeFromCommitToStart(build)

executionTestSteps.each { stage ->
def stageName = stage.name
// Update the current phase based on the stage name
currentPhase = getCurrentPhase(stageName, currentPhase)
def stagePhase = getStagePhase(stage.name)

if (currentPhase == 'teardown') {
if (stagePhase == 'teardown') {
teardownDuration += stage.duration
} else if (currentPhase == 'setup') {
} else if (stagePhase == 'setup') {
setupDuration += stage.duration
} else if (currentPhase == 'execution') {
} else if (stagePhase == 'execution') {
executionDuration += stage.duration
}
}
Expand Down Expand Up @@ -321,28 +319,20 @@ def convertTimeValueToDouble(def value) {
def calculateErrorTime(executionTestSteps) {
def errorTime = null
def accumulatedTime = 0
def currentPhase = 'setup'

executionTestSteps.each { stage ->
if (stage == null) {
return
}

def stageName = stage.get('name')
def stageDuration = stage.get('duration', 0)

currentPhase = getCurrentPhase(stageName, currentPhase)

// Only process stages in the execution phase
if (currentPhase == 'execution') {
def teststeps = stage.get('teststeps', [])
def stageErrorTime = findNonPassedVerdict(teststeps, stageDuration)
if (stageErrorTime != null) {
errorTime = accumulatedTime + stageErrorTime
return errorTime
}
accumulatedTime += stageDuration
def teststeps = stage.get('teststeps', [])
def stageErrorTime = findNonPassedVerdict(teststeps, stageDuration)
if (stageErrorTime != null) {
errorTime = accumulatedTime + stageErrorTime
return errorTime
}
accumulatedTime += stageDuration
}
return errorTime
}
Expand All @@ -351,18 +341,16 @@ def calculateErrorTime(executionTestSteps) {
* Determines the respective phase of the stage being examined.
*
* @param stageName
* the name of the current stage
* @param currentPhase
* the corresponding phase in which the current stage is located
* the name of the stage
* @return the current phase
*/
def getCurrentPhase(stageName, currentPhase) {
if (stageName.contains("stage (Declarative: Post")) {
def getStagePhase(stageName) {
if (stageName.contains("Post Actions") || stageName.contains("Post:")) {
return 'teardown'
} else if (!stageName.contains("stage (Declarative") && currentPhase == 'setup') {
return 'execution'
} else if (stageName.contains("Declarative:") || stageName.contains("Pre:")) {
return 'setup'
}
return currentPhase
return 'execution'
}

/**
Expand Down

0 comments on commit 9e200ef

Please sign in to comment.