If you fire in melee, you get a -20 penalty and there is a chance you hit your ally.
If you opt to hose the meleeing guys with autofire instead of a carefully aimed pot shot, you actually have LESS chance to hit the wrong guy cause you get a +20 bonus. However, this seems unrealistic. Unloading without restraint in a vicious melee would suggest you risk a bigger chance of hitting your friend with at least some of the bullets.
Also what if your ally is in melee with two or more adversaries? Or two allies with one opponent? Do the chances of hitting the wrong ones change?