There was a long thread on Mudcat about the origins of the song.
thread.cfm?threadid=16979#161608
The definitive answer is dated 21-01-00 and is by Malcolm Douglas - who else?
Briefly it dates back to Blackwoods Magazine (Edinburgh) and 1821.
The version most people sing now is probably based on Anne Brigg's version.