summary refs log tree commit diff
path: root/.local/bin/jira-CR.php
blob: 7e5f55fc7f7b9cd69f6156fb3f18bb8300a6e69c (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env php
<?php

// shim before php 8.4
if (!function_exists('array_any')) {
    function array_any(array $array, callable $callback): bool {
        foreach ($array as $key => $value) {
            if ($callback($value, $key)) {
                return true;
            }
        }
        return false;
    }
}

$ansired = "\033[31m";
$ansigreen = "\033[32m";
$ansireset = "\033[0m";

$padlen = 50;
print(str_pad("\rGetting tickets from jira...", $padlen));
$ticket_lines = explode("\n", rtrim(shell_exec("jira-cli issues list -q'project IS NOT EMPTY and project != AUTO and project != NET and project != MSP and project != SYST and project != LOYAL' --order-by='Sprint' -s'Code Review' --columns=KEY,LABELS --plain | grep -v '^KEY\b'")));
$tickets = array_map(fn($x) => explode("\t", $x)[0], $ticket_lines);
$ticketLabels = array_combine(
    $tickets,
    array_map(fn($x) => explode(',', explode("\t", $x)[1] ?? ''), $ticket_lines)
);

$params = 'title,url,statusCheckRollup,reviews,author,reviewDecision,reviewRequests,headRefName,mergeable,additions,deletions';

print(str_pad("\rGetting OnBDC2 PR's from github...", $padlen));
$prs_jsons[] = json_decode(shell_exec("gh pr list -L 50 -R BetterCarPeople/OnBDC2 --json '$params'"), true);
print(str_pad("\rGetting maple-ai PR's from github...", $padlen));
$prs_jsons[] = json_decode(shell_exec("gh pr list -R BetterCarPeople/maple-ai --json '$params'"), true);
print(str_pad("\rGetting maple-web PR's from github...", $padlen));
$prs_jsons[] = json_decode(shell_exec("gh pr list -R BetterCarPeople/maple-web --json '$params'"), true);

print(str_pad("\r", $padlen));
print("\r");

$prs_json = array_merge(...$prs_jsons);
$output = "";
$has_pr = [];
foreach ($prs_json as $pr) {
    $pr_ticket = null;
    $pr_ticket = in_array($pr['headRefName'], $tickets) ? $pr['headRefName'] : null;
    $pr_ticket ??= (preg_match("/^[^: ]*/", $pr['title'], $matches) && in_array($matches[0], $tickets)) ? $matches[0] : null;
    if ($pr_ticket) {
        $has_pr[$pr_ticket] = true;
        // These are github PR's that are in code review in Jira
        $title = str_pad($pr['title'], 40);
        if (strlen($title) > 40) {
            $title = substr($title,0,40-3)."...";
        }
        $author = $pr['author']['login'];

        $labels = $ticketLabels[$pr_ticket] ?? [];
        $labelsText = trim(
            (in_array("Successful_Demonstration", $labels) ? "๐Ÿ“บ Demo'd " : '')
            . (in_array("Hot", $labels) ? "๐Ÿ”ฅ Hot " : '')
        );

        $plusminus = $ansigreen . "+" . $pr['additions'] . $ansired . "-" . $pr['deletions'] . $ansireset;

        $mergeable = $pr['mergeable'] == 'CONFLICTING' ? '๐Ÿšง: Merge conflicts. ' : '';

        // Show pipeline status if failed
        $failedPipes = [];
        foreach ($pr['statusCheckRollup'] as $scr) {
            if ($scr['conclusion'] === 'FAILURE') {
                $failedPipes[] = $scr['name'];
            }
        }
        $failed = $failedPipes ? (str_repeat("๐Ÿงช๐Ÿ’”", count($failedPipes)) . ": " . implode(", ", $failedPipes) . '. ') : '';

        $pipeline_status = $failed;

        // Show approvers
        $review_status = "";

        $changeNames = [];
        foreach ($pr['reviews'] as $r) {
            if ($r['state'] === "CHANGES_REQUESTED") {
                $changeNames[] = $r['author']['login'];
            }
        }
        $changeNames = array_unique($changeNames);

        $requestNames = [];
        foreach ($pr['reviewRequests'] as $rr) {
            if ($rr['__typename'] === "Team") {
                $requestNames[] = $rr['name'];
            } else {
                $requestNames[] = $rr['login'];
            }
        }
        $requestNames = array_unique($requestNames);

        $approvers = [];
        foreach ($pr['reviews'] as $r) {
            if ($r['state'] === "APPROVED") {
                $approvers[] = $r['author']['login'];
            }
        }

        $commentNames = [];
        foreach ($pr['reviews'] as $r) {
            $newName = $r['author']['login'];
            if ($r['state'] === "COMMENTED" && $newName != $author && !array_any([...$approvers, ...$requestNames, ...$changeNames], fn($n)=>$n===$newName)) {
                $commentNames[] = $newName;
            }
        }
        $commentNames = array_unique($commentNames);

        $changes = $changeNames ? (str_repeat("โŒ", count($changeNames)) . ": " . implode(", ", $changeNames) . '. ') : '';
        $reviewRequests = $requestNames ? (str_repeat("๐ŸŸก", count($requestNames)) . ": " . implode(", ", $requestNames) . '. ') : '';
        $approvals = $approvers ? (str_repeat("โœ…", count($approvers)) . ": " . implode(", ", $approvers) . '. ') : '';
        $comments = $commentNames ? (str_repeat("๐Ÿ’ฌ", count($commentNames)) . ": " . implode(", ", $commentNames) . '. ') : '';

        $review_status = $changes . $reviewRequests . $approvals . $comments;
        
        echo "$pr[url]?T=$title($author)$labelsText$plusminus$mergeable$pipeline_status$review_status\n";
    }
}
$has_no_pr = array_diff($tickets, array_keys($has_pr));
foreach ($has_no_pr as $ticket) {
    echo "https://proactivedealersolutions.atlassian.net/browse/$ticket Has no PR\n";
}

exit(1);