summaryrefslogtreecommitdiff
path: root/.github/scripts/pr-comment.js
blob: 9d03eb54873d4aa2e8c6cb367908a2f433122fe1 (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
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
module.exports = async({ github, context, state, pr_number }) => {

    console.log(`Workflow: ${JSON.stringify(context.payload.workflow_run,null,2)}`);

    /* We use this link service as github does not (yet) expose an API where
       you can download an artifact.
       https://github.com/actions/upload-artifact/issues/50
    */
    var nightlyURL = (artifact) => {
        return `https://nightly.link/${context.repo.owner}/${context.repo.repo}/actions/artifacts/${artifact.id}.zip`
    };

    const artifacts = await github.paginate(
        github.rest.actions.listWorkflowRunArtifacts,
        {
            owner: context.repo.owner,
            repo: context.repo.repo,
            run_id: context.payload.workflow_run.id,
            per_page: 100
        });

    const ct_logs = artifacts.find(
        (a) => { return a.name == 'test_results'; });
    const html_docs = artifacts.find(
        (a) => { return a.name == 'otp_doc_html'; });
    const win_exe = artifacts.find(
        (a) => { return a.name.match(/win32/); });

    let gh_comments = await github.paginate(
        github.rest.issues.listComments,
        {
            owner: context.repo.owner,
            repo: context.repo.repo,
            issue_number: pr_number,
            per_page: 100
        });

    /* We find the newest comment by "github-actions[bot]". There really should
       only be one, but the PR scripts may be buggy and if so we want to updated
       the latest post. */
    gh_comments.reverse();
    const gh_comment = gh_comments.find(c => c.user.login == "github-actions[bot]");

    console.log(`Comment to update: ${JSON.stringify(gh_comment,null,2)}`);

    let ct_body;

    /* The EnricoMi/publish-unit-test-result-action@v1 action creates/updates a comment
       to always start with "## CT Test Results". Below we append some data to that
       comment to further help the user.
    */

    if (gh_comment && !gh_comment.body.match("<!-- marker -->")) {
        /* If the comment does not have a marker, it has been touched by
           EnricoMi/publish-unit-test-result-action@v1 and then we need
           to update the comment */
        ct_body = gh_comment.body;
    } else if (gh_comment && state == 'starting') {
        /* If the comment exists and we are just starting the workflow we do nothing */
        return;
    } else {
        /* if the comment does not exist we use a place holder comment. This
           needs to start with "## CT Test Results" and
           contain "Results for commit" as otherwise
           EnricoMi/publish-unit-test-result-action@v1 will create a new comment. */
        ct_body = "## CT Test Results\n\n";
        if (state == 'starting') {
            ct_body += `Tests are running... ${context.payload.workflow_run.html_url}\n\n`;
        } else {
            ct_body += "No tests were run for this PR. This is either because the build failed, or the PR is based on a branch without GH actions tests configured.\n\n";
        }
        ct_body += `Results for commit ${context.payload.workflow_run.head_sha}`;
    }

    console.log(`ct_body: ${ct_body}`);

    const body = `${ct_body}

<!-- marker -->

To speed up review, make sure that you have read [Contributing to Erlang/OTP](/${context.repo.owner}/${context.repo.repo}/blob/master/CONTRIBUTING.md) and that all [checks](/${context.repo.owner}/${context.repo.repo}/pull/${pr_number}/checks) pass.

See the [TESTING](/${context.repo.owner}/${context.repo.repo}/blob/master/HOWTO/TESTING.md) and [DEVELOPMENT](/${context.repo.owner}/${context.repo.repo}/blob/master/HOWTO/DEVELOPMENT.md) HowTo guides for details about how to run test locally.

## Artifacts
* ` + (ct_logs ? `[Complete CT logs](https://erlang.github.io/prs/${pr_number}/ct_logs/index.html) ([Download Logs](${nightlyURL(ct_logs)}))` : "No CT logs found") + `
* ` + (html_docs ? `[HTML Documentation](https://erlang.github.io/prs/${pr_number}/doc/index.html) ([Download HTML Docs](${nightlyURL(html_docs)}))` : "No HTML docs found") + `
* ` + (win_exe ? `[Windows Installer](${nightlyURL(win_exe)})` : "No Windows Installer found") + `

// Erlang/OTP Github Action Bot
`;
    if (gh_comment) {
        if (gh_comment.body != body) {
            console.log("Update comment: " + gh_comment.id);
            await github.rest.issues.updateComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                comment_id: gh_comment.id,
                body: body
            });
        }
    } else {
        await github.rest.issues.createComment({
            owner: context.repo.owner,
            repo: context.repo.repo,
            issue_number: pr_number,
            body: body
        });
    }
};