Skip to content
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

Доработано падение шага при отказе скрипта с вызовом ring (EDT) #113

Merged
merged 4 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 8 additions & 4 deletions src/ru/pulsar/jenkins/library/IStepExecutor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ interface IStepExecutor {

boolean isUnix()

int sh(String script, boolean returnStatus, String encoding)
def sh(String script, boolean returnStatus, boolean returnStdout, String encoding)

int bat(String script, boolean returnStatus, String encoding)
def bat(String script, boolean returnStatus, boolean returnStdout, String encoding)

String libraryResource(String path)

Expand All @@ -30,9 +30,13 @@ interface IStepExecutor {

void echo(message)

int cmd(String script, boolean returnStatus)
def cmd(String script, boolean returnStatus, boolean returnStdout)

int cmd(String script)
def cmd(String script, boolean returnStatus)

def cmd(String script)

def ringCommand(String script)

void tool(String toolName)

Expand Down
17 changes: 11 additions & 6 deletions src/ru/pulsar/jenkins/library/StepExecutor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class StepExecutor implements IStepExecutor {
}

@Override
int sh(String script, boolean returnStatus, String encoding) {
steps.sh script: script, returnStatus: returnStatus, encoding: encoding
def sh(String script, boolean returnStatus, boolean returnStdout, String encoding) {
steps.sh script: script, returnStatus: returnStatus, returnStdout: returnStdout, encoding: encoding
}

@Override
int bat(String script, boolean returnStatus, String encoding) {
steps.bat script: script, returnStatus: returnStatus, encoding: encoding
def bat(String script, boolean returnStatus, boolean returnStdout, String encoding) {
steps.bat script: script, returnStatus: returnStatus, returnStdout: returnStdout, encoding: encoding
}

@Override
Expand Down Expand Up @@ -58,8 +58,13 @@ class StepExecutor implements IStepExecutor {
}

@Override
int cmd(String script, boolean returnStatus = false) {
return steps.cmd(script, returnStatus)
def cmd(String script, boolean returnStatus = false, boolean returnStdout = false) {
return steps.cmd(script, returnStatus, returnStdout)
}

@Override
def ringCommand(String script) {
return steps.ringCommand(script)
}

@Override
Expand Down
16 changes: 11 additions & 5 deletions src/ru/pulsar/jenkins/library/steps/Cmd.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ class Cmd implements Serializable {

private String script;
private boolean returnStatus
private boolean returnStdout
private String encoding = 'UTF-8'

Cmd(String script, boolean returnStatus = false) {
Cmd(String script, boolean returnStatus = false, boolean returnStdout = false) {
this.script = script
this.returnStatus = returnStatus
this.returnStdout = returnStdout
};

int run() {
def run() {
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()

int returnValue
def returnValue

if (returnStatus & returnStdout) {
return "returnStatus and returnStdout are not supported at the same time"
nixel2007 marked this conversation as resolved.
Show resolved Hide resolved
}

if (steps.isUnix()) {
returnValue = steps.sh("$script", returnStatus, encoding)
returnValue = steps.sh("$script", returnStatus, returnStdout, encoding)
} else {
returnValue = steps.bat("chcp 65001 > nul \n$script", returnStatus, encoding)
returnValue = steps.bat("chcp 65001 > nul \n$script", returnStatus, returnStdout, encoding)
}

return returnValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DesignerToEdtFormatTransformation implements Serializable {

def ringOpts = [Constants.DEFAULT_RING_OPTS]
steps.withEnv(ringOpts) {
nixel2007 marked this conversation as resolved.
Show resolved Hide resolved
steps.cmd(ringCommand)
steps.ringCommand(ringCommand)
}

steps.zip(WORKSPACE, WORKSPACE_ZIP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class EdtToDesignerFormatTransformation implements Serializable {

def ringOpts = [Constants.DEFAULT_RING_OPTS]
steps.withEnv(ringOpts) {
steps.cmd(ringCommand)
steps.ringCommand(ringCommand)
}

steps.zip(CONFIGURATION_DIR, CONFIGURATION_ZIP)
Expand Down
2 changes: 1 addition & 1 deletion src/ru/pulsar/jenkins/library/steps/EdtValidate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class EdtValidate implements Serializable {
def ringOpts = [Constants.DEFAULT_RING_OPTS]
steps.withEnv(ringOpts) {
steps.catchError {
Copy link
Member

Choose a reason for hiding this comment

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

Обрати внимание, тут catchError. Он подавит исключение от падения едт

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Обрати внимание, тут catchError. Он подавит исключение от падения едт

С этим непонятно, это шаг именно валидации, возможно при его падении не должен падать пайплайн и поэтому тут catchError? Ты не помнишь какую логику сюда закладывал?

Copy link
Member

Choose a reason for hiding this comment

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

Падение самой валидации не должно валить пайплайн. Но если рухнул сам ринг с еггогом, то пайплайн надо таки завалить кмк, так как это сильно зааффектит анализ в сонаре потом

steps.cmd(ringCommand)
steps.ringCommand(ringCommand)
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/ru/pulsar/jenkins/library/steps/RingCommand.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.pulsar.jenkins.library.steps

import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.ioc.ContextRegistry

class RingCommand implements Serializable {

private String script;

RingCommand(String script) {
this.script = script
};

def run() {
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()

String ringMessage = steps.cmd(script, false, true)
nixel2007 marked this conversation as resolved.
Show resolved Hide resolved
if (ringMessage.contains("error")) {
steps.error(ringMessage)
}
}
}
27 changes: 15 additions & 12 deletions test/unit/groovy/ru/pulsar/jenkins/library/steps/CmdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ void runOk() {
final String script = "echo hello";
Cmd cmd = new Cmd(script);

when(steps.bat(anyString(), anyBoolean(), anyBoolean(), anyString())).thenReturn(0);
when(steps.sh(anyString(), anyBoolean(), anyBoolean(), anyString())).thenReturn(0);

// when
int run = cmd.run();
Object run = cmd.run();

// then
verify(steps).isUnix();
assertThat(steps).satisfiesAnyOf(
steps -> verify(steps).bat(contains(script), eq(false), anyString()),
steps -> verify(steps).sh(contains(script), eq(false), anyString())
steps -> verify(steps).bat(contains(script), eq(false), eq(false), anyString()),
steps -> verify(steps).sh(contains(script), eq(false), eq(false), anyString())
);

assertThat(run).isEqualTo(0);
Expand All @@ -49,8 +52,8 @@ void runFailNoReturn() {
Cmd cmd = new Cmd(script);

String thrownText = "failed";
when(steps.bat(anyString(), anyBoolean(), anyString())).thenThrow(new Error(thrownText));
when(steps.sh(anyString(), anyBoolean(), anyString())).thenThrow(new Error(thrownText));
when(steps.bat(anyString(), anyBoolean(), anyBoolean(), anyString())).thenThrow(new Error(thrownText));
when(steps.sh(anyString(), anyBoolean(), anyBoolean(), anyString())).thenThrow(new Error(thrownText));

// when
Throwable thrown = catchThrowable(cmd::run);
Expand All @@ -59,8 +62,8 @@ void runFailNoReturn() {
// then
verify(steps).isUnix();
assertThat(steps).satisfiesAnyOf(
steps -> verify(steps).bat(contains(script), eq(false), anyString()),
steps -> verify(steps).sh(contains(script), eq(false), anyString())
steps -> verify(steps).bat(contains(script), eq(false), eq(false), anyString()),
steps -> verify(steps).sh(contains(script), eq(false), eq(false), anyString())
);
}

Expand All @@ -70,17 +73,17 @@ void runPassAndReturn() {
final String script = "false";
Cmd cmd = new Cmd(script, true);

when(steps.bat(anyString(), anyBoolean(), anyString())).thenReturn(1);
when(steps.sh(anyString(), anyBoolean(), anyString())).thenReturn(1);
when(steps.bat(anyString(), anyBoolean(), anyBoolean(), anyString())).thenReturn(1);
when(steps.sh(anyString(), anyBoolean(), anyBoolean(), anyString())).thenReturn(1);

// when
int run = cmd.run();
Object run = cmd.run();

// then
verify(steps).isUnix();
assertThat(steps).satisfiesAnyOf(
steps -> verify(steps).bat(contains(script), eq(true), anyString()),
steps -> verify(steps).sh(contains(script), eq(true), anyString())
steps -> verify(steps).bat(contains(script), eq(true), eq(false), anyString()),
steps -> verify(steps).sh(contains(script), eq(true), eq(false), anyString())
);

assertThat(run).isEqualTo(1);
Expand Down
4 changes: 2 additions & 2 deletions vars/cmd.groovy
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ru.pulsar.jenkins.library.steps.Cmd
import ru.pulsar.jenkins.library.ioc.ContextRegistry

int call(String script, boolean returnStatus = false) {
def call(String script, boolean returnStatus = false, boolean returnStdout = false ) {
ContextRegistry.registerDefaultContext(this)

Cmd cmd = new Cmd(script, returnStatus)
Cmd cmd = new Cmd(script, returnStatus, returnStdout)
return cmd.run()
}
9 changes: 9 additions & 0 deletions vars/ringCommand.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.steps.RingCommand

def call(String script ) {
ContextRegistry.registerDefaultContext(this)

RingCommand ringCommand = new RingCommand(script)
return ringCommand.run()
}