public async Task SubscriptionDeletedAsync(string user) { ConditionalValue <InProgressPullRequest> maybePr = await StateManager.TryGetStateAsync <InProgressPullRequest>(PullRequest); if (maybePr.HasValue) { InProgressPullRequest pr = maybePr.Value; if (string.IsNullOrEmpty(pr.Url)) { // somehow a bad PR got in the collection, remove it await StateManager.RemoveStateAsync(PullRequest); return; } long installationId = 0; if (pr.Url.Contains("github.com")) { (string owner, string repo, int id) = GitHubClient.ParsePullRequestUri(pr.Url); installationId = await Context.GetInstallationId($"https://github.com/{owner}/{repo}"); } IRemote darc = await DarcFactory.CreateAsync(pr.Url, installationId); await darc.CreatePullRequestCommentAsync( pr.Url, $@"The subscription that generated this pull request has been deleted by @{user}. This pull request will no longer be tracked by maestro."); await StateManager.RemoveStateAsync(PullRequest); } }
protected async Task <IRemote> GetDarc() { (string targetRepository, string targetBranch) = await GetTargetAsync(); long installationId = await Context.GetInstallationId(targetRepository); return(await DarcFactory.CreateAsync(targetRepository, installationId)); }
public async Task UpdateAsync(int buildId) { await SynchronizeInProgressPRAsync(); Subscription subscription = await Context.Subscriptions.FindAsync(SubscriptionId); Build build = await Context.Builds.Include(b => b.Assets) .ThenInclude(a => a.Locations) .FirstAsync(b => b.Id == buildId); string targetRepository = subscription.TargetRepository; string targetBranch = subscription.TargetBranch; long installationId = await Context.GetInstallationId(subscription.TargetRepository); IRemote darc = await DarcFactory.CreateAsync(targetRepository, installationId); List <AssetData> assets = build.Assets.Select(a => new AssetData { Name = a.Name, Version = a.Version }) .ToList(); ConditionalValue <InProgressPullRequest> maybePr = await StateManager.TryGetStateAsync <InProgressPullRequest>(PullRequest); string prUrl; if (maybePr.HasValue) { InProgressPullRequest pr = maybePr.Value; await darc.UpdatePullRequestAsync(pr.Url, build.Commit, targetBranch, assets); prUrl = pr.Url; } else { prUrl = await darc.CreatePullRequestAsync(targetRepository, targetBranch, build.Commit, assets); } var newPr = new InProgressPullRequest { Url = prUrl, BuildId = build.Id }; await StateManager.SetStateAsync(PullRequest, newPr); await Reminders.TryRegisterReminderAsync( PullRequestCheck, Array.Empty <byte>(), new TimeSpan(0, 5, 0), new TimeSpan(0, 5, 0)); await StateManager.SaveStateAsync(); }
private async Task <string> CheckMergePolicyAsyncImpl(string prUrl) { Subscription subscription = await Context.Subscriptions.FindAsync(SubscriptionId); if (subscription == null) { await Reminders.TryUnregisterReminderAsync(PullRequestCheck); await StateManager.TryRemoveStateAsync(PullRequest); return("Action Ignored: Subscription does not exist."); } ConditionalValue <InProgressPullRequest> maybePr = await StateManager.TryGetStateAsync <InProgressPullRequest>(PullRequest); if (!maybePr.HasValue) { return("Action Ignored: Pull Request not found."); } InProgressPullRequest pr = maybePr.Value; long installationId = await Context.GetInstallationId(subscription.TargetRepository); IRemote darc = await DarcFactory.CreateAsync(pr.Url, installationId); SubscriptionPolicy policy = subscription.PolicyObject; PrStatus status = await darc.GetPullRequestStatusAsync(pr.Url); switch (status) { case PrStatus.Open: string result = await CheckMergePolicyInternalAsync(darc, policy.MergePolicies, pr); if (result.StartsWith("Merged:")) { subscription.LastAppliedBuildId = pr.BuildId; await Context.SaveChangesAsync(); await StateManager.RemoveStateAsync(PullRequest); return(result); } return(result); default: return("Action Ignored: Pull Request is not Open."); } }
private async Task <string> UpdateAsyncImpl(int buildId) { await SynchronizeInProgressPRAsync(); Subscription subscription = await Context.Subscriptions.FindAsync(SubscriptionId); Build build = await Context.Builds.Include(b => b.Assets) .ThenInclude(a => a.Locations) .FirstAsync(b => b.Id == buildId); string targetRepository = subscription.TargetRepository; string targetBranch = subscription.TargetBranch; long installationId = await Context.GetInstallationId(subscription.TargetRepository); IRemote darc = await DarcFactory.CreateAsync(targetRepository, installationId); List <AssetData> assets = build.Assets.Select(a => new AssetData { Name = a.Name, Version = a.Version }) .ToList(); string title = GetTitle(subscription, build); string description = GetDescription(subscription, build, assets); ConditionalValue <InProgressPullRequest> maybePr = await StateManager.TryGetStateAsync <InProgressPullRequest>(PullRequest); InProgressPullRequest pr; if (maybePr.HasValue) { pr = maybePr.Value; await darc.UpdatePullRequestAsync(pr.Url, build.Commit, targetBranch, assets, title, description); } else { string prUrl = await darc.CreatePullRequestAsync( targetRepository, targetBranch, build.Commit, assets, pullRequestTitle : title, pullRequestDescription : description); if (string.IsNullOrEmpty(prUrl)) { return($"No Pull request created. Darc Reports no dependencies need to be updated."); } pr = new InProgressPullRequest { Url = prUrl }; } pr.BuildId = build.Id; await StateManager.SetStateAsync(PullRequest, pr); await Reminders.TryRegisterReminderAsync( PullRequestCheck, Array.Empty <byte>(), new TimeSpan(0, 5, 0), new TimeSpan(0, 5, 0)); await StateManager.SaveStateAsync(); return($"Pull request '{pr.Url}' updated."); }
public async Task SynchronizeInProgressPRAsync() { Subscription subscription = await Context.Subscriptions.FindAsync(SubscriptionId); if (subscription == null) { await Reminders.TryUnregisterReminderAsync(PullRequestCheck); await StateManager.TryRemoveStateAsync(PullRequest); return; } ConditionalValue <InProgressPullRequest> maybePr = await StateManager.TryGetStateAsync <InProgressPullRequest>(PullRequest); if (maybePr.HasValue) { InProgressPullRequest pr = maybePr.Value; long installationId = await Context.GetInstallationId(subscription.TargetRepository); IRemote darc = await DarcFactory.CreateAsync(pr.Url, installationId); MergePolicy policy = subscription.PolicyObject.MergePolicy; PrStatus status = await darc.GetPullRequestStatusAsync(pr.Url); switch (status) { case PrStatus.Open: switch (policy) { case MergePolicy.Never: return; case MergePolicy.BuildSucceeded: case MergePolicy.UnitTestPassed: // for now both of these cases are the same if (await ShouldMergePrAsync(darc, pr.Url, policy)) { await darc.MergePullRequestAsync(pr.Url); goto merged; } return; default: Logger.LogError("Unknown merge policy '{policy}'", policy); return; } case PrStatus.Merged: merged: subscription.LastAppliedBuildId = pr.BuildId; await Context.SaveChangesAsync(); goto case PrStatus.Closed; case PrStatus.Closed: await StateManager.RemoveStateAsync(PullRequest); break; default: Logger.LogError("Unknown pr status '{status}'", status); return; } } await Reminders.TryUnregisterReminderAsync(PullRequestCheck); }