Sergey Otpuschennikov | 3e25b6b | 2020-09-11 19:45:00 +0400 | [diff] [blame] | 1 | #!groovy |
| 2 | @Grab('org.yaml:snakeyaml:1.17') |
| 3 | import jenkins.model.Jenkins; |
| 4 | import jenkins.model.GlobalConfiguration |
| 5 | import net.sf.json.JSONArray; |
| 6 | import net.sf.json.JSONObject |
| 7 | import org.yaml.snakeyaml.Yaml |
| 8 | import io.jenkins.plugins.casc.ConfigurationAsCode |
| 9 | import jenkins.security.UpdateSiteWarningsConfiguration |
| 10 | |
| 11 | Yaml parser = new Yaml() |
| 12 | |
| 13 | def casc = ConfigurationAsCode.get() |
| 14 | def configPath = casc.getStandardConfig()[0] |
| 15 | def symlFiles = new FileNameFinder().getFileNames(configPath, '**/*.syml', '.**/ **/.*') |
| 16 | def configs = [] |
| 17 | symlFiles.each { |
| 18 | configs = configs + parser.load((it as File).text) |
| 19 | } |
| 20 | |
| 21 | class Globals { |
| 22 | public static Boolean compareObjects( Object a, b) { |
| 23 | return Jenkins.XSTREAM.toXML(a) == Jenkins.XSTREAM.toXML(b) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | class UpdateSiteWarnings { |
| 28 | UpdateSiteWarnings() {} |
| 29 | Boolean changed = false |
| 30 | def instance = Jenkins.instance |
| 31 | void configure(params) { |
| 32 | def config = GlobalConfiguration.all().get(UpdateSiteWarningsConfiguration.class) |
| 33 | def _config = new UpdateSiteWarningsConfiguration() |
| 34 | |
| 35 | _config.configure(null, JSONObject.fromObject(params)) |
| 36 | if (! Globals.compareObjects(config, _config)) { |
| 37 | config.configure(null, JSONObject.fromObject(params)) |
| 38 | changed = true |
| 39 | } |
| 40 | _config = null |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | class CSP { |
| 45 | CSP() {} |
| 46 | Boolean changed = false |
| 47 | def instance = Jenkins.instance |
| 48 | void configure(param) { |
| 49 | String currentPolicy = System.getProperty("hudson.model.DirectoryBrowserSupport.CSP") |
| 50 | String newPolicy = param.policy.trim() |
| 51 | if ( currentPolicy != newPolicy ){ |
| 52 | System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", newPolicy) |
| 53 | changed = true |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | class GerritTrigger { |
| 59 | GerritTrigger() {} |
| 60 | |
| 61 | def instance = Jenkins.instance |
| 62 | Boolean changed = false |
| 63 | def defaultServerParam = [ |
| 64 | noConnectionOnStartup: false, |
| 65 | gerritBuildStartedVerifiedValue: 0, |
| 66 | gerritBuildSuccessfulVerifiedValue: 1, |
| 67 | gerritBuildFailedVerifiedValue: -1, |
| 68 | gerritBuildUnstableVerifiedValue: 0, |
| 69 | gerritBuildNotBuiltVerifiedValue: 0, |
| 70 | gerritBuildStartedCodeReviewValue: 0, |
| 71 | gerritBuildSuccessfulCodeReviewValue: 0, |
| 72 | gerritBuildFailedCodeReviewValue: 0, |
| 73 | gerritBuildUnstableCodeReviewValue: -1, |
| 74 | gerritBuildNotBuiltCodeReviewValue: 0, |
| 75 | gerritVerifiedCmdBuildStarted: |
| 76 | "gerrit review <CHANGE>,<PATCHSET> --message 'Build Started <BUILDURL> <STARTED_STATS>' --verified <VERIFIED> --code-review <CODE_REVIEW>", |
| 77 | gerritVerifiedCmdBuildSuccessful: |
| 78 | "gerrit review <CHANGE>,<PATCHSET> --message 'Build Successful <BUILDS_STATS>' --verified <VERIFIED> --code-review <CODE_REVIEW>", |
| 79 | gerritVerifiedCmdBuildFailed: |
| 80 | "gerrit review <CHANGE>,<PATCHSET> --message 'Build Failed <BUILDS_STATS>' --verified <VERIFIED> --code-review <CODE_REVIEW>", |
| 81 | gerritVerifiedCmdBuildUnstable: |
| 82 | "gerrit review <CHANGE>,<PATCHSET> --message 'Build Unstable <BUILDS_STATS>' --verified <VERIFIED> --code-review <CODE_REVIEW>", |
| 83 | gerritVerifiedCmdBuildNotBuilt: |
| 84 | "gerrit review <CHANGE>,<PATCHSET> --message 'No Builds Executed <BUILDS_STATS>' --verified <VERIFIED> --code-review <CODE_REVIEW>", |
| 85 | verdictCategories: [ |
| 86 | [ verdictValue: 'Code-Review', verdictDescription: 'Code Review' ], |
| 87 | [ verdictValue: 'Verified', verdictDescription: 'Verified' ], |
| 88 | ] |
| 89 | ] |
| 90 | |
| 91 | Boolean compareObjects( Object a, b) { |
| 92 | String aXML = Jenkins.XSTREAM.toXML(a).replaceAll(/\{AQA[^\}]+\}/) { |
| 93 | hudson.util.Secret.decrypt(it) } |
| 94 | String bXML = Jenkins.XSTREAM.toXML(b).replaceAll(/\{AQA[^\}]+\}/) { |
| 95 | hudson.util.Secret.decrypt(it) } |
| 96 | return aXML == bXML |
| 97 | } |
| 98 | |
| 99 | void configure(params) { |
| 100 | if ( Jenkins.instance.pluginManager.activePlugins.find { it.shortName == "gerrit-trigger" } == null ) { |
| 101 | return |
| 102 | } |
| 103 | def gerritPlugin = Jenkins.instance.getPlugin(com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl.class); |
| 104 | |
| 105 | // Configure common parameters |
| 106 | def pluginConfig = gerritPlugin.getPluginConfig() |
| 107 | def _pluginConfig = new com.sonyericsson.hudson.plugins.gerrit.trigger.config.PluginConfig() |
| 108 | |
| 109 | _pluginConfig.setValues(JSONArray.fromObject(params)) |
| 110 | if (! compareObjects(pluginConfig, _pluginConfig)) { |
| 111 | pluginConfig.setValues(JSONArray.fromObject(params)) |
| 112 | changed = true |
| 113 | } |
| 114 | _pluginConfig = null |
| 115 | |
| 116 | // Configure servers |
| 117 | params.servers.each { _name, _params -> |
| 118 | def __params = defaultServerParam + _params |
| 119 | def values = JSONArray.fromObject(__params) |
| 120 | def server = gerritPlugin.getServer(_name) |
| 121 | |
| 122 | if (! server) { |
| 123 | server = new com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer( |
| 124 | _name, __params.noConnectionOnStartup) |
| 125 | } |
| 126 | def config = server.getConfig() |
| 127 | def _server = new com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer( |
| 128 | _name, __params.noConnectionOnStartup) |
| 129 | def _config = _server.getConfig() |
| 130 | _config.setValues(values) |
| 131 | _server.setConfig(_config) |
| 132 | if (! compareObjects(server, _server)) { |
| 133 | changed = true |
| 134 | if ( gerritPlugin.containsServer(_name) ) { |
| 135 | gerritPlugin.removeServer(gerritPlugin.getServer(_name)) |
| 136 | } |
| 137 | gerritPlugin.addServer(_server) |
| 138 | _server.start() |
| 139 | if (! __params.noConnectionOnStartup) { |
| 140 | _server.startConnection() |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | configs.each { params -> |
| 148 | params.each { _name, _params -> |
| 149 | def clazz = Class.forName(_name) |
| 150 | if (clazz) { |
| 151 | def act = clazz.newInstance() |
| 152 | act.configure(_params) |
| 153 | if (act.changed) { |
| 154 | act.instance.save() |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |