BitBucket Advanced Statuses

Show advanced statuses

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         BitBucket Advanced Statuses
// @version      1.0
// @description  Show advanced statuses
// @icon         https://clipground.com/images/png-connection-status.png
// @author       Mihai Stancu
// @license      MIT
// @namespace    msus
// @match        https://bitbucket.org/*/*/pull-requests*
// @grant        GM_addStyle
// @run-at       document-idle
// ==/UserScript==

(() => new MutationObserver(main).observe(document.querySelector('body'), {childList: true, subtree: true}))();

function main(mutations, observer) {
    const PRs = document.querySelectorAll('[data-qa=pull-request-row]');
    if (!PRs[0]) {
        return;
    }

    observer.disconnect();
    const table = PRs[0].closest('table');

    table.insertAdjacentHTML('beforeend', '<tbody class="pseudo-header"><tr><th colspan=15>Approved</th></tr></tbody>');
    table.insertAdjacentHTML('beforeend', '<tbody class="approved"></tbody>');
    table.insertAdjacentHTML('beforeend', '<tbody class="pseudo-header"><tr><th colspan=15>Needs review</th></tr></tbody>');
    table.insertAdjacentHTML('beforeend', '<tbody class="needs-review"></tbody>');
    table.insertAdjacentHTML('beforeend', '<tbody class="pseudo-header"><tr><th colspan=15>In progress</th></tr></tbody>');
    table.insertAdjacentHTML('beforeend', '<tbody class="in-progress"></tbody>');

    for (const PR of PRs) {
        table.querySelector('tbody.' + status(PR)).appendChild(PR);
    }
}


/**
 * @param {Element} item
 *
 * @returns {string}
 */
function status(item) {
    /** If changes have been requested => in progress */
    const rejection = item.querySelector('[aria-label="avatar group"] [alt*=" requested changes "]');
    if (rejection) {
        return 'in-progress';
    }

    /** If there are open tasks => in progress */
    const activities = item.querySelectorAll('td:nth-child(2)')
    for (const activity of activities) {
        if (activity.innerHTML.includes(' open tasks')) {
            return 'in-progress';
        }
    }

    /** If there are open tasks => in progress */
    const avatars = item.querySelectorAll('[aria-label="avatar group"] img[alt]');
    const approvals = item.querySelectorAll('[aria-label="avatar group"] img[alt*=" approved "]');
    if (approvals.length >= 1 && avatars.length === approvals.length) {
        return 'approved';
    }

    if (approvals.length >= 2) {
        return 'approved';
    }

    return 'needs-review';
}

GM_addStyle(`
    .pseudo-header {
        border-bottom: 0px;
    }
    .pseudo-header th {
        padding-top: 32px;
    }

    tbody.needs-review {
        border-bottom: 0;
        border-top: 2px solid #FFAB00;
    }
    tbody.approved {
        border-bottom: 0;
        border-top: 2px solid #00875A;
    }
    tbody.in-progress {
        border-bottom: 0;
        border-top: 2px solid #DE350B;
    }
`);