In AppleScript or JavaScript how can you click menu item?
Since the OP is tagged with both AppleScript and JavaScript, and are two entirely separate languages, and it was not explicitly and specifically stated that the solution had to be only in JavaScript, here it some code that should work. I say "should work", because it does work on the applications tested on, but I do not have Sourcetree installed to test explicitly and specifically with it.

AppleScript code:

tell application "Sourcetree" to activate
delay 1
tell application "System Events"
    click menu item ¬
        "Dark" of menu 1 of menu item ¬
        "Theme" of menu 1 of menu bar item ¬
        "View" of menu bar 1 of application process "Sourcetree"
end tell


Note: The value of the delay command may need to be adjusted for your system.



JavaScript code:

menuItemClick("Sourcetree", ['View', 'Theme', 'Dark'])

function menuItemClick(strAppName, lstMenuPath) {
    var oApp = Application(strAppName),
        lngChain = lstMenuPath.length,
        blnResult = false;

    if (lngChain &gt; 1) {

        var appSE = Application("System Events"),
            lstApps = appSE.processes.where({
                name: strAppName
            }),
            procApp = lstApps.length ? lstApps[0] : null;

        if (procApp) {
            oApp.activate();
            var strMenu = lstMenuPath[0],
                fnMenu = procApp.menuBars[0].menus.byName(strMenu),
                lngLast = lngChain - 1;

            for (var i = 1; i &lt; lngLast; i++) {
                strMenu = lstMenuPath[i];
                fnMenu = fnMenu.menuItems[strMenu].menus[strMenu];
            }


            fnMenu.menuItems[
                lstMenuPath[lngLast]
            ].click();
            blnResult = true;
        }
    }
    return blnResult;
}


Note: The JavaScript code comes from jxaClickAppSubMenuItem.applescript by bumaociyuan and was forked from RobTrew/jxaClickAppSubMenuItem.applescript.