private void RaiseWorkflow(GITServerBranchRecord hostRecord, Commit log)
        {
            var contentManager    = this.orchardServices.ContentManager;
            var gitLogContentItem = contentManager.New(GITCommitPart.ContentItemTypeName);
            var gitLogPart        = gitLogContentItem.As <GITCommitPart>();


            gitLogPart.LogMessage = log.Message;
            gitLogPart.Author     = log.Author.Name;
            gitLogPart.Sha        = log.Sha;
            gitLogPart.Time       = log.Author.When.DateTime;

            contentManager.Create(gitLogContentItem);
            contentManager.Publish(gitLogContentItem);

            workflowManager.TriggerEvent(
                GITCommitReceivedActivity.ActivityName,
                gitLogContentItem,
                () => new Dictionary <string, object> {
                { "Content", gitLogContentItem }
            });

            hostRecord.Sha = log.Sha;
            this.svnServerRepository.Flush();
        }
        public void Process(ScheduledTaskContext context)
        {
            if (context.Task.TaskType == TaskType)
            {
                var settings = this.orchardServices.WorkContext.CurrentSite.As <GITSettingsPart>();
                if (string.IsNullOrEmpty(settings.Server))
                {
                    return;
                }

                try
                {
                    this.transactionManager.Demand();
                    var gitPath = this.appDataFolder.Combine("Sites", shellSettings.Name, "GIT");
                    if (!this.appDataFolder.DirectoryExists(gitPath))
                    {
                        this.appDataFolder.CreateDirectory(gitPath);
                    }

                    var serverRecord = this.svnServerRepository.Table.FirstOrDefault(c => c.Server == settings.Server);

                    var        path       = Path.Combine(gitPath, settings.LocalFolder);
                    var        mappedPath = this.appDataFolder.MapPath(path);
                    Repository repo       = null;
                    if (serverRecord == null)
                    {
                        if (this.appDataFolder.DirectoryExists(path))
                        {
                            Directory.Delete(mappedPath, true);
                        }

                        this.appDataFolder.CreateDirectory(path);
                        // var destination = Repository.Init(mappedPath);
                        CloneOptions options = new CloneOptions();
                        options.CredentialsProvider = new LibGit2Sharp.Handlers.CredentialsHandler(
                            (url, usernameFromUrl, types) => new UsernamePasswordCredentials()
                        {
                            Username = settings.Username,
                            Password = settings.Password
                        });
                        Repository.Clone(settings.Server, mappedPath, options);
                        repo = new Repository(mappedPath);

                        serverRecord = new GITServerRecord {
                            Server = settings.Server, LastRevision = settings.LastRevision, FromDate = DateTime.UtcNow
                        };
                        this.svnServerRepository.Create(serverRecord);
                        this.svnServerRepository.Flush();
                    }

                    repo = repo ?? new Repository(mappedPath);
                    var branches = !string.IsNullOrEmpty(settings.Branches) ? settings.Branches.Split(',') : new[] { "master" };
                    branches = branches.Select(c => c.Trim()).Where(c => !string.IsNullOrEmpty(c)).ToArray();

                    if (string.IsNullOrEmpty(settings.Password) || string.IsNullOrEmpty(settings.Username))
                    {
                        repo.Network.Fetch("origin", branches.Select(c => c + ":" + c));
                    }
                    else
                    {
                        FetchOptions options = new FetchOptions();
                        options.CredentialsProvider = new LibGit2Sharp.Handlers.CredentialsHandler(
                            (url, usernameFromUrl, types) => new UsernamePasswordCredentials()
                        {
                            Username = settings.Username,
                            Password = settings.Password
                        });
                        repo.Network.Fetch("origin", branches.Select(c => c + ":" + c), options);
                    }

                    var branchRecords = this.gitServerBranchRepository.Table.Where(c => c.ServerRecord.Id == serverRecord.Id).ToList();

                    foreach (var branchName in branches)
                    {
                        var branch = repo.Branches.FirstOrDefault(c => c.FriendlyName == branchName);
                        if (branch == null)
                        {
                            continue;
                        }

                        var branchRecord = branchRecords.FirstOrDefault(c => c.BranchName == branchName);
                        if (branchRecord == null)
                        {
                            branchRecord = new GITServerBranchRecord {
                                BranchName = branchName, ServerRecord = serverRecord, LastUpdate = DateTime.UtcNow
                            };
                            this.gitServerBranchRepository.Create(branchRecord);
                            this.gitServerBranchRepository.Flush();
                        }

                        var commits = branch.Commits.ToList();
                        if (!string.IsNullOrEmpty(branchRecord.Sha))
                        {
                            var commit = commits.FirstOrDefault(c => c.Sha == branchRecord.Sha);
                            if (commit != null)
                            {
                                int index = commits.IndexOf(commit);
                                commits = commits.Skip(index + 1).ToList();
                            }
                        }

                        foreach (var commit in commits)
                        {
                            RaiseWorkflow(branchRecord, commit);
                        }
                    }

                    var settingContentItem = this.orchardServices.ContentManager.Get(settings.Id);
                    var gitSettingPart     = settingContentItem.As <GITSettingsPart>();
                    gitSettingPart.LastSuccessfullConnectionTime = DateTime.UtcNow;
                    gitSettingPart.LatestError     = null;
                    gitSettingPart.LatestErrorTime = null;
                    this.transactionManager.Demand();
                }
                catch (Exception e)
                {
                    this.transactionManager.Cancel();

                    this.Logger.Error(e, e.Message);

                    // We need a new transaction for storing the data
                    this.transactionManager.RequireNew();
                    settings.LatestError     = e.Message;
                    settings.LatestErrorTime = DateTime.UtcNow;
                    var settingContentItem = this.orchardServices.ContentManager.Get(settings.Id);
                    var gitSettingPart     = settingContentItem.As <GITSettingsPart>();
                    gitSettingPart.LatestError     = e.Message;
                    gitSettingPart.LatestErrorTime = settings.LatestErrorTime;
                    this.transactionManager.Demand();
                }
                finally
                {
                    DateTime nextTaskDate = DateTime.UtcNow.AddMinutes(PeriodInMinutes);
                    this.ScheduleNextTask(nextTaskDate);
                }
            }
        }