aboutsummaryrefslogtreecommitdiff
path: root/src/index.ts
blob: 4955ba6bc2bcd10775a76dba710a7358bd3683ad (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import dotenv from "dotenv";
import express from "express";
import { JSDOM } from "jsdom";
import { badgen } from "badgen";
import { MongoClient } from "mongodb";
import path from "path";
import fs from "fs";

import formats, { Format } from "./formats";
import Metadata from "./metadata";

// Start-up configuration
dotenv.config();
const PORT = Number(process.env.PORT || 3000);
const TOKEN = process.env.TOKEN || "";
const UPLOAD_LIMIT = Number(process.env.UPLOAD_LIMIT || 4194304);
const MONGO_URI =
  process.env.MONGO_URI ||
  (() => {
    throw Error("MONGO_URI must be defined");
  })();
const MONGO_DB = process.env.MONGO_DB || "ao-coverage";
const HOST_DIR =
  process.env.HOST_DIR ||
  (() => {
    throw Error("HOST_DIR must be defined");
  })();

fs.accessSync(HOST_DIR, fs.constants.R_OK | fs.constants.W_OK);
if (!path.isAbsolute(HOST_DIR)) {
  throw Error("HOST_DIR must be an absolute path");
}

new MongoClient(MONGO_URI, { useUnifiedTopology: true }).connect(
  (err, mongo) => {
    if (err !== null) {
      throw err;
    }

    const app: express.Application = express();
    const metadata = new Metadata(mongo.db(MONGO_DB));

    // Upload HTML file
    app.post("/v1/:org/:repo/:branch/:commit.html", (req, res) => {
      const { org, repo, branch, commit } = req.params;
      console.info(
        "POST request to /v1/%s/%s/%s/%s.html",
        org,
        repo,
        branch,
        commit
      );

      const { token, format } = req.query;
      //TODO @Metadata token should come from metadata
      if (token != TOKEN) {
        return res.status(401).send("Invalid token");
      }

      if (!formats.list_formats().includes(format)) {
        return res.status(406).send("Report format unknown");
      }

      var contents = "";
      req.on("data", raw => {
        if (contents.length + raw.length > UPLOAD_LIMIT) {
          res.status(413).send("Uploaded file is too large");
        } else {
          contents += raw;
        }
      });
      req.on("end", () => {
        let formatter: Format, coverage: number;
        try {
          const doc = new JSDOM(contents).window.document;
          formatter = formats.get_format(format);
          coverage = formatter.parse_coverage(doc);
        } catch {
          return res.status(400).send("Invalid report document");
        }

        const badge = badgen({
          label: "coverage",
          status: Math.floor(coverage).toString() + "%",
          //TODO @Metadata stage values should come from metadata
          color: formatter.match_color(coverage, 95, 80)
        });

        const report_path = path.join(HOST_DIR, org, repo, branch, commit);

        fs.promises
          .mkdir(report_path, { recursive: true })
          .then(() =>
            fs.promises.writeFile(path.join(report_path, "badge.svg"), badge)
          )
          .then(() =>
            fs.promises.writeFile(
              path.join(report_path, "index.html"),
              contents
            )
          )
          .then(() =>
            metadata.updateBranch({ org, repo, name: branch, head: commit })
          )
          .then(result =>
            result
              ? res.status(200).send()
              : res.status(500).send("Unknown error occurred")
          );
      });
    });

    app.get("/v1/:org/:repo/:branch.svg", (req, res) => {
      const { org, repo, branch } = req.params;
      console.info("GET request to /v1/%s/%s/%s.svg", org, repo, branch);

      metadata.getHeadCommit(org, repo, branch).then(
        commit => {
          console.debug(
            "Found commit %s for ORB %s/%s/%s",
            commit,
            org,
            repo,
            branch
          );

          res.sendFile(
            path.join(HOST_DIR, org, repo, branch, commit, "badge.svg")
          );
        },
        () => {
          res.status(500).send("Unknown error occurred");
        }
      );
    });

    app.get("/v1/:org/:repo/:branch.html", (req, res) => {
      const { org, repo, branch } = req.params;
      console.info("GET request to /v1/%s/%s/%s.html", org, repo, branch);

      metadata.getHeadCommit(org, repo, branch).then(
        commit => {
          console.debug(
            "Found commit %s for ORB %s/%s/%s",
            commit,
            org,
            repo,
            branch
          );

          res.sendFile(
            path.join(HOST_DIR, org, repo, branch, commit, "index.html")
          );
        },
        () => {
          res.status(500).send("Unknown error occurred");
        }
      );
    });

    // provide hard link for commit
    app.get("/v1/:org/:repo/:branch/:commit.svg", (req, res) => {
      const { org, repo, branch, commit } = req.params;
      console.info(
        "GET request to /v1/%s/%s/%s/%s.svg",
        org,
        repo,
        branch,
        commit
      );

      res.sendFile(path.join(HOST_DIR, org, repo, branch, commit, "badge.svg"));
    });

    // provide hard link for commit
    app.get("/v1/:org/:repo/:branch/:commit.html", (req, res) => {
      const { org, repo, branch, commit } = req.params;
      console.info(
        "GET request to /v1/%s/%s/%s/%s.html",
        org,
        repo,
        branch,
        commit
      );

      res.sendFile(
        path.join(HOST_DIR, org, repo, branch, commit, "index.html")
      );
    });

    const server = app.listen(PORT, () => {
      console.log("Express started on port " + PORT);
    });

    // application exit handling
    const handle_closure = (signal: NodeJS.Signals) => {
      console.log("%s signal received. Closing shop.", signal);

      mongo.close().then(() => {
        console.log("Mongo client connection closed.");
        server.close(() => {
          console.log("Express down.");
          process.exit();
        });
      });
    };

    const handle_codes: NodeJS.Signals[] = ["SIGTERM", "SIGHUP", "SIGINT"];
    const process_event = (code: NodeJS.Signals) =>
      process.on(code, handle_closure);
    handle_codes.map(process_event);
  }
);