示例#1
0
def test_write_to_file():
    open_mock = mock_open()
    with patch('__main__.open', open_mock, create=True):
        commons.write_to_file("somefilepath",
                              "test_write_to_file",
                              open_func=open_mock)

    open_mock.assert_called_once_with("somefilepath", "a")
    file_mock = open_mock()
    file_mock.write.assert_called_once_with("test_write_to_file")
示例#2
0
def call_github_getversion(git_hub_instance, file_path=None, open_func=open):
    commons.print_msg('aggregator', 'call_github_getversion', 'begin')
    clazz = 'aggregator'
    method = 'call_github_getversion'
    commons.print_msg(clazz, method, 'begin')

    my_version = git_hub_instance.get_git_last_tag()
    if file_path:
        try:
            commons.write_to_file(file_path, my_version, mode='w', open_func=open_func)
        except Exception as e:
            commons.print_msg(clazz, method, 'Failed creating file {file}.  {error}'.format(
                file=file_path, error=e), 'ERROR')
            exit(1)
    else:
        print(my_version)

    commons.print_msg(clazz, method, 'end')
示例#3
0
def call_github_version(github_instance,
                        tracker_instance,
                        config=None,
                        file_path=None,
                        open_func=open,
                        args=None):
    clazz = 'aggregator'
    method = 'call_github_version'
    commons.printMSG(clazz, method, 'begin')
    if config is None:
        config = BuildConfig

    release_notes = None

    if config.version_strategy == 'manual':
        try:
            if 'version' not in args:
                commons.printMSG(
                    clazz, method,
                    'Version number required for release but not passed in.',
                    'ERROR')
                exit(1)
        except:
            commons.printMSG(
                clazz, method,
                'Version number required for release but not passed in.',
                'ERROR')
            exit(1)

        # find the highest existing tag that matches the base release that was passed in.
        base_semver_tag_array = github_instance.convert_semver_string_to_semver_tag_array(
            args.version.strip())
        highest_semver_tag_array = github_instance.get_highest_semver_tag()
        highest_semver_release_tag_array = github_instance.get_highest_semver_release_tag(
        )

        # default the bump stategy to None.
        if config.artifact_category == 'snapshot':
            if highest_semver_release_tag_array == base_semver_tag_array:
                commons.printMSG(
                    clazz, method,
                    "Version number {} already has release build associated.".
                    format(highest_semver_release_tag_array), 'ERROR')
                exit(1)

            highest_semver_tag_array_from_base = github_instance.get_highest_semver_array_snapshot_tag_from_base(
                base_semver_tag_array)
            # - Fetch all commit history.  Either from the last valid tag that includes base or the last tag if the base
            # doesn't exist in tags yet       if
            commits = github_instance.get_all_git_commit_history_between_provided_tags(
                highest_semver_tag_array if highest_semver_tag_array_from_base
                is None else highest_semver_tag_array_from_base)

            next_semver_tag_array = github_instance.calculate_next_semver(
                tag_type=config.artifact_category,
                bump_type=None,
                highest_version_array=base_semver_tag_array
                if highest_semver_tag_array_from_base is None else
                highest_semver_tag_array_from_base)
        else:  # release, so use the base
            # - Fetch all commit history.  Either from the last valid tag that includes base or the last tag if the base
            # doesn't exist in tags yet
            commits = github_instance.get_all_git_commit_history_between_provided_tags(
                highest_semver_release_tag_array)

            next_semver_tag_array = base_semver_tag_array

        # - Dig through commits to find story list.
        story_list = commons.extract_story_id_from_commit_messages(commits)

        # - Dig through the story list to fetch some meta data about each story.
        if tracker_instance is None:
            story_details = None
        else:
            story_details = tracker_instance.get_details_for_all_stories(
                story_list)

        # - Tag the version
        # - Update the release notes on the new version number with the stories meta data.
        release_notes = github_instance.format_github_specific_release_notes_from_tracker_story_details(
            story_details)

        my_version = github_instance.convert_semver_tag_array_to_semver_string(
            next_semver_tag_array)
        if file_path:
            commons.write_to_file(file_path, my_version, open_func=open_func)
        commons.printMSG(clazz, method, 'end')

    elif config.version_strategy == 'tracker':
        if args and 'version' in args and args.version is not None:
            commons.printMSG(
                clazz, method,
                'Version strategy set to automated in buildConfig but version flag was '
                'passed in.  Either change versionStreategy to manual or remove -v flag.',
                'ERROR')
            exit(1)

        if config.artifact_category == 'snapshot':
            # - Find the highest sem ver tag (not latest), doesn't matter if snapshot or release or beginning of time
            highest_semver_tag_array = github_instance.get_highest_semver_tag()
            highest_semver_tag_array_history = github_instance.get_highest_semver_snapshot_tag(
            )

        elif config.artifact_category == 'release':
            # - Find the last semantic version release tag or begining of time
            highest_semver_tag_array = github_instance.get_highest_semver_release_tag(
            )
            highest_semver_tag_array_history = highest_semver_tag_array
        else:
            raise Exception(
                "Invalid artifact_category provided.  Must be 'snapshot' or 'release'"
            )

        # - Fetch all commit history from that tag to now
        commits = github_instance.get_all_git_commit_history_between_provided_tags(
            highest_semver_tag_array_history)
        # - Dig through commits to find story list.
        story_list = commons.extract_story_id_from_commit_messages(commits)

        story_details = None
        if config.json_config['tracker']:
            # - Dig through the story list to fetch some meta data about each story.
            story_details = tracker_instance.get_details_for_all_stories(
                story_list)

        # default the bump strategy to None.
        if config.artifact_category == 'snapshot':
            next_semver_tag_array = github_instance.calculate_next_semver(
                tag_type=config.artifact_category,
                bump_type=None,
                highest_version_array=highest_semver_tag_array)
        else:  # release
            # New Release build
            # - Find the last semantic version release tag or beginning of time
            # - Based upon the story meta data, decided on the bump strategy.
            bump_strategy = tracker_instance.determine_semantic_version_bump(
                story_details)

            # - Increment the build number + 1
            next_semver_tag_array = github_instance.calculate_next_semver(
                tag_type=config.artifact_category,
                bump_type=bump_strategy,
                highest_version_array=highest_semver_tag_array)

        # - Tag the version
        # - Update the release notes on the new version number with the stories meta data.
        release_notes = github_instance.format_github_specific_release_notes_from_tracker_story_details(
            story_details)
        my_version = github_instance.convert_semver_tag_array_to_semver_string(
            next_semver_tag_array)

        if file_path:
            commons.write_to_file(file_path, my_version, open_func=open_func)
        commons.printMSG(clazz, method, 'end')

    if args is not None and args.release_notes_output_path is not None:
        if release_notes is None:
            commons.printMSG(clazz, method,
                             'No release notes found to save to file', 'ERROR')
            exit(1)
        try:
            args.release_notes_output_path.write(release_notes)
        except Exception as e:
            commons.printMSG(
                clazz, method, 'Failed creating file {file}.  {error}'.format(
                    file=args.release_notes_output_path, error=e), 'ERROR')
            exit(1)

    if args is None or not args.no_publish:
        github_instance.add_tag_and_release_notes_to_github(
            next_semver_tag_array, release_notes)

    print(my_version)