Ejemplo n.º 1
0
 public static DataContracts.GitHub.GithubEventResult ToContract(this Domain.Entities.GithubEventResult source)
 => ToContract <Domain.Entities.GithubEventResult, DataContracts.GitHub.GithubEventResult>(CurrentMapper, source);
Ejemplo n.º 2
0
        private void ExecutePersistencyEvent()
        {
            var token = source.Token;

            while (!disposed)
            {
                try
                {
                    Thread.Sleep(2000);
                    if (!token.IsCancellationRequested && (this.pullRequestEvents.TryDequeue(out IGithubEvent @event) || (this.pullRequestEvents.Count == 0 && this.pushEvents.TryDequeue(out @event))))
                    {
                        var appLocated = this.appRepository.GetByAsync(app => @event.ApplicationId == app.Id && app.TeamCode == @event.TeamId && app.OrganisationId == @event.OrganisationId && app.WebHookEnabled).GetAwaiter().GetResult();
                        if (appLocated != null)
                        {
                            Domain.Entities.GithubEventResult eventResult = null;
                            if (@event is PushEvent)
                            {
                                var pushEvent = (@event as PushEvent);

                                eventResult = this.repository.GetByAsync(x => x.OrganisationId == appLocated.OrganisationId && x.TeamCode == appLocated.TeamCode && x.AppId == appLocated.Id && x.PushCommit == pushEvent.HeadCommit.Id).GetAwaiter().GetResult();

                                if (eventResult == null)
                                {
                                    eventResult = new Domain.Entities.GithubEventResult
                                    {
                                        FromBranch     = pushEvent.Ref.Replace("refs/heads/", ""),
                                        ToBranch       = "",
                                        PushCommit     = pushEvent.HeadCommit.Id,
                                        CommitMessage  = pushEvent.HeadCommit.Message,
                                        OrganisationId = appLocated.OrganisationId,
                                        TeamCode       = appLocated.TeamCode,
                                        AppId          = appLocated.Id,
                                    };


                                    if (!appLocated.KeyBranches.Any(b => Regex.IsMatch(eventResult.FromBranch, b.BranchPattern)))
                                    {
                                        continue;
                                    }

                                    this.repository.AddAsync(eventResult).GetAwaiter().GetResult();
                                }

                                eventResult.OrganisationId = appLocated.OrganisationId;
                                eventResult.TeamCode       = appLocated.TeamCode;
                                eventResult.AppId          = appLocated.Id;
                                eventResult.Status         = "ready";
                            }
                            else
                            {
                                var pullRequestEvent = (@event as PullRequestEvent);
                                eventResult = this.repository.GetByAsync(x => x.PullRequestId == pullRequestEvent.PullRequest.Id && x.OrganisationId == appLocated.OrganisationId && x.TeamCode == appLocated.TeamCode && x.AppId == appLocated.Id).GetAwaiter().GetResult();

                                if (eventResult == null)
                                {
                                    eventResult = new Domain.Entities.GithubEventResult
                                    {
                                        FromBranch     = pullRequestEvent.PullRequest.Head.Ref,
                                        ToBranch       = pullRequestEvent.PullRequest.Base.Ref,
                                        PushCommit     = pullRequestEvent.PullRequest.MergeCommitSha,
                                        CommitMessage  = pullRequestEvent.PullRequest.Title + "|" + pullRequestEvent.PullRequest.Body,
                                        PullRequestId  = pullRequestEvent.PullRequest.Id,
                                        OrganisationId = appLocated.OrganisationId,
                                        TeamCode       = appLocated.TeamCode,
                                        AppId          = appLocated.Id,
                                    };


                                    if (!appLocated.Branches.Any(b => Regex.IsMatch(eventResult.FromBranch, b.BranchPattern)))
                                    {
                                        if (!appLocated.KeyBranches.Any(b => Regex.IsMatch(eventResult.FromBranch, b.BranchPattern)))
                                        {
                                            continue;
                                        }
                                    }


                                    if (!appLocated.KeyBranches.Any(b => Regex.IsMatch(eventResult.ToBranch, b.BranchPattern)))
                                    {
                                        continue;
                                    }

                                    eventResult = this.repository.AddAsync(eventResult).GetAwaiter().GetResult();
                                }

                                eventResult.OrganisationId = appLocated.OrganisationId;
                                eventResult.TeamCode       = appLocated.TeamCode;
                                eventResult.AppId          = appLocated.Id;
                                eventResult.PullRequestId  = pullRequestEvent.PullRequest.Id;
                                eventResult.FromBranch     = pullRequestEvent.PullRequest.Head.Ref;
                                eventResult.ToBranch       = pullRequestEvent.PullRequest.Base.Ref;
                                eventResult.PushCommit     = pullRequestEvent.PullRequest.MergeCommitSha;
                                eventResult.CommitMessage  = pullRequestEvent.PullRequest.Title + "|" + pullRequestEvent.PullRequest.Body;

                                eventResult.Status = pullRequestEvent.Action == "open" ? "pending" : pullRequestEvent.Action == "closed" && !pullRequestEvent.PullRequest.Merged ? "discard" : "pending";
                            }

                            this.repository.UpdateAsync(eventResult).GetAwaiter().GetResult();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }