Skip to content

Commit

Permalink
Unescape HTML entities during state diff (#63)
Browse files Browse the repository at this point in the history
This solves a state inconsistency whereby if you create a Jenkins job using XML that contains HTML entities, on subsequent plans, it will show an unclean diff, bouncing between escaped & unescaped HTML entities. The Jenkins job itself works perfectly fine but this unclean diff can make plans annoying, and if you have many jobs provisioned, the XML template can take up a large portion of the plan.
zahiar authored Sep 11, 2021

Verified

This commit was signed with the committer’s verified signature.
rjackson Rob Jackson
1 parent 8d11b81 commit 4b89d6f
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions jenkins/util.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package jenkins
import (
"context"
"fmt"
"html"
"log"
"regexp"
"strings"
@@ -83,9 +84,11 @@ func templateDiff(k, old, new string, d *schema.ResourceData) bool {
old = re.ReplaceAllString(old, "")
old = strings.Replace(old, " ", "", -1)
old = strings.TrimSpace(old)
old = html.UnescapeString(old)
new = re.ReplaceAllString(new, "")
new = strings.Replace(new, " ", "", -1)
new = strings.TrimSpace(new)
new = html.UnescapeString(new)

log.Printf("[DEBUG] jenkins::diff - Old: %q", old)
log.Printf("[DEBUG] jenkins::diff - New: %q", new)
18 changes: 18 additions & 0 deletions jenkins/util_test.go
Original file line number Diff line number Diff line change
@@ -108,6 +108,24 @@ func TestTemplateDiff(t *testing.T) {
}
}

func TestTemplateDiff_HTMLEntities(t *testing.T) {
job := resourceJenkinsFolder()
bag := job.TestResourceData()
_ = bag.Set("description", "Case")

inputLeft := "<root>&apos;/&apos;</root>"
inputRight := "<root>'/'</root>"
if actual := templateDiff("", inputLeft, inputRight, bag); !actual {
t.Errorf("Expected %s to be considered equal to %s", inputLeft, inputRight)
}

inputLeft = "<root>'/'</root>"
inputRight = "<root>&apos;/&apos;</root>"
if actual := templateDiff("", inputLeft, inputRight, bag); !actual {
t.Errorf("Expected %s to be considered equal to %s", inputLeft, inputRight)
}
}

func TestGenerateCredentialID(t *testing.T) {
inputFolder, inputName := "test-folder", "test-name"
actual := generateCredentialID(inputFolder, inputName)

0 comments on commit 4b89d6f

Please sign in to comment.