Ejemplo n.º 1
0
        public ActionResult GetFileDiff(int issueId)
        {
            string newFile = string.Empty;

            string oldFile = string.Empty;

            string fileName = Request["filename"] ?? string.Empty;

            string fullfilename = Request["fullfilename"] ?? string.Empty;

            string provider = Request["provider"] ?? string.Empty;

            string revisionid = Request["revisionid"] ?? string.Empty;

            string fileid = Request["fileid"] ?? string.Empty;

            string repositoryUrl = Request["repositoryurl"] ?? string.Empty;

            string workspace = Request["workspace"] ?? string.Empty;

            fileName = fileName.Trim();

            fullfilename = fullfilename.Trim();

            provider = provider.Trim();

            revisionid = revisionid.Trim();

            fileid = fileid.Trim();

            repositoryUrl = repositoryUrl.Trim();

            workspace = workspace.Trim();

            //Authentication details
            string authenticateForm = string.Empty;

            bool IsUserAuthorized = false;

            bool isFileIdMissing = false;

            string data = string.Empty;

            string errorMessage = string.Empty;

            if (!repositoryUrl.IsEmpty())
            {
                if (provider == SourceControlProvider.GitHub.ToString())
                {
                    GitHub github = new GitHub();

                    if (github.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext))
                    {
                        try
                        {
                            if (fileid.IsEmpty())
                            {
                                isFileIdMissing = true;
                                // Need to do this, because when github sends back the committed data there is no fileid(which we need to get the file content) for the files.
                                // This will go and get the fileids once for each commit where fileid's are empty
                                fileid = github.updateFileIds(GeminiContext, repositoryUrl, revisionid, fileName, issueId);
                            }

                            newFile = github.GetFileContent(GeminiContext, issueId, repositoryUrl, revisionid, fileName, fileid);

                            oldFile = github.GetFileContent(GeminiContext, issueId, repositoryUrl, revisionid, fileName, fileid, true);

                            IsUserAuthorized = true;
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            authenticateForm = github.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                            errorMessage = "Invalid login details";
                        }
                    }
                    else
                    {
                        authenticateForm = github.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                        errorMessage = "Invalid login details";
                    }
                }
                else if (provider == SourceControlProvider.TFS2012.ToString())
                {
                    TFS2012 tfs2012 = new TFS2012();

                    if (tfs2012.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext))
                    {
                        try
                        {
                            oldFile = tfs2012.GetFileContent(GeminiContext, issueId, fileName, fullfilename, workspace, revisionid, fileid, repositoryUrl, true);

                            newFile = tfs2012.GetFileContent(GeminiContext, issueId, fileName, fullfilename, workspace, revisionid, fileid, repositoryUrl);

                            IsUserAuthorized = true;
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            authenticateForm = tfs2012.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                            errorMessage = "Invalid login details";
                        }
                        catch (TeamFoundationServerUnauthorizedException ex)
                        {
                            authenticateForm = tfs2012.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                            errorMessage = ex.Message;
                        }
                    }
                    else
                    {
                        authenticateForm = tfs2012.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);
                    }
                }
                else if (provider == SourceControlProvider.TFS2010.ToString())
                {
                    TFS2010 tfs2010 = new TFS2010();

                    if (tfs2010.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext))
                    {
                        try
                        {
                            oldFile = tfs2010.GetFileContent(GeminiContext, issueId, fileName, fullfilename, workspace, revisionid, fileid, repositoryUrl, true);

                            newFile = tfs2010.GetFileContent(GeminiContext, issueId, fileName, fullfilename, workspace, revisionid, fileid, repositoryUrl);

                            IsUserAuthorized = true;
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            authenticateForm = tfs2010.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                            errorMessage = "Invalid login details";
                        }
                        catch (TeamFoundationServerUnauthorizedException ex)
                        {
                            authenticateForm = tfs2010.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                            errorMessage = ex.Message;
                        }
                    }
                    else
                    {
                        authenticateForm = tfs2010.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);
                    }
                }
                else if (provider == SourceControlProvider.SVN.ToString())
                {
                    SVN svn = new SVN();

                    if (svn.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext))
                    {
                        try
                        {
                            oldFile = svn.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid, true);

                            newFile = svn.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid);

                            IsUserAuthorized = true;
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            authenticateForm = svn.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                            errorMessage = "Invalid login details";
                        }
                    }
                    else
                    {
                        authenticateForm = svn.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                        errorMessage = "Invalid login details";
                    }
                }
                else if (provider == SourceControlProvider.Git.ToString())
                {
                    Git git = new Git();

                    if (git.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext))
                    {
                        try
                        {
                            oldFile = git.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid, true);

                            newFile = git.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid);

                            IsUserAuthorized = true;
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            authenticateForm = git.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                            errorMessage = "Invalid login details";
                        }
                    }
                    else
                    {
                        authenticateForm = git.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                        errorMessage = "Invalid login details";
                    }
                }
                else if (provider == SourceControlProvider.Bitbucket.ToString())
                {
                    Bitbucket bitbucket = new Bitbucket();

                    if (bitbucket.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext))
                    {
                        try
                        {
                            oldFile = bitbucket.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid, true);

                            newFile = bitbucket.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid);

                            IsUserAuthorized = true;
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            authenticateForm = bitbucket.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                            errorMessage = "Invalid login details";
                        }
                    }
                    else
                    {
                        authenticateForm = bitbucket.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName);

                        errorMessage = "Invalid login details";
                    }
                }
            }
            else
            {
                errorMessage = "ERROR: Repository Url is missing";
            }

            if (IsUserAuthorized)
            {
                // Handle BOM markers
                oldFile = oldFile.Replace("\x00EF\x00BB\x00BF", "");

                newFile = newFile.Replace("\x00EF\x00BB\x00BF", "");

                // Diff
                var tmpModel = diffBuilder.BuildDiffModel(oldFile ?? string.Empty, newFile ?? string.Empty);

                IssueWidgetData <List <Comment> > tmpData = GeminiContext.IssueWidgetStore.Get <List <Comment> >(issueId, Constants.AppId, Constants.ControlId);

                DiffplexComments model = new DiffplexComments();

                if (tmpData != null)
                {
                    SourceControlProvider enumProvider;

                    try
                    {
                        enumProvider = (SourceControlProvider)Enum.Parse(typeof(SourceControlProvider), provider, true);
                    }
                    catch (ArgumentException ex)
                    {
                        enumProvider = 0;

                        GeminiApp.LogException(new Exception(ex.Message)
                        {
                            Source = "Saucery"
                        }, false);
                    }

                    var comments = tmpData.Value.FindAll(f => f.FileName == fileName && f.RepositoryUrl == repositoryUrl && f.Provider == enumProvider && f.ChangesetId == revisionid);

                    if (comments != null)
                    {
                        model.data = tmpModel;

                        model.comments = comments;
                    }
                }
                else
                {
                    model.data = tmpModel;

                    model.comments = new List <Comment>();
                }

                data = RenderPartialViewToString(this, AppManager.Instance.GetAppUrl("F473D13E-19B7-45F3-98ED-6ED77B6BAB0A", "views/diff.cshtml"), model);
            }
            else
            {
                data = authenticateForm;
            }

            return(JsonSuccess(new { authenticationSuccess = IsUserAuthorized, fileid = fileid, data = data, isFileIdMissing = isFileIdMissing, errorMessage = errorMessage }));
        }
Ejemplo n.º 2
0
        public ActionResult Authenticate(SourceControlProvider provider)
        {
            //Authentication
            string username = Request["username"] ?? string.Empty;

            string password = Request["password"] ?? string.Empty;

            string repositoryUrl = Request["repositoryurl"] ?? string.Empty;

            string filename = Request["filename"] ?? string.Empty;

            string message = string.Empty; //Commit message

            bool success = true;

            string extraData = string.Empty;

            if (username.IsEmpty() || password.IsEmpty() || provider.ToString().IsEmpty() || repositoryUrl.IsEmpty())
            {
                message = "Please make sure Username, Password are not empty";

                success = false;
            }

            if (success)
            {
                UserWidgetDataDetails userData = new UserWidgetDataDetails();

                userData.Username = username.Trim();

                userData.Password = SecretsHelper.Encrypt(password.Trim(), SecretsHelper.EncryptionKey);

                userData.Provider = provider;

                userData.RepositoryUrl = repositoryUrl.Trim();

                userData.AccessToken = string.Empty;

                if (provider == SourceControlProvider.SVN)
                {
                    SVN svn = new SVN();

                    svn.SaveLoginDetails(CurrentUser, userData, GeminiContext);
                }
                else if (provider == SourceControlProvider.GitHub)
                {
                    GitHub github = new GitHub();

                    github.SaveLoginDetails(CurrentUser, userData, GeminiContext);

                    extraData = string.Format("https://github.com/login/oauth/authorize?client_id={0}&redirect_uri={1}apps/saucery/github/authenticate?state={2}&scope=repo", username, UserContext.Url, CurrentUser.Entity.Id);
                }
                else if (provider == SourceControlProvider.TFS2012)
                {
                    TFS2012 tfs2012 = new TFS2012();

                    tfs2012.SaveLoginDetails(CurrentUser, userData, GeminiContext);
                }
                else if (provider == SourceControlProvider.TFS2010)
                {
                    TFS2010 tfs2010 = new TFS2010();

                    tfs2010.SaveLoginDetails(CurrentUser, userData, GeminiContext);
                }
                else if (provider == SourceControlProvider.Git)
                {
                    Git git = new Git();
                    git.SaveLoginDetails(CurrentUser, userData, GeminiContext);
                }
                else if (provider == SourceControlProvider.Bitbucket)
                {
                    Bitbucket bitbucket = new Bitbucket();
                    bitbucket.SaveLoginDetails(CurrentUser, userData, GeminiContext);
                }
            }

            return(JsonSuccess(new { success = success, message = message, extraData = extraData }));
        }