-
Notifications
You must be signed in to change notification settings - Fork 23
fix: include every commit in build information #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
256 changes: 256 additions & 0 deletions
256
octopus-agent/src/main/java/octopus/teamcity/agent/CommitHistoryFetcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| /* | ||
| * Copyright 2000-2012 Octopus Deploy Pty. Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package octopus.teamcity.agent; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URL; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.Base64; | ||
| import java.util.List; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import jetbrains.buildServer.agent.BuildProgressLogger; | ||
| import octopus.teamcity.common.Commit; | ||
| import org.apache.commons.io.IOUtils; | ||
| import org.jetbrains.teamcity.rest.Build; | ||
| import org.jetbrains.teamcity.rest.Change; | ||
|
|
||
| public class CommitHistoryFetcher { | ||
|
|
||
| static final int PAGE_SIZE = 1000; | ||
| static final int MAX_COMMITS = 10000; | ||
| static final int TEAMCITY_DEFAULT_PAGE_SIZE = 100; | ||
| static final int ATTEMPTS_PER_PAGE = 3; | ||
| static final long RETRY_DELAY_MILLIS = 1000L; | ||
|
|
||
| private static final Gson GSON = new GsonBuilder().create(); | ||
|
|
||
| interface ChangePageRequester { | ||
| String get(String url) throws IOException; | ||
| } | ||
|
|
||
| interface RetryDelay { | ||
| void pause(int failedAttempts) throws InterruptedException; | ||
| } | ||
|
|
||
| private final String serverUrl; | ||
| private final BuildProgressLogger logger; | ||
| private final ChangePageRequester requester; | ||
| private final RetryDelay retryDelay; | ||
|
|
||
| public CommitHistoryFetcher( | ||
| final String serverUrl, | ||
| final String accessUser, | ||
| final String accessCode, | ||
| final BuildProgressLogger logger) { | ||
| this( | ||
| serverUrl, | ||
| logger, | ||
| new BasicAuthRequester(accessUser, accessCode), | ||
| failedAttempts -> Thread.sleep(failedAttempts * RETRY_DELAY_MILLIS)); | ||
| } | ||
|
|
||
| CommitHistoryFetcher( | ||
| final String serverUrl, | ||
| final BuildProgressLogger logger, | ||
| final ChangePageRequester requester, | ||
| final RetryDelay retryDelay) { | ||
| this.serverUrl = | ||
| serverUrl.endsWith("/") ? serverUrl.substring(0, serverUrl.length() - 1) : serverUrl; | ||
| this.logger = logger; | ||
| this.requester = requester; | ||
| this.retryDelay = retryDelay; | ||
| } | ||
|
|
||
| public CommitHistory fetch(final Build build, final long buildId) { | ||
| final List<Commit> commits = new ArrayList<>(); | ||
| try { | ||
| return fetchAllPages(buildId, commits); | ||
| } catch (Exception ex) { | ||
| if (!commits.isEmpty()) { | ||
| logger.warning( | ||
| "Unable to read the rest of the change list from the TeamCity REST API (" | ||
| + ex | ||
| + "). Keeping the " | ||
| + commits.size() | ||
| + " commits already read."); | ||
| return new CommitHistory(commits, truncationWarning(commits.size())); | ||
| } | ||
| logger.warning( | ||
| "Unable to read the change list from the TeamCity REST API (" | ||
| + ex | ||
| + "). Falling back to the first page of changes only."); | ||
| final List<Commit> fallback = toCommits(build.fetchChanges()); | ||
| return new CommitHistory(fallback, fallbackWarning(fallback.size())); | ||
| } | ||
| } | ||
|
|
||
| // The client library requests changes without a count, so TeamCity caps it at its default page. | ||
| private static String fallbackWarning(final int commitCount) { | ||
| if (commitCount < TEAMCITY_DEFAULT_PAGE_SIZE) { | ||
| return null; | ||
| } | ||
| return truncationWarning(commitCount); | ||
| } | ||
|
|
||
| private static String truncationWarning(final int commitCount) { | ||
| return "The full change list could not be read from TeamCity, so only the first " | ||
| + commitCount | ||
| + " commits of this build were included in the build information."; | ||
| } | ||
|
|
||
| private CommitHistory fetchAllPages(final long buildId, final List<Commit> commits) | ||
| throws IOException { | ||
| int start = 0; | ||
| while (true) { | ||
| final List<Commit> page = parsePage(getWithRetries(changesUrl(buildId, start))); | ||
| commits.addAll(page); | ||
|
|
||
| if (page.size() < PAGE_SIZE) { | ||
| return new CommitHistory(commits, null); | ||
| } | ||
| // Only a page past the cap proves commits were left out; exactly MAX_COMMITS is complete. | ||
| if (commits.size() > MAX_COMMITS) { | ||
| return new CommitHistory( | ||
| new ArrayList<>(commits.subList(0, MAX_COMMITS)), | ||
| "Only the first " | ||
| + MAX_COMMITS | ||
| + " commits of this build were included in the build information."); | ||
| } | ||
| start += PAGE_SIZE; | ||
| } | ||
| } | ||
|
|
||
| // A page can fail on a transient 500 or a read timeout, so give each one a few attempts before | ||
| // settling for whatever has been read so far. | ||
| private String getWithRetries(final String url) throws IOException { | ||
| IOException lastFailure = null; | ||
| for (int attempt = 1; attempt <= ATTEMPTS_PER_PAGE; attempt++) { | ||
| try { | ||
| return requester.get(url); | ||
| } catch (IOException ex) { | ||
| lastFailure = ex; | ||
| if (attempt < ATTEMPTS_PER_PAGE) { | ||
| logger.warning( | ||
| "Reading a page of changes from the TeamCity REST API failed (" | ||
| + ex | ||
| + "). Retrying, attempt " | ||
| + (attempt + 1) | ||
| + " of " | ||
| + ATTEMPTS_PER_PAGE | ||
| + "."); | ||
| pauseBeforeRetry(attempt); | ||
| } | ||
| } | ||
| } | ||
| throw lastFailure; | ||
| } | ||
|
|
||
| private void pauseBeforeRetry(final int failedAttempts) throws IOException { | ||
| try { | ||
| retryDelay.pause(failedAttempts); | ||
| } catch (InterruptedException ex) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new IOException("Interrupted while waiting to retry the change list request", ex); | ||
| } | ||
| } | ||
|
|
||
| private String changesUrl(final long buildId, final int start) { | ||
| // Without an explicit count TeamCity returns only its default page of 100 changes. | ||
| return serverUrl | ||
| + "/httpAuth/app/rest/changes?locator=build:(id:" | ||
| + buildId | ||
| + "),count:" | ||
| + PAGE_SIZE | ||
| + ",start:" | ||
| + start | ||
| + "&fields=change(version,comment)"; | ||
| } | ||
|
|
||
| private static List<Commit> parsePage(final String body) { | ||
| final ChangesPage page = GSON.fromJson(body, ChangesPage.class); | ||
| final List<Commit> commits = new ArrayList<>(); | ||
| if (page == null || page.change == null) { | ||
| return commits; | ||
| } | ||
| for (final ChangeJson change : page.change) { | ||
| commits.add(toCommit(change.version, change.comment)); | ||
| } | ||
| return commits; | ||
| } | ||
|
|
||
| private static List<Commit> toCommits(final List<Change> changes) { | ||
| final List<Commit> commits = new ArrayList<>(); | ||
| for (final Change change : changes) { | ||
| commits.add(toCommit(change.getVersion(), change.getComment())); | ||
| } | ||
| return commits; | ||
| } | ||
|
|
||
| private static Commit toCommit(final String version, final String comment) { | ||
| final Commit commit = new Commit(); | ||
| commit.Id = version; | ||
| commit.Comment = comment; | ||
| return commit; | ||
| } | ||
|
|
||
| private static final class ChangesPage { | ||
| List<ChangeJson> change; | ||
| } | ||
|
|
||
| private static final class ChangeJson { | ||
| String version; | ||
| String comment; | ||
| } | ||
|
|
||
| private static final class BasicAuthRequester implements ChangePageRequester { | ||
|
|
||
| private static final int TIMEOUT_MILLIS = 30000; | ||
|
|
||
| private final String authorization; | ||
|
|
||
| BasicAuthRequester(final String accessUser, final String accessCode) { | ||
| final byte[] credentials = (accessUser + ":" + accessCode).getBytes(StandardCharsets.UTF_8); | ||
| this.authorization = "Basic " + Base64.getEncoder().encodeToString(credentials); | ||
| } | ||
|
|
||
| @Override | ||
| public String get(final String url) throws IOException { | ||
| final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); | ||
| try { | ||
| connection.setRequestMethod("GET"); | ||
| connection.setConnectTimeout(TIMEOUT_MILLIS); | ||
| connection.setReadTimeout(TIMEOUT_MILLIS); | ||
| connection.setRequestProperty("Authorization", authorization); | ||
| connection.setRequestProperty("Accept", "application/json"); | ||
|
|
||
| final int statusCode = connection.getResponseCode(); | ||
| if (statusCode != HttpURLConnection.HTTP_OK) { | ||
| throw new IOException("TeamCity responded with HTTP " + statusCode + " for " + url); | ||
| } | ||
| try (InputStream responseStream = connection.getInputStream()) { | ||
| return IOUtils.toString(responseStream, StandardCharsets.UTF_8); | ||
| } | ||
| } finally { | ||
| connection.disconnect(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 3 additions & 12 deletions
15
octopus-agent/src/main/java/octopus/teamcity/agent/OctopusBuildInformationBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commits already fetched are discarded when a later page fails.
If pages 1–2 succeed (2000 commits) and page 3 throws — transient 500 or read timeout, and there's no retry — this catch block throws away all 2000 and re-fetches via
build.fetchChanges(), yielding 100. Needs >1000 commits plus network flakiness so it's uncommon, but flakiness is a matter of time, and the degradation is far larger than it needs to be.Returning the partial list with a truncation warning would be strictly better. It also matters for the warning text: "only the first 100 commits" is accurate here only by coincidence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in dad606f — the accumulator now lives in
fetch(), so a failing page keeps whatever pages already succeeded and returns them with a truncation warning; thebuild.fetchChanges()fallback only runs when nothing was read at all. Your point about the wording held too: the warning now names the number of commits actually included instead of hardcoding TeamCity's default page size. New testkeepsThePagesAlreadyReadWhenALaterPageFailsfails page 3 and asserts the first 2000 survive. Still no retry — happy to add one if you think it's worth it, but keeping the partial result seemed like the bigger win.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will keep expanding this with Claude
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the retry in db65ddc, so this thread is fully covered now. Each page gets
ATTEMPTS_PER_PAGE= 3 attempts onIOException(the requester turns a non-200 into one, so 500s and read timeouts both retry) with a linear backoff of 1s then 2s. Only once the attempts are exhausted does it settle for the pages already read. The delay is injected as aRetryDelay, so the tests record the pauses rather than sleep —retriesAPageThatFailsAndCarriesOnWhenTheRetrySucceedsandgivesUpOnAPageAfterTheAttemptLimit. A malformed-JSON response is deliberately not retried; it falls straight through to the existing handling.