Пример #1
0
        public void getTypes(String LocalFilePath)
        {
            var ext = new List <string> {
                ".java"
            };
            //TODO: Project path eka UI eken aragen danda
            var myFiles = Directory.GetFiles(@LocalFilePath, "*.*", SearchOption.AllDirectories)
                          .Where(s => ext.Any(e => s.EndsWith(e)));

            using (var _cycloneEntities = new cycloneEntities())
            {
                string analyzerid = _cycloneEntities.source_analyzer.OrderByDescending(a => a.processed_time).FirstOrDefault().analyzer_id;
                var    filesList  = _cycloneEntities.clone_class.Where(a => a.analyzer_id == analyzerid).ToList();
                foreach (clone_class cloneclass in filesList)
                {
                    var fragmentList = _cycloneEntities.clone_fragment.Where(a => a.analyzer_id == analyzerid && a.clone_class_id == cloneclass.cloneclass_id).ToList();
                    foreach (clone_fragment cf in fragmentList)
                    {
                        int i = 0;
                        if (i > 0)
                        {
                            break;
                        }
                        String CodeBlockI = getCodeByStartAndEnd(cf.source_path, cf.start_line, cf.end_line);
                        bool   isTypeII   = false;
                        for (int j = 0; j < fragmentList.Count; j++)
                        {
                            if (i < j)
                            {
                                String CodeBlockII = getCodeByStartAndEnd(fragmentList[j].source_path,
                                                                          fragmentList[j].start_line, fragmentList[j].end_line);
                                Boolean status = CompareCodeBlocks(CodeBlockI, CodeBlockII);

                                if (!status)
                                {
                                    isTypeII = true;
                                    break;
                                }
                            }
                        }
                        clone_class cloneClass = _cycloneEntities.clone_class.Where(a => a.cloneclass_id == cloneclass.cloneclass_id && a.analyzer_id == cloneclass.analyzer_id).FirstOrDefault();
                        if (isTypeII)
                        {
                            cloneClass.clone_class_type = "Type2";
                        }
                        else
                        {
                            cloneClass.clone_class_type = "Type1";
                        }
                        _cycloneEntities.clone_class.Attach(cloneClass);
                        var entry = _cycloneEntities.Entry(cloneClass);
                        entry.Property(e => e.clone_class_type).IsModified = true;
                        _cycloneEntities.SaveChanges();
                        i++;
                    }
                }
            }
        }
Пример #2
0
        private void TimelyAnalyzeSource(string DestinationPath, string projectREF, source source)
        {
            workingDirectory = ConfigurationSettings.AppSettings["DefaultSourceLocation"];
            String DefaultOutputLocation = ConfigurationSettings.AppSettings["DefaultOutputLocation"];
            String batchFileLocation     = ConfigurationSettings.AppSettings["CONQATEngineBATPath"];

            if (Directory.Exists(workingDirectory))
            {
                grantAccess(workingDirectory);

                if (Directory.Exists(workingDirectory + "\\" + projectREF))
                {
                    grantAccess(workingDirectory + "\\" + projectREF);
                    if (File.Exists(workingDirectory + "\\" + projectREF + "\\" + projectREF + ".cqb"))
                    {
                        if (File.Exists(workingDirectory + "\\" + projectREF + "\\" + projectREF + ".cqr"))
                        {
                            var batchFile = workingDirectory + "\\" + projectREF + "\\" + projectREF + ".bat";
                            if (File.Exists(batchFile))
                            {
                                try
                                {
                                    Process          p;
                                    ProcessStartInfo psi = new ProcessStartInfo();
                                    psi.UseShellExecute = false;
                                    psi.CreateNoWindow  = true;
                                    psi.FileName        = batchFile;
                                    psi.Arguments       = " /k start /wait";
                                    p = System.Diagnostics.Process.Start(psi);
                                    do
                                    {
                                        if (!p.HasExited)
                                        {
                                            Thread.Sleep(1000);
                                        }
                                    } while (!p.WaitForExit(1000));

                                    p.WaitForExit();
                                    p.Close();
                                    //read the xml and update the database
                                    if (!File.Exists(workingDirectory + "\\" + projectREF + "\\" + "clones.xml"))
                                    {
                                        //strStatus = "Clones not found!";
                                    }
                                    else
                                    {
                                        XmlReader             xmlReader         = new XmlReader();
                                        source_analyzer       sourceAnalyzer    = new source_analyzer();
                                        List <source_file>    sourceFileList    = new List <source_file>();
                                        List <clone_class>    cloneClassList    = new List <clone_class>();
                                        List <clone_fragment> cloneFragmentList = new List <clone_fragment>();
                                        sourceAnalyzer = xmlReader.ReadCloneXml(workingDirectory + "\\" + projectREF + "\\" + "clones.xml",
                                                                                projectREF, out sourceFileList, out cloneClassList, out cloneFragmentList);
                                        sourceAnalyzer.source_path = DestinationPath;
                                        //source source = new source();
                                        //source.create_date = DateTime.Now;
                                        //source.location = LocalFilePath;
                                        //source.source_id = projectREF;
                                        //source.source_name = ProjectName;
                                        //source.git_url = GitHubUrl;
                                        using (var _cycloneEntities = new cycloneEntities())
                                        {
                                            //_cycloneEntities.sources.Add(source);
                                            _cycloneEntities.SaveChanges();
                                            foreach (source_file sourceFile in sourceFileList)
                                            {
                                                sourceFile.source_id = source.source_id;
                                                _cycloneEntities.source_file.Add(sourceFile);
                                            }
                                            _cycloneEntities.SaveChanges();

                                            _cycloneEntities.source_analyzer.Add(sourceAnalyzer);
                                            _cycloneEntities.SaveChanges();
                                            foreach (clone_class cloneClass in cloneClassList)
                                            {
                                                _cycloneEntities.clone_class.Add(cloneClass);
                                            }
                                            _cycloneEntities.SaveChanges();
                                            foreach (clone_fragment cloneFragment in cloneFragmentList)
                                            {
                                                _cycloneEntities.clone_fragment.Add(cloneFragment);
                                            }
                                            _cycloneEntities.SaveChanges();
                                            TypeDetector _typeDetector = new TypeDetector();
                                            _typeDetector.getTypes(DestinationPath);
                                            SetUpFilesForVisualizaton(sourceAnalyzer.analyzer_id);
                                        }
                                    }
                                }
                                catch (DbEntityValidationException ex)
                                {
                                    // Retrieve the error messages as a list of strings.
                                    var errorMessages = ex.EntityValidationErrors
                                                        .SelectMany(x => x.ValidationErrors)
                                                        .Select(x => x.ErrorMessage);

                                    // Join the list to a single string.
                                    var fullErrorMessage = string.Join("; ", errorMessages);

                                    // Combine the original exception message with the new one.
                                    var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                                    // Throw a new DbEntityValidationException with the improved exception message.
                                    throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
                                }
                            }
                            else
                            {
                                string cqrFile = workingDirectory + projectREF + @"\" + projectREF + ".cqr";
                                var    command = @batchFileLocation + "conqat.bat" + @" -f  " + @"""" + cqrFile + @"""";
                                File.WriteAllText(workingDirectory + "\\" + projectREF + "\\" + projectREF + ".bat", command);
                                TimelyAnalyzeSource(DestinationPath, projectREF, source);
                            }
                        }
                        else
                        {
                            //creating cqr
                            using (StreamReader sr = new StreamReader("Templates\\CQR\\template.cqr"))
                            {
                                String cqrText = sr.ReadToEnd();
                                cqrText = cqrText.Replace("{blockSpecname}", projectREF);
                                File.WriteAllText(workingDirectory + "\\" + projectREF + "\\" + projectREF + ".cqr", cqrText);
                                TimelyAnalyzeSource(DestinationPath, projectREF, source);
                            }
                        }
                    }
                    else
                    {
                        //creating cqb
                        using (StreamReader sr = new StreamReader("Templates\\CQB\\template.cqb"))
                        {
                            String cqbText = sr.ReadToEnd();
                            cqbText = cqbText.Replace("{blockSpecname}", projectREF)
                                      .Replace("{rootDir}", DestinationPath)
                                      .Replace("{xmlOutput}", @DefaultOutputLocation + projectREF + @"\")
                                      .Replace("{htmlPresentationOutput}", @DefaultOutputLocation + projectREF + @"\");
                            File.WriteAllText(workingDirectory + "\\" + projectREF + "\\" + projectREF + ".cqb", cqbText);
                            TimelyAnalyzeSource(DestinationPath, projectREF, source);
                        }
                    }
                }
                else
                {
                    Directory.CreateDirectory(workingDirectory + "\\" + projectREF);
                    TimelyAnalyzeSource(DestinationPath, projectREF, source);
                }
            }
        }
Пример #3
0
        public void RetriveFromHook()
        {
            using (_onlineDBEntities = new onlineDBEntities())
            {
                //using (TransactionScope dbContextTransaction = new TransactionScope())
                //{
                try
                {
                    List <webHook> webHookList = _onlineDBEntities.webHooks
                                                 .Where(a => a.isSync != true).ToList();

                    List <CodeCloneAnalysis_DB.webhook>         webHookListForMYSQL    = new List <webhook>();
                    List <CodeCloneAnalysis_DB.webhook_commits> webHookCommitsForMYSQL = new List <webhook_commits>();
                    List <CodeCloneAnalysis_DB.webhook_files>   webHookFilesForMYSQL   = new List <webhook_files>();
                    String source_id = "";

                    foreach (webHook _webhook in webHookList)
                    {
                        CodeCloneAnalysis_DB.webhook wh = new CodeCloneAnalysis_DB.webhook();
                        wh.after_id             = _webhook.after;
                        wh.before_id            = _webhook.before;
                        wh.entry_time           = _webhook.entryTime;
                        wh.head_commit_added    = _webhook.headCommitAdded;
                        wh.head_commit_id       = _webhook.headCommitId;
                        wh.head_commit_message  = _webhook.headCommitMessage;
                        wh.head_commit_modified = _webhook.headCommitModified;
                        wh.head_commit_removed  = _webhook.headCommitRemoved;
                        wh.head_commit_url      = _webhook.headCommitUrl;
                        wh.repository_id        = _webhook.repositoryId;
                        wh.repository_url       = _webhook.repositoryUrl;
                        wh.webhook_ref_id       = _webhook.webHookRefId;
                        wh.source_id            = GetSourceIdByUrl(_webhook.repositoryUrl);
                        source_id = wh.source_id;
                        webHookListForMYSQL.Add(wh);

                        foreach (webHookFile _webHookfile in _webhook.webHookFiles)
                        {
                            CodeCloneAnalysis_DB.webhook_files whf = new webhook_files();
                            whf.additions      = _webHookfile.additions;
                            whf.changes        = _webHookfile.changes;
                            whf.deletions      = _webHookfile.deletions;
                            whf.file_id        = _webHookfile.fileId;
                            whf.file_name      = _webHookfile.fileName;
                            whf.patch          = _webHookfile.patch;
                            whf.status         = _webHookfile.Status;
                            whf.webhook_ref_id = _webhook.webHookRefId;
                            webHookFilesForMYSQL.Add(whf);
                        }

                        foreach (webHookCommit _webHookCommit in _webhook.webHookCommits)
                        {
                            CodeCloneAnalysis_DB.webhook_commits whc = new webhook_commits();
                            whc.added               = _webHookCommit.added;
                            whc.author_name         = _webHookCommit.authorName;
                            whc.author_user_name    = _webHookCommit.authorUserName;
                            whc.committer_name      = _webHookCommit.committerName;
                            whc.committer_user_name = _webHookCommit.committerUserName;
                            whc.git_commit_id       = _webHookCommit.gitCommitId;
                            whc.icommit_id          = _webHookCommit.icommitId;
                            whc.message             = _webHookCommit.message;
                            whc.modified            = _webHookCommit.modified;
                            whc.removed             = _webHookCommit.removed;
                            whc.timestamp           = _webHookCommit.timestamp;
                            whc.url            = _webHookCommit.url;
                            whc.webhook_ref_id = _webhook.webHookRefId;
                            webHookCommitsForMYSQL.Add(whc);
                        }
                    }

                    using (_cycloneEntities = new cycloneEntities())
                    {
                        foreach (webhook wh in webHookListForMYSQL)
                        {
                            _cycloneEntities.webhooks.Add(wh);
                        }
                        _cycloneEntities.SaveChanges();
                        foreach (webhook_commits whc in webHookCommitsForMYSQL)
                        {
                            _cycloneEntities.webhook_commits.Add(whc);
                        }
                        foreach (webhook_files whf in webHookFilesForMYSQL)
                        {
                            _cycloneEntities.webhook_files.Add(whf);
                        }
                        _cycloneEntities.SaveChanges();

                        foreach (webHook webHook in webHookList)
                        {
                            webHook.isSync   = true;
                            webHook.TimeSync = DateTime.Now;

                            _onlineDBEntities.webHooks.Attach(webHook);
                            var entry = _onlineDBEntities.Entry(webHook);
                            entry.Property(e => e.isSync).IsModified   = true;
                            entry.Property(e => e.TimeSync).IsModified = true;
                        }
                        UpdateModifiedUsers(webHookList, source_id);
                    }
                    _onlineDBEntities.SaveChanges();
                    // dbContextTransaction.Complete();
                }
                catch (Exception ex)
                {
                    // dbContextTransaction.Dispose();
                }
                finally
                {
                    // dbContextTransaction.Dispose();
                }
                //}
            }
        }
Пример #4
0
        private void UpdateModifiedUsers(List <webHook> webHook, string source_id)
        {
            using (_cycloneEntities = new cycloneEntities())
            {
                if (webHook.Count > 0)
                {
                    string lastAnalysisId = _cycloneEntities.source_analyzer.Where(a => a.source_id == source_id)
                                            .OrderByDescending(a => a.processed_time).FirstOrDefault().analyzer_id;

                    List <clone_fragment> cloneFragments = _cycloneEntities.clone_fragment
                                                           .Where(a => a.analyzer_id == lastAnalysisId).ToList();
                    foreach (webHook wh in webHook)
                    {
                        foreach (webHookFile whf in wh.webHookFiles)
                        {
                            if (whf.patch == null)
                            {
                                continue;
                            }
                            String[]      changes    = whf.patch.Split(new[] { "@@" }, StringSplitOptions.RemoveEmptyEntries);
                            List <String> StartLines = new List <string>();
                            for (int i = 0; i < changes.Length; i++)
                            {
                                if (changes[i].Trim().StartsWith("-"))
                                {
                                    StartLines.Add(changes[i].ToString());
                                }
                            }

                            for (int i = 0; i < cloneFragments.Count(); i++)
                            {
                                if (cloneFragments[i].source_path.ToString().Split('\\').Last() == whf.fileName.Split('/').Last())
                                {
                                    bool changed = false;
                                    foreach (String str in StartLines)
                                    {
                                        int startingLine = Convert.ToInt32(str.Replace("-", "").Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).First());
                                        if (startingLine >= cloneFragments[i].start_line && startingLine <= cloneFragments[i].end_line)
                                        {
                                            changed = true;
                                        }
                                    }
                                    if (!changed)
                                    {
                                        continue;
                                    }
                                    clones_modified_users cmu = new clones_modified_users();
                                    cmu.analyzer_id          = lastAnalysisId;
                                    cmu.file_name            = whf.fileName;
                                    cmu.modified_line_number = Convert.ToInt32(cloneFragments[i].start_line);
                                    cmu.modification_id      = Guid.NewGuid().ToString();
                                    cmu.source_id            = source_id;
                                    cmu.status    = whf.Status;
                                    cmu.timestamp = wh.entryTime;
                                    cmu.user_name = _cycloneEntities.webhook_commits.Where(a => a.webhook_ref_id == wh.webHookRefId).OrderBy(a => a.icommit_id).FirstOrDefault().committer_name;

                                    _cycloneEntities.clones_modified_users.Add(cmu);
                                    _cycloneEntities.SaveChanges();

                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }