Passed
Push — development ( b9ff40...abd635 )
by André
02:02
created

src/out.js   A

Complexity

Total Complexity 21
Complexity/F 5.25

Size

Lines of Code 100
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 57
mnd 17
bc 17
fnc 4
dl 0
loc 100
rs 10
bpm 4.25
cpm 5.25
noi 1
c 0
b 0
f 0
1
const mqtt = require('async-mqtt')
2
const configuration = require('./configuration')
3
const validate = require('./validate')
4
5
module.exports = (input, callback) => {
6
7
	let error = null
8
	let output = {}
9
10
	validate.sourceConfiguration(input, (validatedInput, thrownError) => {
11
		input = validatedInput
12
		error = thrownError
13
	})
14
	validate.paramConfiguration(input, (validatedInput, thrownError) => {
15
		input = validatedInput
16
		error = thrownError
17
	})
18
19
	// Send MQTT
20
	if (!error) {
21
		let configurationMqtt = configuration.mqtt(input)
22
		let client = mqtt.connect(input.source.url, configurationMqtt)
23
24
		input.params.qos = 0
25
		const sendMessage = async () => {
26
			try {
27
				await client.publish(
28
					input.params.topic,
29
					input.params.payload.toString()
30
				)
31
				output = {
32
					'version': {'message': input.params.payload.toString()},
33
					'metadata': [
34
						{'name': 'ATC_EXTERNAL_URL', 'value': process.env.ATC_EXTERNAL_URL || 'not set'},
35
						{'name': 'BUILD_ID', 'value': process.env.BUILD_ID || 'not set'},
36
						{'name': 'BUILD_NAME', 'value': process.env.BUILD_NAME || 'not set'},
37
						{'name': 'BUILD_PIPELINE_NAME', 'value': process.env.BUILD_PIPELINE_NAME || 'not set'},
38
						{'name': 'BUILD_TEAM_NAME', 'value': process.env.BUILD_TEAM_NAME || 'not set'},
39
						{'name': 'source.url', 'value': input.source.url || 'not set'},
40
						{'name': 'source.port', 'value': input.source.port.toString() || 'not set'},
41
						{'name': 'params.payload', 'value': input.params.payload || 'not set'},
42
						{'name': 'params.topic', 'value': input.params.topic || 'not set'},
43
						{'name': 'params.qos', 'value': input.params.qos.toString() || 'not set'}
44
					]
45
				}
46
				await client.end()
47
				callback(error, output)
48
			} catch (e) {
49
				callback(e.stack, {})
50
			}
51
		}
52
		client.on('connect', sendMessage)
53
	}
54
}
55