Although the word martyr somewhat applies, it includes a nobility not necessarily present.
An idiom describing this, in a voluntary situation, would be drawing the short straw, thereby acquiring some unpleasant or dangerous duty. An idiom for an involuntary situation would be getting thrown under the bus.
Friend: I knew someone would eventually need to refactor this code. I didn't think it would be me.
Me: You're our... uh, what's the word?