#328 Fix 65535 string length issue

This commit is contained in:
Mikolaj Karebski 2020-04-24 11:47:10 +02:00
parent 9715217f1d
commit fd1b5f2bd9
2 changed files with 65 additions and 5 deletions

View File

@ -35,16 +35,49 @@ func (c *ConfigurationAsCode) Ensure(jenkins *v1alpha2.Jenkins) (requeue bool, e
return c.groovyClient.Ensure(func(name string) bool { return c.groovyClient.Ensure(func(name string) bool {
return strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml") return strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml")
}, func(groovyScript string) string { }, func(groovyScript string) string {
return fmt.Sprintf(applyConfigurationAsCodeGroovyScriptFmt, groovyScript) return fmt.Sprintf(applyConfigurationAsCodeGroovyScriptFmt, prepareScript(groovyScript))
}) })
} }
const applyConfigurationAsCodeGroovyScriptFmt = ` const applyConfigurationAsCodeGroovyScriptFmt = `
def config = ''' String[] configContent = ['''%s''']
%s def configSb = new StringBuffer()
''' for (int i=0; i<configContent.size(); i++) {
def stream = new ByteArrayInputStream(config.getBytes('UTF-8')) configSb << configContent[i]
}
def stream = new ByteArrayInputStream(configSb.toString().getBytes('UTF-8'))
def source = new io.jenkins.plugins.casc.yaml.YamlSource(stream, io.jenkins.plugins.casc.yaml.YamlSource.READ_FROM_INPUTSTREAM) def source = new io.jenkins.plugins.casc.yaml.YamlSource(stream, io.jenkins.plugins.casc.yaml.YamlSource.READ_FROM_INPUTSTREAM)
io.jenkins.plugins.casc.ConfigurationAsCode.get().configureWith(source) io.jenkins.plugins.casc.ConfigurationAsCode.get().configureWith(source)
` `
func prepareScript(script string) string {
groovyUtf8MaxStringLength := 65535
var slicedScript []string
if len(script) > groovyUtf8MaxStringLength {
slicedScript = splitTooLongScript(script)
} else {
slicedScript = append(slicedScript, script)
}
return strings.Join(slicedScript, "''','''")
}
func splitTooLongScript(groovyScript string) []string {
groovyUtf8MaxStringLength := 65535
var slicedGroovyScript []string
lastSubstrIndex := len(groovyScript) % groovyUtf8MaxStringLength
lastSubstr := groovyScript[len(groovyScript)-lastSubstrIndex:]
substrNumber := len(groovyScript) / groovyUtf8MaxStringLength
for i := 0; i < substrNumber; i++ {
scriptSubstr := groovyScript[i*groovyUtf8MaxStringLength : (i*groovyUtf8MaxStringLength)+groovyUtf8MaxStringLength]
slicedGroovyScript = append(slicedGroovyScript, scriptSubstr)
}
slicedGroovyScript = append(slicedGroovyScript, lastSubstr)
return slicedGroovyScript
}

File diff suppressed because one or more lines are too long