def get_closed_issues_since_last_tag(cli_args: dict) -> list:
    last_tagged_release_date = get_last_tagged_release_date(cli_args)

    closed_issues = get_closed_issues_for_project(cli_args)

    closed_issues_since_tag = []
    for issue in closed_issues:
        logger.info(issue)
        if dateutil.parser.parse(issue["closed_at"]) > dateutil.parser.parse(
            last_tagged_release_date
        ):
            closed_issues_since_tag.append(
                {"closed_at": issue["closed_at"], "title": issue["title"]}
            )

    return closed_issues_since_tag
Beispiel #2
0
    def generate_changelog(self, cli_args: dict) -> str:
        # Get any commits since that date
        new_commits = get_commits_until_latest_bump(cli_args)

        # Get the current date so that we can add it to the CHANGELOG.md document
        date = datetime.datetime.now()
        current_date = date.strftime('%Y/%m/%d')

        allowed_projs = self.include_projs
        allowed_projs.append(cli_args['sub_project'])
        logger.debug('allow_projs')
        logger.debug(allowed_projs)

        commits_type_dict = {type: [] for type in self.type_map}
        commits_type_dict[''] = []
        for commit in new_commits:
            title = commit['title']
            match_obj = re.match(r'^(.+)(\((.+)\))?:', title)
            change_type = ''
            if match_obj:
                change_type = match_obj.group(1)
                proj = match_obj.group(2)
                if proj and proj not in allowed_projs:
                    logger.info(title)
                    continue
            logger.info(title)
            if change_type in self.type_map:
                commits_type_dict[change_type].append(commit)
            else:
                commits_type_dict[''].append(commit)

        version = self.get_version(cli_args)
        new_version = self.get_next_version(version, commits_type_dict,
                                            cli_args)
        if version == new_version:
            logger.info('No changes')
            return

        # Determine whether a CHANGELOG.md file already exists
        if not os.path.isfile(self.file_path):
            open(self.file_path, 'w').close()
        with open(self.file_path, 'r') as original_changelog:
            original_changelog_data = original_changelog.readlines()
            if len(original_changelog_data) > 2:
                original_changelog_data = original_changelog_data[2:]
            with open(self.file_path, 'w') as modified_changelog:
                modified_changelog.write('# CHANGELOG\n\n')
                modified_changelog.write(
                    f'## v{new_version} - {current_date}\n')
                for type in self.type_order:
                    commits = commits_type_dict[type]
                    if not commits:
                        continue
                    modified_changelog.write(f'\n### {self.type_map[type]} \n')
                    for commit in commits:
                        logger.debug('commit ')
                        logger.debug(commit)
                        lines = commit['message'].strip().split('\n')
                        modified_changelog.write(
                            f"  * {commit['committed_date'][:10]} - {lines[0]}"
                        )

                        if not re.match(r'^.+\(\![0-9]+\)$', lines[0]):
                            modified_changelog.write(
                                f" ({commit['short_id']})")

                        modified_changelog.write('\n')
                        if len(lines) > 1:
                            modified_changelog.write('\n'.join(
                                '    ' + line for line in lines[1:] if line))
                            modified_changelog.write('\n')

                modified_changelog.write(f'\n')
                modified_changelog.write(''.join(original_changelog_data))
        return f'{self.file_path} updated successfully'
def generate_changelog(cli_args: dict) -> str:
    # Get the date of the last commit
    last_commit = get_last_commit_date(cli_args)

    closed_issues_since_last_tag = get_closed_issues_since_last_tag(cli_args)

    # Get any commits since that date
    new_commits = get_commits_since_date(last_commit, cli_args)

    # Get the current date so that we can add it to the CHANGELOG.md document
    date = datetime.datetime.now()
    current_date = date.strftime("%Y-%m-%d")

    # Determine whether a CHANGELOG.md file already exists
    if os.path.isfile("CHANGELOG.md"):
        if cli_args["changelog"] == "Y":
            with open("CHANGELOG.md", "r") as original_changelog:
                original_changelog_data = original_changelog.read()
                with open("CHANGELOG.md", "w") as modified_changelog:
                    modified_changelog.write(
                        f"## v{cli_args['version']} ({current_date})\n"
                    )
                    [
                        modified_changelog.write(
                            f"* {commit['committed_date'][:10]} - {y} \n"
                        )
                        for commit in new_commits
                        for y in commit["message"].split("\n")
                    ]

                    if closed_issues_since_last_tag:
                        modified_changelog.write(f"\n### Closed Issues\n")
                        [
                            modified_changelog.write(
                                f"* {closed_issue['title']}"
                            )
                            for closed_issue in closed_issues_since_last_tag
                        ]
                    modified_changelog.write(f"\n")
                    modified_changelog.write(original_changelog_data)
                    return "CHANGELOG.md updated successfully"
        else:
            logger.info("Existing CHANGELOG.md found but not specified")
            logger.info("Writing CHANGELOG_generated.md as a result...")
            with open("CHANGELOG_generated.md", "w") as changelog:
                changelog.write(f"## v{cli_args['version']} ({current_date})\n")
                [
                    changelog.write(
                        f"* {commit['committed_date'][:10]} - {y} \n"
                    )
                    for commit in new_commits
                    for y in commit["message"].split("\n")
                ]

                if closed_issues_since_last_tag:
                    changelog.write(f"\n### Closed Issues\n")
                    [
                        changelog.write(f"* {closed_issue['title']}")
                        for closed_issue in closed_issues_since_last_tag
                    ]
            return "CHANGELOG_generated.md written successfully"
    else:
        logger.info("No CHANGELOG.md found and no CHANGELOG.md specified...")
        logger.info("Writing CHANGELOG.md as a result...")
        with open("CHANGELOG.md", "w") as changelog:
            changelog.write(f"## v{cli_args['version']} ({current_date})\n")
            [
                changelog.write(f"* {commit['committed_date'][:10]} - {y} \n")
                for commit in new_commits
                for y in commit["message"].split("\n")
            ]
            if closed_issues_since_last_tag:
                changelog.write(f"\n### Closed Issues\n")
                [
                    changelog.write(f"* {closed_issue['title']}")
                    for closed_issue in closed_issues_since_last_tag
                ]
        return "New CHANGELOG.md file written successfully"
Beispiel #4
0
    def generate_changelog(self, cli_args: dict) -> str:
        # Get the date of the last commit
        last_commit = get_last_commit_date(cli_args)

        # Get any commits since that date
        new_commits = get_commits_since_date(last_commit, cli_args)

        # Get the current date so that we can add it to the CHANGELOG.md document
        date = datetime.datetime.now()
        current_date = date.strftime("%Y-%m-%d")

        allowed_projs = include_projs
        allowed_projs.append(cli_args["sub_project"])
        logger.debug("allow_projs")
        logger.debug(allowed_projs)

        commits_type_dict = {type: [] for type in type_map}
        commits_type_dict[""] = []
        for commit in new_commits:
            title = commit["title"]
            match_obj = re.match(r'^(.+)\((.+)\):', title)
            change_type = match_obj.group(1)
            proj = match_obj.group(2)
            if proj not in allowed_projs:
                logger.info(title)
                continue
            logger.info(title)
            if change_type in type_map:
                commits_type_dict[change_type].append(commit)
            else:
                commits_type_dict[""].append(commit)

        # Determine whether a CHANGELOG.md file already exists
        file_path = f"{cli_args['sub_project']}/CHANGELOG.md"
        if not os.path.isfile(file_path):
            open(file_path, 'a').close()
        with open(file_path, "r") as original_changelog:
            original_changelog_data = original_changelog.read()
            with open(file_path, "w") as modified_changelog:
                modified_changelog.write(
                    f"## v{cli_args['version']} ({current_date})\n")
                for type in type_order:
                    commits = commits_type_dict[type]
                    if not commits:
                        continue
                    modified_changelog.write(f"\n### {type_map[type]} \n")
                    for commit in commits:
                        modified_changelog.write("\n")
                        logger.debug("commit ")
                        logger.debug(commit)
                        lines = commit["message"].split("\n")
                        modified_changelog.write(
                            f"  * {commit['committed_date'][:10]} - {lines[0]} \n"
                        )
                        modified_changelog.write("\n".join(
                            "    " + line for line in lines[1:] if line))
                        modified_changelog.write("\n")

                modified_changelog.write(f"\n")
                modified_changelog.write(original_changelog_data)
                return f"{file_path} updated successfully"