| | 1 | | using System; |
| | 2 | | using System.Globalization; |
| | 3 | | using System.Net; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | | using ApplicationCore.Model; |
| | 6 | | using Microsoft.AspNetCore.Http; |
| | 7 | |
|
| | 8 | | namespace ApplicationCore |
| | 9 | | { |
| | 10 | | public class PostCommentService : IPostCommentService |
| | 11 | | { |
| | 12 | | private readonly CommentConfig _config; |
| | 13 | | private readonly ICommentFactory _commentFactory; |
| | 14 | | private readonly IPullRequestService _pullRequestService; |
| | 15 | |
|
| 10 | 16 | | public PostCommentService( |
| 10 | 17 | | CommentConfig config, |
| 10 | 18 | | ICommentFactory commentFactory, |
| 10 | 19 | | IPullRequestService pullRequestService) |
| 10 | 20 | | { |
| 10 | 21 | | _config = config; |
| 10 | 22 | | _commentFactory = commentFactory; |
| 10 | 23 | | _pullRequestService = pullRequestService; |
| 10 | 24 | | } |
| | 25 | |
|
| | 26 | | public async Task<PostCommentResult> PostCommentAsync(IFormCollection form) |
| 8 | 27 | | { |
| 8 | 28 | | if (form is null) |
| 1 | 29 | | { |
| 1 | 30 | | throw new ArgumentNullException(nameof(form)); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | //Make sure the site posting the comment is the correct site. |
| 7 | 34 | | var allowedSite = _config.WebsiteUrl.AbsoluteUri; |
| 7 | 35 | | var postedSite = form["CommentSite"]; // TODO: fix magic string |
| 7 | 36 | | if (!string.IsNullOrWhiteSpace(allowedSite) && !AreSameSites(allowedSite, postedSite)) |
| 3 | 37 | | { |
| 3 | 38 | | return new PostCommentResult( |
| 3 | 39 | | HttpStatusCode.BadRequest, |
| 3 | 40 | | string.Format( |
| 3 | 41 | | CultureInfo.InvariantCulture, |
| 3 | 42 | | CommentResources.AreNotSameSitesErrorMessage, |
| 3 | 43 | | postedSite.ToString() ?? "null")); |
| | 44 | | } |
| | 45 | |
|
| 4 | 46 | | var commentResult = await _commentFactory.CreateFromFormAsync(form).ConfigureAwait(false); |
| | 47 | |
|
| 4 | 48 | | if (!commentResult.HasErrors) |
| 3 | 49 | | { |
| 3 | 50 | | var pullRequestResult = await _pullRequestService.TryCreatePullRequestAsync(commentResult.Comment).Confi |
| | 51 | |
|
| 3 | 52 | | if (!pullRequestResult.HasError) |
| 2 | 53 | | { |
| 2 | 54 | | if (!Uri.TryCreate(form["Redirect"], UriKind.Absolute, out var redirectUri)) |
| 1 | 55 | | { |
| 1 | 56 | | return new PostCommentResult(HttpStatusCode.OK); |
| | 57 | | } |
| | 58 | | else |
| 1 | 59 | | { |
| 1 | 60 | | return new PostCommentResult(HttpStatusCode.Redirect, redirectUri); |
| | 61 | | } |
| | 62 | | } |
| | 63 | | else |
| 1 | 64 | | { |
| 1 | 65 | | return new PostCommentResult(HttpStatusCode.BadRequest, $"Creating pull request failed: {pullRequest |
| | 66 | | } |
| | 67 | | } |
| | 68 | | else |
| 1 | 69 | | { |
| | 70 | | // TODO: Include possible exception |
| 1 | 71 | | return new PostCommentResult(HttpStatusCode.BadRequest, string.Join("\n", commentResult.Errors)); |
| | 72 | | } |
| 7 | 73 | | } |
| | 74 | |
|
| | 75 | | private static bool AreSameSites(string commentSite, string postedCommentSite) |
| 7 | 76 | | { |
| 7 | 77 | | return Uri.TryCreate(commentSite, UriKind.Absolute, out var commentSiteUri) |
| 7 | 78 | | && Uri.TryCreate(postedCommentSite, UriKind.Absolute, out var postedCommentSiteUri) |
| 7 | 79 | | && commentSiteUri.Host.Equals(postedCommentSiteUri.Host, StringComparison.OrdinalIgnoreCase); |
| 7 | 80 | | } |
| | 81 | | } |
| | 82 | | } |