private BuildStatusResults getGitResults()

in plugin/src/com/microsoft/alm/plugin/operations/BuildStatusLookupOperation.java [170:239]


    private BuildStatusResults getGitResults(final ServerContext context) {
        final List<BuildStatusRecord> buildStatusRecords = new ArrayList<BuildStatusRecord>(2);
        Build latestBuildForRepository = null;
        Build matchingBuild = null;
        BuildStatusResults results;

        if (context.getGitRepository() != null) {
            // Using the build REST client we will get the last 100 builds for this team project.
            // We will go through those builds and try to find one that matches our repo and branch.
            // If we can't find a perfect match, we will keep the first one that matches our repo.
            // TODO: The latest REST API allows you to filter the builds based on repo and branch, but the Java SDK
            // TODO: is not up to date with that version yet. We should change this code to use that method as soon
            // TODO: as we can.
            final BuildHttpClient buildClient = context.getBuildHttpClient();
            final List<Build> builds = buildClient.getBuilds(context.getTeamProjectReference().getId(), null,
                    null, null, null, null, null, null, BuildStatus.COMPLETED,
                    null, //TODO: EnumSet.of(BuildResult.FAILED, BuildResult.PARTIALLY_SUCCEEDED, BuildResult.SUCCEEDED),
                    null, null, null, 100, null, null, null, BuildQueryOrder.FINISH_TIME_DESCENDING);
            if (builds.size() > 0) {
                for (final Build b : builds) {
                    if (b.getResult() == BuildResult.CANCELED) {
                        // Ignore canceled builds (it would be better to not query for them above, but that isn't working in the SDK)
                        continue;
                    }

                    // Get the repo and branch for the build and compare them to ours
                    final BuildRepository repo = b.getRepository();
                    if (repo != null && StringUtils.equalsIgnoreCase(context.getGitRepository().getId().toString(), repo.getId())) {
                        // TODO: Get the constant refs/heads/master from someplace common or query for the default branch from the server
                        // Branch names are case sensitive
                        if (StringUtils.equals(b.getSourceBranch(), "refs/heads/master")) {
                            if (latestBuildForRepository == null) {
                                // Found the master branch for the repo, so save that off
                                logger.info("Latest build found for repo for the master branch.");
                                latestBuildForRepository = b;
                            }
                        } else if (StringUtils.equals(b.getSourceBranch(), repositoryContext.getBranch())) {
                            if (matchingBuild == null) {
                                // The repo and branch match the build exactly, so save that off
                                logger.info("Matching build found for repo and branch.");
                                matchingBuild = b;
                            }
                        }

                        if (latestBuildForRepository != null && matchingBuild != null) {
                            // We found both builds
                            break;
                        }
                    }
                }

                // Create the results
                if (latestBuildForRepository != null) {
                    // Add the repository build to the status records list first
                    buildStatusRecords.add(new BuildStatusRecord(latestBuildForRepository));
                }
                if (matchingBuild != null) {
                    // Add the matching build to the status records list last
                    buildStatusRecords.add(new BuildStatusRecord(matchingBuild));
                }
                results = new BuildStatusResults(context, buildStatusRecords);
            } else {
                // No builds were found for this project
                results = new BuildStatusResults(context, null);
            }
        } else {
            results = new BuildStatusResults(null, null);
        }
        return results;
    }