/// <summary>
        /// Cancels and restarts a build
        /// </summary>
        /// <param name="buildNumber">The build number</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
        /// <returns>A <see cref="Task"/> representing the running operation</returns>
        async Task RestartBuild(string buildNumber, CancellationToken cancellationToken)
        {
            logger.LogDebug("Restarting build #{0}", buildNumber);
            var baseUrl = String.Join('/', BaseBuildUrl, buildNumber);

            Task DoBuildPost(string method) => requestManager.RunRequest(new Uri(String.Join('/', baseUrl, method)), String.Empty, GetRequestHeaders(), RequestMethod.POST, cancellationToken);

            try
            {
                //first ensure it's over
                await DoBuildPost("cancel").ConfigureAwait(false);
            }
            catch (WebException e)
            {
                //409 is what happens if the build isn't already running
                if (e.Status != WebExceptionStatus.ProtocolError || !(e.Response is HttpWebResponse response) || response.StatusCode != HttpStatusCode.Conflict)
                {
                    throw;
                }
            }
            //then restart it
            await DoBuildPost("restart").ConfigureAwait(false);
        }
Beispiel #2
0
        /// <summary>
        /// Cancels and restarts a build
        /// </summary>
        /// <param name="buildNumber">The build number</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
        /// <returns>A <see cref="Task"/> representing the running operation</returns>
        async Task RestartBuild(string buildNumber, CancellationToken cancellationToken)
        {
            logger.LogDebug("Restarting build #{0}", buildNumber);
            const string baseBuildURL = "https://api.travis-ci.org/build";
            var          baseUrl      = String.Join('/', baseBuildURL, buildNumber);

            Task DoBuildPost(string method)
            {
                return(requestManager.RunRequest(new Uri(String.Join('/', baseUrl, method)), String.Empty, GetRequestHeaders(), RequestMethod.POST, cancellationToken));
            }

            //first ensure it's over
            await DoBuildPost("cancel").ConfigureAwait(false);

            //then restart it
            await DoBuildPost("restart").ConfigureAwait(false);
        }