|
| 1 | +import { ConfigurationError } from "@pipedream/platform"; |
| 2 | +import app from "../../jenkins.app.mjs"; |
| 3 | +import constants from "../../common/constants.mjs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + props: { |
| 7 | + app, |
| 8 | + db: "$.service.db", |
| 9 | + http: "$.interface.http", |
| 10 | + // eslint-disable-next-line pipedream/props-label, pipedream/props-description |
| 11 | + info: { |
| 12 | + type: "alert", |
| 13 | + alertType: "info", |
| 14 | + content: "Rmember to install the Jenkins Notification Plugin to use this source. Also make sure to activate and enable it in the job configuration.", |
| 15 | + }, |
| 16 | + jobName: { |
| 17 | + propDefinition: [ |
| 18 | + app, |
| 19 | + "jobName", |
| 20 | + ], |
| 21 | + }, |
| 22 | + }, |
| 23 | + hooks: { |
| 24 | + async deploy() { |
| 25 | + const { |
| 26 | + listPlugins, |
| 27 | + setPluginVersion, |
| 28 | + } = this; |
| 29 | + |
| 30 | + const { plugins } = await listPlugins({ |
| 31 | + params: { |
| 32 | + depth: 1, |
| 33 | + tree: "plugins[active,enabled,shortName,longName,version]", |
| 34 | + }, |
| 35 | + }); |
| 36 | + const plugin = plugins.find(({ shortName }) => shortName === "notification"); |
| 37 | + if (!plugin) { |
| 38 | + throw new ConfigurationError("The Jenkins Notification Plugin is not installed. Please install it to use this source."); |
| 39 | + } |
| 40 | + if (!plugin.active) { |
| 41 | + throw new ConfigurationError("The Jenkins Notification Plugin is not active. Please activate it to use this source."); |
| 42 | + } |
| 43 | + if (!plugin.enabled) { |
| 44 | + throw new ConfigurationError("The Jenkins Notification Plugin is not enabled. Please enable it to use this source."); |
| 45 | + } |
| 46 | + |
| 47 | + setPluginVersion(plugin.version); |
| 48 | + }, |
| 49 | + async activate() { |
| 50 | + const { |
| 51 | + http: { endpoint: url }, |
| 52 | + jobName, |
| 53 | + getConfig, |
| 54 | + updateConfig, |
| 55 | + getEventName, |
| 56 | + getPluginVersion, |
| 57 | + } = this; |
| 58 | + |
| 59 | + const { project } = await getConfig({ |
| 60 | + jobName, |
| 61 | + }); |
| 62 | + |
| 63 | + const { properties } = project; |
| 64 | + |
| 65 | + await updateConfig({ |
| 66 | + jobName, |
| 67 | + data: { |
| 68 | + project: { |
| 69 | + ...project, |
| 70 | + properties: { |
| 71 | + ...properties, |
| 72 | + "com.tikal.hudson.plugins.notification.HudsonNotificationProperty": { |
| 73 | + "@_plugin": `notification@${getPluginVersion()}`, |
| 74 | + "endpoints": { |
| 75 | + "com.tikal.hudson.plugins.notification.Endpoint": { |
| 76 | + "protocol": "HTTP", |
| 77 | + "format": "JSON", |
| 78 | + "urlInfo": { |
| 79 | + "urlOrId": url, |
| 80 | + "urlType": "PUBLIC", |
| 81 | + }, |
| 82 | + "event": getEventName(), |
| 83 | + "timeout": 30000, |
| 84 | + "loglines": 0, |
| 85 | + "buildNotes": "", |
| 86 | + "retries": 0, |
| 87 | + "branch": ".*", |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }); |
| 95 | + }, |
| 96 | + async deactivate() { |
| 97 | + const { |
| 98 | + jobName, |
| 99 | + getConfig, |
| 100 | + updateConfig, |
| 101 | + } = this; |
| 102 | + |
| 103 | + const { project } = await getConfig({ |
| 104 | + jobName, |
| 105 | + }); |
| 106 | + |
| 107 | + const { |
| 108 | + // eslint-disable-next-line no-unused-vars |
| 109 | + "com.tikal.hudson.plugins.notification.HudsonNotificationProperty": notificationProperty, |
| 110 | + ...properties |
| 111 | + } = project.properties; |
| 112 | + |
| 113 | + await updateConfig({ |
| 114 | + jobName, |
| 115 | + data: { |
| 116 | + project: { |
| 117 | + ...project, |
| 118 | + properties, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }); |
| 122 | + }, |
| 123 | + }, |
| 124 | + methods: { |
| 125 | + setPluginVersion(value) { |
| 126 | + this.db.set(constants.PLUGIN_VERSION, value); |
| 127 | + }, |
| 128 | + getPluginVersion() { |
| 129 | + return this.db.get(constants.PLUGIN_VERSION); |
| 130 | + }, |
| 131 | + generateMeta() { |
| 132 | + throw new ConfigurationError("generateMeta is not implemented"); |
| 133 | + }, |
| 134 | + getEventName() { |
| 135 | + throw new ConfigurationError("getEventName is not implemented"); |
| 136 | + }, |
| 137 | + processResource(resource) { |
| 138 | + this.$emit(resource, this.generateMeta(resource)); |
| 139 | + }, |
| 140 | + listPlugins(args = {}) { |
| 141 | + return this.app._makeRequest({ |
| 142 | + debug: true, |
| 143 | + path: "/pluginManager/api/json", |
| 144 | + ...args, |
| 145 | + }); |
| 146 | + }, |
| 147 | + getConfig({ |
| 148 | + jobName, ...args |
| 149 | + } = {}) { |
| 150 | + return this.app._makeRequest({ |
| 151 | + ...args, |
| 152 | + debug: true, |
| 153 | + path: `/job/${encodeURIComponent(jobName)}/config.xml`, |
| 154 | + headers: { |
| 155 | + "Content-Type": "text/xml", |
| 156 | + }, |
| 157 | + }); |
| 158 | + }, |
| 159 | + updateConfig({ |
| 160 | + jobName, ...args |
| 161 | + } = {}) { |
| 162 | + return this.app.post({ |
| 163 | + ...args, |
| 164 | + debug: true, |
| 165 | + path: `/job/${encodeURIComponent(jobName)}/config.xml`, |
| 166 | + headers: { |
| 167 | + "Content-Type": "text/xml", |
| 168 | + }, |
| 169 | + }); |
| 170 | + }, |
| 171 | + }, |
| 172 | + async run({ body }) { |
| 173 | + this.processResource(body); |
| 174 | + }, |
| 175 | +}; |
0 commit comments