'Generate a release changelog and write it to a file.
The changelog contains a section per microservice that describes the
changes made since the last Spinnaker release. It also contains the
version information as well.'

def generate_changelog(self):

    changelog = []
    for comp in sorted(self.__changelog_start_hashes.keys()):
        if (comp == 'spinnaker'):
            continue
        hash = self.__changelog_start_hashes.get(comp, None)
        version = self.__version_from_tag(comp)
        print 'Generating    changelog    for    {comp}...'.format(comp=comp)
        component_url = run_quick('git    -C    {path}    config    --get    remote.origin.url'.format(path=comp)).stdout.strip()
        if component_url.endswith('.git'):
            component_url = component_url.replace('.git', '')
        result = run_quick('cd    {comp};    clog    -r    {url}    -f    {hash}    --setversion    {version};    cd    ..'.format(comp=comp, url=component_url, hash=hash, version=version))
        if (result.returncode != 0):
            print 'Changelog    generation    failed    for    {0}    with    \n{1}\n    exiting...'.format(comp, result.stdout)
            exit(result.returncode)
        if ((BUG_FIXES in result.stdout) or (FEATURES in result.stdout) or (BREAKING_CHANGES in result.stdout) or (CONFIG_CHANGES in result.stdout)):
            comp_cap = (comp[0].upper() + comp[1:])
            changelog.append('#    {0}\n{1}'.format(comp_cap, result.stdout))
    print 'Writing    changelog...'
    changelog_file = (self.__changelog_output or '{0}-changelog.md'.format(self.__toplevel_version))
    with open(changelog_file, 'w') as clog:
        clog.write('\n'.join(changelog))
