summaryrefslogtreecommitdiffstats
path: root/sshTranslator.js
blob: c1504e229c69dacfda2a77a1aa6c69d2c0dd454d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

const http = require('http');
const { spawn } = require('child_process');
const EventEmitter = require('events');

class SSHForwarder extends EventEmitter {
  constructor() {
    super();
    this.sshProcess = null;
  }

  start() {
    this.sshProcess = spawn('ssh', [
      'codereview.qt-project.org',
      'gerrit',
      'stream-events',
      '-s',
      'change-integration-fail',
      '-s',
      'comment-added',
    ]);

    this.sshProcess.stdout.on('data', (data) => {
      try {
        console.log(data.toString());
        const jsonData = data.toString();
        this.forwardEvent(jsonData);
      } catch (err) {
        console.error('Error processing event:', err);
      }
    });

    this.sshProcess.stderr.on('data', (data) => {
      console.error(`SSH stderr: ${data}`);
    });

    this.sshProcess.on('close', (code) => {
      console.log(`SSH process exited with code ${code}`);
      setTimeout(() => this.start(), 5000); // Reconnect after 5 seconds
    });
  }

  forwardEvent(eventData) {
    const postData = eventData;

    const req = http.request({
      hostname: 'localhost',
      port: 8092,
      path: '/',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': postData.length
      }
    });

    req.on('error', (err) => {
      console.error('Forwarding error:', err);
    });

    req.write(postData);
    req.end();
  }
}

// Create and start forwarder
const forwarder = new SSHForwarder();
forwarder.start();