Resolve audit log user IDs

This script calls the Jira user API, resolves user IDs in the Jira audit log and replaces them with the display names.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Resolve audit log user IDs
// @namespace    http://schuppentier.org/
// @version      2024-02-19
// @description  This script calls the Jira user API, resolves user IDs in the Jira audit log and replaces them with the display names.
// @author       You
// @match        https://*.atlassian.net/auditing/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=atlassian.net
// @grant        none
// @require      https://bowercdn.net/c/arrive-2.4.1/minified/arrive.min.js
// @license      MIT
// ==/UserScript==

/* jshint esversion: 8 */

(function() {
    'use strict';

    const resolveUserId = async (userId) => {
        const userResponse = await fetch(`/rest/api/3/user?accountId=${userId}`);
        const userObject = await userResponse.json();
        const userDisplayName = userObject.displayName;
        return userDisplayName;
    };

    document.arrive("span.delta-from, span.delta-to", async (elem) => {
        const detailsRow = elem.closest("tr.record-row-details");
        const recordRow = detailsRow.previousElementSibling;
        const summary = recordRow.querySelector("td.summary").innerText;
        if (summary != "Project roles changed") {
            return;
        }

        const fromElem = detailsRow.querySelector("span.delta-from");
        const toElem = detailsRow.querySelector("span.delta-to");

        var fromUserIds = new Set(fromElem.innerText.split(",").map(item => item.trim()));
        var toUserIds = new Set(toElem.innerText.split(",").map(item => item.trim()));

        const duplicateUserIds = fromUserIds.intersection(toUserIds);

        fromUserIds = fromUserIds.difference(duplicateUserIds);
        toUserIds = toUserIds.difference(duplicateUserIds);

        const fromUserNames = await Promise.all(fromUserIds.map(resolveUserId));
        const toUserNames = await Promise.all(toUserIds.map(resolveUserId));

        fromElem.innerText = fromUserNames.join(", ");
        toElem.innerText = toUserNames.join(", ");
    });
})();