public async Task <AsyncDisposableValue <string> > CreateSubscriptionAsync(string yamlDefinition) { string output = await RunDarcAsyncWithInput(yamlDefinition, "add-subscription", "-q", "--read-stdin", "--no-trigger").ConfigureAwait(false); Match match = Regex.Match(output, "Successfully created new subscription with id '([a-f0-9-]+)'"); if (match.Success) { string subscriptionId = match.Groups[1].Value; return(AsyncDisposableValue.Create(subscriptionId, async() => { TestContext.WriteLine($"Cleaning up Test Subscription {subscriptionId}"); try { await RunDarcAsync("delete-subscriptions", "--id", subscriptionId, "--quiet").ConfigureAwait(false); } catch (MaestroTestException) { // If this throws an exception the most likely cause is that the subscription was deleted as part of the test case } })); } throw new MaestroTestException("Unable to create subscription."); }
public async Task <AsyncDisposableValue <string> > CreateTestChannelAsync(string testChannelName) { try { await RunDarcAsync("delete-channel", "--name", testChannelName).ConfigureAwait(false); } catch (MaestroTestException) { // Ignore failures from delete-channel, its just a pre-cleanup that isn't really part of the test } await RunDarcAsync("add-channel", "--name", testChannelName, "--classification", "test").ConfigureAwait(false); return(AsyncDisposableValue.Create(testChannelName, async() => { TestContext.WriteLine($"Cleaning up Test Channel {testChannelName}"); try { string doubleDelete = await RunDarcAsync("delete-channel", "--name", testChannelName).ConfigureAwait(false); } catch (MaestroTestException) { // Ignore failures from delete-channel, this delete is here to ensure that the channel is deleted // even if the test does not do an explicit delete as part of the test } })); }
public async Task <AsyncDisposableValue <string> > CreateSubscriptionAsync( string sourceChannelName, string sourceRepo, string targetRepo, string targetBranch, string updateFrequency, string sourceOrg = "dotnet", List <string> additionalOptions = null, bool sourceIsAzDo = false, bool targetIsAzDo = false, bool trigger = false) { string sourceUrl = sourceIsAzDo ? GetAzDoRepoUrl(sourceRepo) : GetRepoUrl(sourceOrg, sourceRepo); string targetUrl = targetIsAzDo ? GetAzDoRepoUrl(targetRepo) : GetRepoUrl(targetRepo); string[] command = { "add-subscription", "-q", "--channel", sourceChannelName, "--source-repo", sourceUrl, "--target-repo", targetUrl, "--target-branch", targetBranch, "--update-frequency", updateFrequency, trigger? "--trigger" : "--no-trigger" }; if (additionalOptions != null) { command = command.Concat(additionalOptions).ToArray(); } string output = await RunDarcAsync(command).ConfigureAwait(false); Match match = Regex.Match(output, "Successfully created new subscription with id '([a-f0-9-]+)'"); if (!match.Success) { throw new MaestroTestException("Unable to create subscription."); } string subscriptionId = match.Groups[1].Value; return(AsyncDisposableValue.Create(subscriptionId, async() => { TestContext.WriteLine($"Cleaning up Test Subscription {subscriptionId}"); try { await RunDarcAsync("delete-subscriptions", "--id", subscriptionId, "--quiet").ConfigureAwait(false); } catch (MaestroTestException) { // If this throws an exception the most likely cause is that the subscription was deleted as part of the test case } })); }
internal async Task <AsyncDisposableValue <Microsoft.DotNet.DarcLib.PullRequest> > GetAzDoPullRequestAsync(int pullRequestId, string targetRepoName, string targetBranch, bool isUpdated, string expectedPRTitle = null) { string repoUri = GetAzDoRepoUrl(targetRepoName); (string accountName, string projectName, string repoName) = Microsoft.DotNet.DarcLib.AzureDevOpsClient.ParseRepoUri(repoUri); string apiBaseUrl = GetAzDoApiRepoUrl(targetRepoName); if (string.IsNullOrEmpty(expectedPRTitle)) { throw new ArgumentNullException(expectedPRTitle, "ExpectedPRTitle must be defined for AzDo PRs that require an update."); } for (int tries = 10; tries > 0; tries--) { Microsoft.DotNet.DarcLib.PullRequest pr = await AzDoClient.GetPullRequestAsync($"{apiBaseUrl}/pullRequests/{pullRequestId}"); string trimmedTitle = Regex.Replace(pr.Title, @"\s+", " "); if (!isUpdated || trimmedTitle == expectedPRTitle) { return(AsyncDisposableValue.Create(pr, async() => { TestContext.WriteLine($"Cleaning up pull request {pr.Title}"); try { JObject content = await _parameters.AzDoClient.ExecuteAzureDevOpsAPIRequestAsync( HttpMethod.Patch, accountName, projectName, $"_apis/git/repositories/{targetRepoName}/pullrequests/{pullRequestId}", new NUnitLogger(), "{ \"status\" : \"abandoned\"}" ); } catch { // If this throws it means that it was cleaned up by a different clean up method first } })); } await Task.Delay(60 * 1000).ConfigureAwait(false); } throw new MaestroTestException($"The created pull request for {targetRepoName} targeting {targetBranch} was not updated with subsequent subscriptions after creation"); }
public async Task <AsyncDisposableValue <string> > CreateTestChannelAsync(string testChannelName) { string message = ""; try { message = await RunDarcAsync("delete-channel", "--name", testChannelName).ConfigureAwait(false); } catch (MaestroTestException) { // If there are subscriptions associated the the channel then a previous test clean up failed // Run a subscription clean up and try again try { await DeleteSubscriptionsForChannel(testChannelName).ConfigureAwait(false); await RunDarcAsync("delete-channel", "--name", testChannelName).ConfigureAwait(false); } catch (MaestroTestException) { // Otherwise ignore failures from delete-channel, its just a pre-cleanup that isn't really part of the test // And if the test previously succeeded then it'll fail because the channel doesn't exist } } await RunDarcAsync("add-channel", "--name", testChannelName, "--classification", "test").ConfigureAwait(false); return(AsyncDisposableValue.Create(testChannelName, async() => { TestContext.WriteLine($"Cleaning up Test Channel {testChannelName}"); try { string doubleDelete = await RunDarcAsync("delete-channel", "--name", testChannelName).ConfigureAwait(false); } catch (MaestroTestException) { // Ignore failures from delete-channel on cleanup, this delete is here to ensure that the channel is deleted // even if the test does not do an explicit delete as part of the test. Other failures are typical that the channel has already been deleted. } })); }
public async Task <AsyncDisposableValue <string> > CreateSubscriptionAsync(string sourceChannelName, string sourceRepo, string targetRepo, string targetBranch, string updateFrequency) { string output = await RunDarcAsync("add-subscription", "-q", "--no-trigger", "--channel", sourceChannelName, "--source-repo", GetRepoUrl("dotnet", sourceRepo), "--target-repo", GetRepoUrl(targetRepo), "--target-branch", targetBranch, "--update-frequency", updateFrequency).ConfigureAwait(false); Match match = Regex.Match(output, "Successfully created new subscription with id '([a-f0-9-]+)'"); if (match.Success) { string subscriptionId = match.Groups[1].Value; return(AsyncDisposableValue.Create(subscriptionId, async() => { TestContext.WriteLine($"Cleaning up Test Subscription {subscriptionId}"); await RunDarcAsync("delete-subscriptions", "--id", subscriptionId, "--quiet").ConfigureAwait(false); })); } throw new MaestroTestException("Unable to create subscription."); }