Gravitational force is a conservative force. Hence the work done depends on the initial and final positions.
In a complete revolution of the sun by a comet, the displacement becomes zero, so the work done by the gravitational force over every complete orbit of the comet is zero.