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.

BranchDto.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.db.component;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.util.Objects;
  25. import javax.annotation.CheckForNull;
  26. import javax.annotation.Nullable;
  27. import org.sonar.db.protobuf.DbProjectBranches;
  28. import static com.google.common.base.Preconditions.checkArgument;
  29. public class BranchDto {
  30. public static final String DEFAULT_MAIN_BRANCH_NAME = "master";
  31. /**
  32. * Maximum length of column "kee"
  33. */
  34. public static final int KEE_MAX_LENGTH = 255;
  35. /**
  36. * Branch UUID is the projects.uuid that reference projects, branches or pull requests
  37. * (projects.qualifier="TRK").
  38. * Not null.
  39. * Important - the table project_branches does NOT have its own UUIDs for the time being.
  40. * All values must exist in projects.uuid.
  41. */
  42. private String uuid;
  43. /**
  44. * UUID of the project that represents the main branch.
  45. * On main branches, projectUuid equals uuid.
  46. * Not null.
  47. */
  48. private String projectUuid;
  49. /**
  50. * Key that identifies a branch or a pull request.
  51. * For keyType=BRANCH, this is the name of the branch, for example "feature/foo".
  52. * For keyType=PULL_REQUEST, this is the ID of the pull request in some external system, for example 123 in GitHub.
  53. */
  54. private String kee;
  55. /**
  56. * Key type, as provided by {@link KeyType}.
  57. * Not null.
  58. */
  59. private KeyType keyType;
  60. /**
  61. * Branch type, as provided by {@link BranchType}.
  62. * Not null.
  63. */
  64. private BranchType branchType;
  65. /**
  66. * UUID of the branch:
  67. * - in which the short-lived branch or pull request will be merged into
  68. * - that is the base of long-lived branch.
  69. *
  70. * Can be null if information is not known.
  71. */
  72. @Nullable
  73. private String mergeBranchUuid;
  74. /**
  75. * Pull Request data, such as branch name, title, url, and provider specific attributes
  76. */
  77. @Nullable
  78. private byte[] pullRequestBinary;
  79. public String getUuid() {
  80. return uuid;
  81. }
  82. public BranchDto setUuid(String s) {
  83. this.uuid = s;
  84. return this;
  85. }
  86. public String getProjectUuid() {
  87. return projectUuid;
  88. }
  89. public BranchDto setProjectUuid(String s) {
  90. this.projectUuid = s;
  91. return this;
  92. }
  93. public boolean isMain() {
  94. return projectUuid.equals(uuid);
  95. }
  96. /**
  97. * This is the getter used by MyBatis mapper.
  98. */
  99. private String getKee() {
  100. return kee;
  101. }
  102. public String getKey() {
  103. return kee;
  104. }
  105. /**
  106. * This is the setter used by MyBatis mapper.
  107. */
  108. private void setKee(String s) {
  109. this.kee = s;
  110. }
  111. public BranchDto setKey(String s) {
  112. checkArgument(s.length() <= KEE_MAX_LENGTH, "Maximum length of branch name or pull request id is %s: %s", KEE_MAX_LENGTH, s);
  113. setKee(s);
  114. return this;
  115. }
  116. BranchDto setKeyType(@Nullable KeyType keyType) {
  117. this.keyType = keyType;
  118. return this;
  119. }
  120. public BranchType getBranchType() {
  121. return branchType;
  122. }
  123. public BranchDto setBranchType(@Nullable BranchType b) {
  124. this.branchType = b;
  125. return this;
  126. }
  127. @Nullable
  128. public String getMergeBranchUuid() {
  129. return mergeBranchUuid;
  130. }
  131. public BranchDto setMergeBranchUuid(@Nullable String s) {
  132. this.mergeBranchUuid = s;
  133. return this;
  134. }
  135. public BranchDto setPullRequestData(DbProjectBranches.PullRequestData pullRequestData) {
  136. this.pullRequestBinary = encodePullRequestData(pullRequestData);
  137. return this;
  138. }
  139. @CheckForNull
  140. public DbProjectBranches.PullRequestData getPullRequestData() {
  141. if (pullRequestBinary == null) {
  142. return null;
  143. }
  144. return decodePullRequestData(pullRequestBinary);
  145. }
  146. private static byte[] encodePullRequestData(DbProjectBranches.PullRequestData pullRequestData) {
  147. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  148. try {
  149. pullRequestData.writeTo(outputStream);
  150. return outputStream.toByteArray();
  151. } catch (IOException e) {
  152. throw new IllegalStateException("Fail to serialize pull request data", e);
  153. }
  154. }
  155. private static DbProjectBranches.PullRequestData decodePullRequestData(byte[] pullRequestBinary) {
  156. try (ByteArrayInputStream inputStream = new ByteArrayInputStream(pullRequestBinary)) {
  157. return DbProjectBranches.PullRequestData.parseFrom(inputStream);
  158. } catch (IOException e) {
  159. throw new IllegalStateException("Fail to deserialize pull request data", e);
  160. }
  161. }
  162. @Override
  163. public boolean equals(Object o) {
  164. if (this == o) {
  165. return true;
  166. }
  167. if (o == null || getClass() != o.getClass()) {
  168. return false;
  169. }
  170. BranchDto branchDto = (BranchDto) o;
  171. return Objects.equals(uuid, branchDto.uuid) &&
  172. Objects.equals(projectUuid, branchDto.projectUuid) &&
  173. Objects.equals(kee, branchDto.kee) &&
  174. branchType == branchDto.branchType &&
  175. Objects.equals(mergeBranchUuid, branchDto.mergeBranchUuid);
  176. }
  177. @Override
  178. public int hashCode() {
  179. return Objects.hash(uuid, projectUuid, kee, branchType, mergeBranchUuid);
  180. }
  181. @Override
  182. public String toString() {
  183. StringBuilder sb = new StringBuilder("BranchDto{");
  184. sb.append("uuid='").append(uuid).append('\'');
  185. sb.append(", projectUuid='").append(projectUuid).append('\'');
  186. sb.append(", kee='").append(kee).append('\'');
  187. sb.append(", keyType=").append(keyType);
  188. sb.append(", branchType=").append(branchType);
  189. sb.append(", mergeBranchUuid='").append(mergeBranchUuid).append('\'');
  190. sb.append('}');
  191. return sb.toString();
  192. }
  193. }