def test_retrive_builds_for_project():

    # Given
    project = BitriseProject("android-flagship", "f0a251acc3a5f5b7")
    endpoint = f"{BITRISE_API_URL}/apps/f0a251acc3a5f5b7/builds"
    payload = fixture("bitrise_builds")
    responses.add(responses.GET, endpoint, json=payload, status=200)

    bitrise = Bitrise("fake-api-token")

    # When
    builds = bitrise.builds_for_project(project)

    # Then
    linux = BuildMachine("linux.elite", MachineSize.g1medium, BuildStack.linux)
    workflow = BitriseWorkflow("pull-request")
    status = ExecutionStatus.error

    expected = [
        BitriseBuild(project, linux, workflow, BuildMinutes(0, 2, 2), status,
                     "ufs/some-tests"),
        BitriseBuild(project, linux, workflow, BuildMinutes(0, 2, 2), status,
                     "ufs/more-tests"),
    ]

    assert builds == expected
def test_multiplebuilds_perproject_breakdown(cruncher):

    # Given
    builds = [
        BitriseBuild(ANDROID, LINUX_MEDIUM, PR_WORKFLOW,
                     BuildMinutes(0, 30, 30), SUCCESS),
        BitriseBuild(ANDROID, LINUX_MEDIUM, PARALLEL_WORKFLOW,
                     BuildMinutes(0, 20, 20), SUCCESS),
        BitriseBuild(ANDROID, LINUX_MEDIUM, QA_RELEASE,
                     BuildMinutes(0, 40, 40), SUCCESS),
        BitriseBuild(ANDROID, LINUX_MEDIUM, LIVE_RELEASE,
                     BuildMinutes(0, 40, 40), SUCCESS),
    ]

    # When
    breakdown = cruncher.breakdown_per_project(builds)

    # Then
    numbers = CrunchedNumbers(
        count=4,
        queued=0,
        building=130,
        total=130,
        successes=4,
        failures=0,
        abortions=0,
        credits=260,
    )

    expected = {ANDROID: numbers}
    assert breakdown.details == expected
def test_builds_analysed_with_for_target_branch():

    # Given
    project = BitriseProject("android-flagship", "a2b473cfa869c525")
    machine = BuildMachine("linux.elite-xl", MachineSize.g1large,
                           BuildStack.linux)
    workflow = BitriseWorkflow("checks")
    status = ExecutionStatus.success
    target_branch = "main"

    builds = [
        BitriseBuild(project, machine, workflow, BuildMinutes(0, 0, 50),
                     status, "main"),
        BitriseBuild(project, machine, workflow, BuildMinutes(0, 0, 52),
                     status, "main"),
        BitriseBuild(project, machine, workflow, BuildMinutes(0, 0, 51),
                     status, "new-feature"),
    ]

    bitrise = FakeBitrise([project], builds)
    analyser = BuildsAnalyser(bitrise, MetricsCruncher(), target_branch)

    # When
    breakdowns, analysed_builds = analyser.analyse(project)

    # Then
    assert len(breakdowns) == 3
    assert analysed_builds == 2
def test_multiplebuilds_permachine_breakdown(cruncher):

    # Given
    minutes = BuildMinutes(queued=0, building=10, total=10)
    builds = [
        BitriseBuild(ANDROID, LINUX_MEDIUM, PR_WORKFLOW, minutes, SUCCESS),
        BitriseBuild(ANDROID, LINUX_MEDIUM, PARALLEL_WORKFLOW, minutes,
                     SUCCESS),
        BitriseBuild(ANDROID, LINUX_LARGE, PR_WORKFLOW, minutes, SUCCESS),
        BitriseBuild(ANDROID, LINUX_LARGE, PARALLEL_WORKFLOW, minutes,
                     SUCCESS),
    ]

    # When
    breakdown = cruncher.breakdown_per_machine(builds)

    # Then
    expected = {
        LINUX_MEDIUM:
        CrunchedNumbers(
            count=2,
            queued=0,
            building=20,
            total=20,
            successes=2,
            failures=0,
            abortions=0,
            credits=40,
        ),
        LINUX_LARGE:
        CrunchedNumbers(
            count=2,
            queued=0,
            building=20,
            total=20,
            successes=2,
            failures=0,
            abortions=0,
            credits=80,
        ),
    }

    assert breakdown.details == expected
def convert_bitrise_build():
    converter = RawDataConverter()
    android = BitriseProject("my-project", "c92318adbbe2755f")

    build = {
        "triggered_at": "2021-03-01T09:05:22Z",
        "started_on_worker_at": "2021-03-01T09:06:33Z",
        "finished_at": "2021-03-01T09:35:45Z",
        "machine_type_id": "elite-xl",
        "stack_identifier": "android-docker-linux",
        "triggered_workflow": "espresso-tests",
    }

    converted = converter.convert_build(build, android)

    expected = BitriseBuild(
        project=android,
        machine=BuildMachine(MachineSize.medium, BuildStack.osx),
        workflow=BitriseWorkflow("espresso-tests"),
        minutes=BuildMinutes(1, 35, 36),
    )

    assert converted == expected
def test_onebuild_perproject_breakdown(cruncher):

    # Given
    minutes = BuildMinutes(queued=0, building=30, total=30)
    builds = [
        BitriseBuild(ANDROID, LINUX_LARGE, PR_WORKFLOW, minutes, SUCCESS)
    ]

    # When
    breakdown = cruncher.breakdown_per_project(builds)

    numbers = CrunchedNumbers(count=1,
                              queued=0,
                              building=30,
                              total=30,
                              successes=1,
                              failures=0,
                              abortions=0,
                              credits=120)

    expected = {ANDROID: numbers}

    assert breakdown.details == expected
def test_onebuild_perworkfow_breakdown(cruncher):

    # Given
    minutes = BuildMinutes(queued=2, building=20, total=22)
    builds = [
        BitriseBuild(ANDROID, LINUX_LARGE, PR_WORKFLOW, minutes, SUCCESS)
    ]

    # When
    breakdown = cruncher.breakdown_per_workflow(builds)

    # Then
    numbers = CrunchedNumbers(count=1,
                              queued=2,
                              building=20,
                              total=22,
                              successes=1,
                              failures=0,
                              abortions=0,
                              credits=88)

    expected = {PR_WORKFLOW: numbers}
    assert breakdown.details == expected
def test_onebuild_permachine_breakdown(cruncher):

    # Given
    minutes = BuildMinutes(queued=0, building=20, total=20)
    builds = [
        BitriseBuild(ANDROID, LINUX_MEDIUM, PR_WORKFLOW, minutes, SUCCESS)
    ]

    # When
    breakdown = cruncher.breakdown_per_machine(builds)

    # Then
    numbers = CrunchedNumbers(count=1,
                              queued=0,
                              building=20,
                              total=20,
                              successes=1,
                              failures=0,
                              abortions=0,
                              credits=40)

    expected = {LINUX_MEDIUM: numbers}
    assert breakdown.details == expected