aboutsummaryrefslogtreecommitdiff
path: root/test/ufuzz/actions.js
blob: ac494940dcfd57bcde3cda000c4f9298c7a62797 (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
57
58
59
60
61
62
63
64
65
66
67
68
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.sort(function(a, b) {
            return b.run_number - a.run_number;
        });
        var found = false, remaining = 20;
        (function next() {
            var workflow;
            do {
                workflow = runs.pop();
                if (!workflow) return;
                if (workflow.event == "schedule" && workflow.run_number == run_number) found = true;
            } while (!found && workflow.status == "completed");
            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 || workflow.event != "schedule";
                })) return;
                if (remaining >= 0) {
                    next();
                } else {
                    callback();
                }
            });
        })();
    });
};

function read(url, callback) {
    var done = function(reply) {
        done = function() {};
        callback(reply);
    };
    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() {
            var reply;
            try {
                reply = JSON.parse(chunks.join(""))
            } catch (e) {}
            done(reply);
        }).on("error", function() {
            done();
        });
    }).on("error", function() {
        done();
    });
}