Github was subject to US law before MS took it over; gitlab is not MS-owned but had to do the same (not sure about bitbucket but I imagine it's similar).
Whether the #1 place to host open-source projects should be owned by a company that makes a closed-source OS is a good debate question but it's completely orthogonal to whether they have to comply with US law.
I'm not saying I agree either with the sanctions or with Microsoft running github, but I'm saying you can't blame Microsoft for the current administration's sanctions on Iran.