You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScmAccountToUserLoader.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.task.projectanalysis.issue;
  21. import com.google.common.base.Joiner;
  22. import com.google.common.collect.Ordering;
  23. import java.util.Collection;
  24. import java.util.List;
  25. import java.util.Map;
  26. import org.sonar.api.utils.log.Logger;
  27. import org.sonar.api.utils.log.Loggers;
  28. import org.sonar.ce.task.projectanalysis.util.cache.CacheLoader;
  29. import org.sonar.core.util.stream.MoreCollectors;
  30. import org.sonar.server.user.index.UserDoc;
  31. import org.sonar.server.user.index.UserIndex;
  32. /**
  33. * Loads the association between a SCM account and a SQ user
  34. */
  35. public class ScmAccountToUserLoader implements CacheLoader<String, String> {
  36. private static final Logger LOGGER = Loggers.get(ScmAccountToUserLoader.class);
  37. private final UserIndex index;
  38. public ScmAccountToUserLoader(UserIndex index) {
  39. this.index = index;
  40. }
  41. @Override
  42. public String load(String scmAccount) {
  43. List<UserDoc> users = index.getAtMostThreeActiveUsersForScmAccount(scmAccount);
  44. if (users.size() == 1) {
  45. return users.get(0).uuid();
  46. }
  47. if (!users.isEmpty()) {
  48. // multiple users are associated to the same SCM account, for example
  49. // the same email
  50. Collection<String> logins = users.stream()
  51. .map(UserDoc::login)
  52. .sorted(Ordering.natural())
  53. .collect(MoreCollectors.toList(users.size()));
  54. LOGGER.warn(String.format("Multiple users share the SCM account '%s': %s", scmAccount, Joiner.on(", ").join(logins)));
  55. }
  56. return null;
  57. }
  58. @Override
  59. public Map<String, String> loadAll(Collection<? extends String> scmAccounts) {
  60. throw new UnsupportedOperationException("Loading by multiple scm accounts is not supported yet");
  61. }
  62. }