All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.server.computation.issue.IssueAssigner Maven / Gradle / Ivy

There is a newer version: 7.0
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.computation.issue;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import java.util.Date;
import javax.annotation.CheckForNull;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.issue.IssueChangeContext;
import org.sonar.server.computation.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.scm.ScmInfo;
import org.sonar.server.computation.scm.ScmInfoRepository;
import org.sonar.server.issue.IssueUpdater;

import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
import static org.sonar.core.issue.IssueChangeContext.createScan;

/**
 * Detect the SCM author and SQ assignee.
 * 

* It relies on: *

    *
  • SCM information sent in the report for modified files
  • *
  • sources lines stored in database for non-modified files
  • *
*/ public class IssueAssigner extends IssueVisitor { private static final Logger LOGGER = Loggers.get(IssueAssigner.class); private final ScmInfoRepository scmInfoRepository; private final DefaultAssignee defaultAssignee; private final IssueUpdater issueUpdater; private final ScmAccountToUser scmAccountToUser; private final IssueChangeContext changeContext; private String lastCommitAuthor = null; private ScmInfo scmChangesets = null; public IssueAssigner(AnalysisMetadataHolder analysisMetadataHolder, ScmInfoRepository scmInfoRepository, ScmAccountToUser scmAccountToUser, DefaultAssignee defaultAssignee, IssueUpdater issueUpdater) { this.scmInfoRepository = scmInfoRepository; this.scmAccountToUser = scmAccountToUser; this.defaultAssignee = defaultAssignee; this.issueUpdater = issueUpdater; this.changeContext = createScan(new Date(analysisMetadataHolder.getAnalysisDate())); } @Override public void onIssue(Component component, DefaultIssue issue) { boolean authorWasSet = false; if (issue.authorLogin() == null) { loadScmChangesets(component); String scmAuthor = guessScmAuthor(issue); if (!Strings.isNullOrEmpty(scmAuthor)) { issueUpdater.setNewAuthor(issue, scmAuthor, changeContext); authorWasSet = true; } } if (authorWasSet && issue.assignee() == null) { String assigneeLogin = StringUtils.defaultIfEmpty(scmAccountToUser.getNullable(issue.authorLogin()), defaultAssignee.getLogin()); issueUpdater.setNewAssignee(issue, assigneeLogin, changeContext); } } private void loadScmChangesets(Component component) { if (scmChangesets == null) { Optional scmInfoOptional = scmInfoRepository.getScmInfo(component); if (scmInfoOptional.isPresent()) { scmChangesets = scmInfoOptional.get(); lastCommitAuthor = scmChangesets.getLatestChangeset().getAuthor(); } } } @Override public void afterComponent(Component component) { lastCommitAuthor = null; scmChangesets = null; } /** * Get the SCM login of the last committer on the line. When line is zero, * then get the last committer on the file. */ @CheckForNull private String guessScmAuthor(DefaultIssue issue) { Integer line = issue.line(); String author = null; if (line != null && scmChangesets != null) { if (scmChangesets.hasChangesetForLine(line)) { author = scmChangesets.getChangesetForLine(line).getAuthor(); } else { LOGGER.warn("No SCM info has been found for issue {}", issue); } } return defaultIfEmpty(author, lastCommitAuthor); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy