Passed
Push — master ( d06fb8...9f4cb8 )
by André
03:15 queued 01:29
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
	// if file exist do:
20
	//   message = require('fs').readFile('/tmp/build/put/out/message', (err,buf) => { process.stdout.write(buf)})
21
	// endif
22
	// 	{'name': 'pwd', 'value': process.cwd()},
23
24
	// Send MQTT
25
	if (!error) {
26
		let configurationMqtt = configuration.mqtt(input)
27
		let client = mqtt.connect(input.source.url, configurationMqtt)
28
		input.params.qos = 0
29
		const sendMessage = async () => {
30
			try {
31
				await client.publish(
32
					input.params.topic,
33
					input.params.payload.toString()
34
				)
35
				output = {
36
					'version': {
37
						'ref': client.getLastMessageId().toString() ||  'none',
38
						'message': input.params.payload.toString()
39
					},
40
					'metadata': [
41
						{'name': 'ATC_EXTERNAL_URL', 'value': process.env.ATC_EXTERNAL_URL || 'not set'},
42
						{'name': 'BUILD_ID', 'value': process.env.BUILD_ID || 'not set'},
43
						{'name': 'BUILD_NAME', 'value': process.env.BUILD_NAME || 'not set'},
44
						{'name': 'BUILD_JOB_NAME', 'value': process.env.BUILD_JOB_NAME || 'not set'},
45
						{'name': 'BUILD_JOB_ID', 'value': process.env.BUILD_JOB_ID || 'not set'},
46
						{'name': 'BUILD_PIPELINE_NAME', 'value': process.env.BUILD_PIPELINE_NAME || 'not set'},
47
						{'name': 'BUILD_PIPELINE_ID', 'value': process.env.BUILD_PIPELINE_ID || 'not set'},
48
						{'name': 'BUILD_TEAM_NAME', 'value': process.env.BUILD_TEAM_NAME || 'not set'},
49
						{'name': 'MQTT_LAST_MESSAGE_ID', 'value': client.getLastMessageId().toString() || 'not set'},
50
						{'name': 'source.url', 'value': input.source.url || 'not set'},
51
						{'name': 'source.port', 'value': input.source.port.toString() || 'not set'},
52
						{'name': 'params.payload', 'value': input.params.payload || 'not set'},
53
						{'name': 'params.topic', 'value': input.params.topic || 'not set'},
54
						{'name': 'params.qos', 'value': input.params.qos.toString() || 'not set'}
55
					]
56
				}
57
				await client.end()
58
				callback(error, output)
59
			} catch (e) {
60
				callback(e.stack, {})
61
			}
62
		}
63
		client.on('connect', sendMessage)
64
	}
65
}
66