2

Any idea why the IF-ELSE below works

def checkPrValidity() {
    wordCountStr = sh returnStdout: true, script: 'git diff --ignore-space-at-eol $target_branch..PRbranch src | wc -l' 
    wordCount = wordCountStr.toInteger() //force conversion to int data type 
    if (wordCount == 0) {
        return false;
    } else {
        println("This is a valid PR, continue the job execution")
        return true;
    }
}

while the one below doesn't

def checkPrValidity() {
    wordCountStr = sh returnStdout: true, script: 'git diff --ignore-space-at-eol $target_branch..PRbranch src | wc -l'
    if (wordCountStr == '0') {
        return false;
    } else {
        println("This is a valid PR, continue the job execution")
        return true;
    }
}

Why do i need to specifically convert a string to Integer, while it fails to compare it as a string data type?

daspilker
  • 8,154
  • 1
  • 35
  • 49
OK999
  • 1,353
  • 2
  • 19
  • 39

1 Answers1

8

Try to add .trim() to your sh. Because in output there can be new line, which prevents correct comparison.

FCh
  • 667
  • 4
  • 12