Exemplo n.º 1
0
 private Commit[] GetCommitsSortedByDate(Project project, BadSmellMinerDbContext dbContext)
 {
     return(dbContext.Commits.Where(q => q.ProjectId == project.Id)
            .AsNoTracking()
            .OrderBy(q => q.DateTime)
            .ToArray());
 }
Exemplo n.º 2
0
        public void Resolve()
        {
            using (var dbContext = new BadSmellMinerDbContext())
            {
                var refactorings = dbContext.Refactorings
                                   .Include(q => q.Project)
                                   .AsNoTracking()
                                   .ToArray();

                var firstGroup = refactorings.GroupBy(g => g.ProjectId)

                                 .Select(r => new
                {
                    ProjectName = r.First().Project.Name,
                    TestCount   = r
                                  .Where(rg => rg.SourceClassPath.ToLower().Contains("test") ||
                                         rg.TargetClassPath.ToLower().Contains("test") ||
                                         (!string.IsNullOrEmpty(rg.SourceOperatationName) && rg.SourceOperatationName.ToLower().Contains("test")) ||
                                         (!string.IsNullOrEmpty(rg.TargetOperatationName) && rg.TargetOperatationName.ToLower().Contains("test")))
                                  .Count(),
                    Total = r.Count()
                });

                foreach (var g in firstGroup)
                {
                    Console.WriteLine($"{g.ProjectName},{g.Total},{g.TestCount},{g.Total - g.TestCount}");
                }
            }
        }
Exemplo n.º 3
0
 private object GetMethodsOfCommit(Commit commit, BadSmellMinerDbContext dbContext)
 {
     return(dbContext.OrganicMethods.Where(q => q.OrganicClass.CommitId == commit.Id)
            .Include(m => m.Smells)
            .AsNoTracking()
            .ToArray());
 }
Exemplo n.º 4
0
        private void AnalyzeProject(Project project, BadSmellMinerDbContext dbContext)
        {
            var commits = GetCommitsSortedByDate(project, dbContext);

            Console.WriteLine($"project: {project.Name}, Number of Commits: {commits.Length}");
            var smellHistoryHolder = new SmellHistoryHolder();

            foreach (var commit in commits)
            {
                Console.WriteLine($"{DateTime.Now} - Date Of Commit : {commit.DateTime}");

                Console.WriteLine($"{DateTime.Now} - Load Classes");
                var organicClasses = GetClassesOfCommit(commit, dbContext);

                Console.WriteLine($"{DateTime.Now} - Update Smell Statuses");
                foreach (var organicClass in organicClasses)
                {
                    smellHistoryHolder.AddSmells(commit, organicClass);
                }

                Console.WriteLine($"{DateTime.Now} - ResolveSmells");
                smellHistoryHolder.ResolveUnseenSmells(commit);

                Console.WriteLine(smellHistoryHolder.SmellsCount);
                Console.WriteLine(smellHistoryHolder.ResolvedSmellsCount);
            }
        }
Exemplo n.º 5
0
 private OrganicClass[] GetClassesOfCommit(Commit commit, BadSmellMinerDbContext dbContext)
 {
     return(dbContext.OrganicClasses.Where(q => q.CommitId == commit.Id)
            .Include(m => m.Commit)
            .Include(m => m.Smells)
            .Include(m => m.Methods).ThenInclude(m => m.Smells)
            .AsNoTracking()
            .ToArray());
 }
Exemplo n.º 6
0
        public void Resolve()
        {
            using (var dbContext = new BadSmellMinerDbContext())
            {
                var projects = dbContext.Projects.ToArray();

                foreach (var project in projects)
                {
                    AnalyzeProject(project, dbContext);
                }
            }
        }
Exemplo n.º 7
0
        private static Project CreateProject(string projectName, BadSmellMinerDbContext context)
        {
            var project = context.Projects.FirstOrDefault(q => q.Name == projectName);

            if (project == null)
            {
                project = new Project(projectName);
                context.Projects.Add(project);
                context.SaveChanges();
            }

            return(project);
        }
Exemplo n.º 8
0
        public void Resolve()
        {
            using (var dbContext = new BadSmellMinerDbContext())
            {
                var refactorings = dbContext.Refactorings.ToArray();
                var commits      = dbContext.Commits
                                   .Include(m => m.Project)
                                   .ToDictionary(q => q.CommitId);

                /* var methods = dbContext.OrganicMethods
                 * .AsNoTracking()
                 * .ToArray();*/

                var refactoringDictionary = new Dictionary <long, Dictionary <string, int> >();

                foreach (var refactoring in refactorings)
                {
                    if (!refactoringDictionary.ContainsKey(refactoring.ProjectId))
                    {
                        refactoringDictionary[refactoring.ProjectId] = new Dictionary <string, int>();
                    }

                    if (!refactoringDictionary[refactoring.ProjectId].ContainsKey(refactoring.CommitId))
                    {
                        refactoringDictionary[refactoring.ProjectId][refactoring.CommitId] = 0;
                    }

                    refactoringDictionary[refactoring.ProjectId][refactoring.CommitId]++;
                }


                var lines = new List <string>();

                foreach (var projectId in refactoringDictionary.Keys)
                {
                    foreach (var commitSha in refactoringDictionary[projectId].Keys)
                    {
                        //if(!commits.ContainsKey(commitSha))
                        //continue;

                        //var commit = commits[commitSha];
                        lines.Add($"{projectId},{commitSha},{refactoringDictionary[projectId][commitSha]}");
                    }
                }

                File.WriteAllLines(@"RQs\RQ3\results2.txt", lines);
            }
        }
Exemplo n.º 9
0
        private static void SaveRefactorings(string projectName, Refactoring[] refactorings)
        {
            using (var context = new BadSmellMinerDbContext())
            {
                var project = CreateProject(projectName, context);

                context.Refactorings.AddRange(refactorings);

                foreach (var refactoring in refactorings)
                {
                    refactoring.ProjectId = project.Id;
                }

                context.SaveChanges();
            }
        }
Exemplo n.º 10
0
        private static void HandleCheckout(LibGit2Sharp.Commit checkoutCommit, string projectName, string projectPath, string branchName)
        {
            using (var context = new BadSmellMinerDbContext())
            {
                if (context.Commits.Any(q => q.CommitId == checkoutCommit.Sha))
                {
                    return;
                }

                var organicAnalysisFilePath = RunOrganic(checkoutCommit.Sha, projectName, projectPath);

                var project = CreateProject(projectName, context);

                var organicClasses = ExtractOrganicClasses(organicAnalysisFilePath);

                CreateNewCommit(project, organicClasses, branchName, checkoutCommit, context);
            }
        }
Exemplo n.º 11
0
        public void ResolveUnseenSmells(Commit commit)
        {
            using (var dbContext = new BadSmellMinerDbContext())
            {
                foreach(var key in _smellHolder.Keys)
                {
                    var unseenSmells=_smellHolder[key]
                    .Where(q=>!q.HaveBeenSeenInTheLastCommit && q.BirthDate!=commit.DateTime);

                    foreach(var unseenSmell in unseenSmells)
                    {
                        counterOfSmellsThatDisappeared++;
                        unseenSmell.Resolved=true;
                        unseenSmell.DemiseDate=commit.DateTime;

                        //ADD NIC: I could get the refactorings for each smell, in here:
                        var keyCleaned = cleanKey(key);
                        Refactoring[] refactoringsForSmell = null;
                        try
                        {
                            refactoringsForSmell = dbContext.Refactorings.Where(
                                    q => q.CommitId == commit.CommitId
                                            && ( true // just get whatever refactoring was done at that commit for now. Forget about matching the exact file, it doesn't work I don't know why.
                                                // string.IsNullOrEmpty(q.SourceClassName) ? false : q.SourceClassName.Split(".", StringSplitOptions.None).Last() == keyCleaned
                                                // || string.IsNullOrEmpty(q.TargetClassName) ? false : q.TargetClassName.Split(".", StringSplitOptions.None).Last() == keyCleaned
                                                // || string.IsNullOrEmpty(q.SourceOperatationName) ? false : q.SourceOperatationName.Split(".", StringSplitOptions.None).Last() == keyCleaned
                                                // || string.IsNullOrEmpty(q.TargetOperatationName) ? false : q.TargetOperatationName.Split(".", StringSplitOptions.None).Last() == keyCleaned
                                                )
                                        )
                                .AsNoTracking()
                                .ToArray();

                        }
                        catch (Exception e)
                        {
                            //Console.WriteLine("commit.Id ==> "+commit.Id);
                        }
                        // output each of them for retrofit so I can verify against my own. (!!!!)
                        if(refactoringsForSmell != null && refactoringsForSmell.Count() > 0) 
                        {
                            foreach(Refactoring refactoring in refactoringsForSmell)
                            {
                                // Console.WriteLine("*************************************************************************************");
                                // Console.WriteLine("refactoring.ProjectId ==> "+refactoring.ProjectId);
                                // Console.WriteLine("refactoring.CommitId ==> "+refactoring.CommitId);
                                // Console.WriteLine("refactoring.Id) ==> "+refactoring.Id);
                                // Console.WriteLine("refactoring.SourceClassName ==> "+refactoring.SourceClassName);
                                // Console.WriteLine("refactoring.SourceOperatationName ==> "+refactoring.SourceOperatationName);
                                // Console.WriteLine("refactoring.TargetClassName ==> "+refactoring.TargetClassName);
                                // Console.WriteLine("refactoring.TargetOperatationName ==> "+refactoring.TargetOperatationName);
                                // Console.WriteLine("refactoring.Type ==> "+refactoring.Type);
                                // Console.WriteLine("--------------------------------------------------------------------------------------");
                                // Console.WriteLine("unseenSmell.SmellName ==> "+unseenSmell.SmellName);
                                // Console.WriteLine("smell key (which is the method or class) ==> "+key);
                                // Console.WriteLine("unseenSmell.Resolved ==> "+unseenSmell.Resolved);
                                // Console.WriteLine("unseenSmell.BirthDate ==> "+unseenSmell.BirthDate);
                                // Console.WriteLine("unseenSmell.DemiseDate ==> "+unseenSmell.DemiseDate);
                                // write csv:
                                using (StreamWriter sw = File.AppendText("smellsThatDisappearedWithCounterAndLocationAndDateOfBirthAndDemiseAndCorrespondingRefactoringTypeAndIdAndCommitIdIfAvailable.csv"))
                                {
                                    sw.WriteLine(
                                        refactoring.ProjectId + ", " +
                                        unseenSmell.CommitId + ", " +
                                        counterOfSmellsThatDisappeared + ", " + 
                                        unseenSmell.SmellName + " , " + 
                                        keyDoesStartWithMethodMarker(key) + " , " + 
                                        keyDoesStartWithClassMarker(key) + " , " + 
                                        cleanKeyToRemoveIsMethodOrIsClassAtStart(key) + ", " + 
                                        unseenSmell.BirthDate + ", " + 
                                        unseenSmell.DemiseDate + ", " + 
                                        refactoring.Type + ", " + 
                                        refactoring.Id + ", "
                                        );
                                }

                            }
                        }
                        else 
                        {
                            // write csv:
                            using (StreamWriter sw = File.AppendText("smellsThatDisappearedWithCounterAndLocationAndDateOfBirthAndDemiseAndCorrespondingRefactoringTypeAndIdAndCommitIdIfAvailable.csv"))
                            {
                                sw.WriteLine(
                                    unseenSmell.ProjectId + ", " +
                                    unseenSmell.CommitId + ", " +
                                    counterOfSmellsThatDisappeared + ", " + 
                                    unseenSmell.SmellName + " , " + 
                                    keyDoesStartWithMethodMarker(key) + " , " + 
                                    keyDoesStartWithClassMarker(key) + " , " + 
                                    cleanKeyToRemoveIsMethodOrIsClassAtStart(key) + ", " + 
                                    unseenSmell.BirthDate + ", " + 
                                    unseenSmell.DemiseDate
                                    );
                            }
                        }
                        //Console.WriteLine();
                        //
                    }
                }
            }

        }
Exemplo n.º 12
0
        private static void CreateNewCommit(Project project, OrganicClass[] organicClasses, string branchName, LibGit2Sharp.Commit checkoutCommit, BadSmellMinerDbContext context)
        {
            var commit = new DomainModels.Commit(project.Id, checkoutCommit.Sha, organicClasses);

            commit.AuthorName   = checkoutCommit.Committer.Email;
            commit.FullMessage  = checkoutCommit.Message;
            commit.ShortMessage = checkoutCommit.MessageShort;
            commit.DateTime     = checkoutCommit.Committer.When.DateTime;
            commit.BranchName   = branchName;

            context.Commits.Add(commit);

            context.SaveChanges();
        }