protected override DriverResult Editor(GITSettingsPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            var previousPassword = part.Password;

            updater.TryUpdateModel(part, Prefix, null, null);

            // check whether the form is posted or not
            IsRenderedModel temp = new IsRenderedModel();

            updater.TryUpdateModel(temp, Prefix, null, null);
            if (!temp.IsRendered)
            {
                return(null);
            }

            if (!part.IsValid())
            {
                return(null);
            }

            part.Server = part.Server.Trim();
            var record = this.svnServerRepository.Table.FirstOrDefault(c => c.Server == part.Server);

            if (record == null)
            {
                record = new GITServerRecord {
                    Server = part.Server, LastRevision = part.LastRevision, FromDate = DateTime.UtcNow
                };
                this.svnServerRepository.Create(record);
            }
            else
            {
                record.LastRevision = part.LastRevision;
            }

            part.LastSuccessfullConnectionTime = null;
            part.LatestError     = string.Empty;
            part.LatestErrorTime = null;

            return(ContentShape("Parts_GITSettings_Edit", () =>
            {
                // restore password if the input is empty, meaning it has not been reseted
                if (string.IsNullOrEmpty(part.Password))
                {
                    part.Password = previousPassword;
                }
                return shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: part, Prefix: Prefix);
            })
                   .OnGroup("GIT Client"));
        }
        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);
                }
            }
        }