aboutsummaryrefslogtreecommitdiff
path: root/test/ufuzz/actions.js
blob: b4d8c86bca3e0616429eef8638d6d92680cd9fb6 (about) (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
var get = require("https").get;
var parse = require("url").parse;

var base, token, run_number, eldest = true;
exports.init = function(url, auth, num) {
    base = url;
    token = auth;
    run_number = num;
};
exports.should_stop = function(callback) {
    read(base + "/actions/runs?per_page=100", function(reply) {
        if (!reply || !Array.isArray(reply.workflow_runs)) return;
        var runs = reply.workflow_runs.filter(function(workflow) {
            return workflow.status != "completed";
        }).sort(function(a, b) {
            return b.run_number - a.run_number;
        });
        if (runs.length < 10) return;
        var found = false, remaining = 20;
        (function next() {
            if (!runs.length) return;
            var workflow = runs.pop();
            if (workflow.run_number == run_number) found = true;
            read(workflow.jobs_url, function(reply) {
                if (!reply || !Array.isArray(reply.jobs)) return;
                if (!reply.jobs.every(function(job) {
                    if (job.status == "completed") return true;
                    remaining--;
                    return found;
                })) return;
                if (remaining >= 0) {
                    next();
                } else {
                    callback();
                }
            });
        })();
    });
};

function read(url, callback) {
    var options = parse(url);
    options.headers = {
        "Authorization": "Token " + token,
        "User-Agent": "UglifyJS",
    };
    get(options, function(response) {
        var chunks = [];
        response.setEncoding("utf8");
        response.on("data", function(chunk) {
            chunks.push(chunk);
        }).on("end", function() {
            callback(JSON.parse(chunks.join("")));
        });
    });
}