Line data Source code
1 : // Copyright (C) 2014 The Android Open Source Project
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : package com.google.gerrit.server;
16 :
17 : import static com.google.common.collect.ImmutableList.toImmutableList;
18 :
19 : import com.google.common.base.Strings;
20 : import com.google.common.collect.ImmutableList;
21 : import com.google.common.collect.Streams;
22 : import com.google.common.flogger.FluentLogger;
23 : import com.google.gerrit.entities.Patch;
24 : import com.google.gerrit.entities.Project;
25 : import com.google.gerrit.extensions.common.DiffWebLinkInfo;
26 : import com.google.gerrit.extensions.common.WebLinkInfo;
27 : import com.google.gerrit.extensions.registration.DynamicSet;
28 : import com.google.gerrit.extensions.webui.BranchWebLink;
29 : import com.google.gerrit.extensions.webui.DiffWebLink;
30 : import com.google.gerrit.extensions.webui.EditWebLink;
31 : import com.google.gerrit.extensions.webui.FileHistoryWebLink;
32 : import com.google.gerrit.extensions.webui.FileWebLink;
33 : import com.google.gerrit.extensions.webui.ParentWebLink;
34 : import com.google.gerrit.extensions.webui.PatchSetWebLink;
35 : import com.google.gerrit.extensions.webui.ProjectWebLink;
36 : import com.google.gerrit.extensions.webui.ResolveConflictsWebLink;
37 : import com.google.gerrit.extensions.webui.TagWebLink;
38 : import com.google.gerrit.extensions.webui.WebLink;
39 : import com.google.inject.Inject;
40 : import com.google.inject.Singleton;
41 : import java.util.function.Function;
42 :
43 : @Singleton
44 : public class WebLinks {
45 150 : private static final FluentLogger logger = FluentLogger.forEnclosingClass();
46 :
47 : private final DynamicSet<PatchSetWebLink> patchSetLinks;
48 : private final DynamicSet<ResolveConflictsWebLink> resolveConflictsLinks;
49 : private final DynamicSet<ParentWebLink> parentLinks;
50 : private final DynamicSet<EditWebLink> editLinks;
51 : private final DynamicSet<FileWebLink> fileLinks;
52 : private final DynamicSet<FileHistoryWebLink> fileHistoryLinks;
53 : private final DynamicSet<DiffWebLink> diffLinks;
54 : private final DynamicSet<ProjectWebLink> projectLinks;
55 : private final DynamicSet<BranchWebLink> branchLinks;
56 : private final DynamicSet<TagWebLink> tagLinks;
57 :
58 : @Inject
59 : public WebLinks(
60 : DynamicSet<PatchSetWebLink> patchSetLinks,
61 : DynamicSet<ResolveConflictsWebLink> resolveConflictsLinks,
62 : DynamicSet<ParentWebLink> parentLinks,
63 : DynamicSet<EditWebLink> editLinks,
64 : DynamicSet<FileWebLink> fileLinks,
65 : DynamicSet<FileHistoryWebLink> fileLogLinks,
66 : DynamicSet<DiffWebLink> diffLinks,
67 : DynamicSet<ProjectWebLink> projectLinks,
68 : DynamicSet<BranchWebLink> branchLinks,
69 150 : DynamicSet<TagWebLink> tagLinks) {
70 150 : this.patchSetLinks = patchSetLinks;
71 150 : this.resolveConflictsLinks = resolveConflictsLinks;
72 150 : this.parentLinks = parentLinks;
73 150 : this.editLinks = editLinks;
74 150 : this.fileLinks = fileLinks;
75 150 : this.fileHistoryLinks = fileLogLinks;
76 150 : this.diffLinks = diffLinks;
77 150 : this.projectLinks = projectLinks;
78 150 : this.branchLinks = branchLinks;
79 150 : this.tagLinks = tagLinks;
80 150 : }
81 :
82 : /**
83 : * Returns links for patch sets
84 : *
85 : * @param project Project name.
86 : * @param commit SHA1 of commit.
87 : * @param commitMessage the commit message of the commit.
88 : * @param branchName branch of the commit.
89 : * @param changeKey change Identifier for this change
90 : */
91 : public ImmutableList<WebLinkInfo> getPatchSetLinks(
92 : Project.NameKey project,
93 : String commit,
94 : String commitMessage,
95 : String branchName,
96 : String changeKey) {
97 103 : return filterLinks(
98 : patchSetLinks,
99 : webLink ->
100 1 : webLink.getPatchSetWebLink(
101 1 : project.get(), commit, commitMessage, branchName, changeKey));
102 : }
103 :
104 : /**
105 : * Returns links for resolving conflicts
106 : *
107 : * @param project Project name.
108 : * @param commit SHA1 of commit.
109 : * @param commitMessage the commit message of the commit.
110 : * @param branchName branch of the commit.
111 : */
112 : public ImmutableList<WebLinkInfo> getResolveConflictsLinks(
113 : Project.NameKey project, String commit, String commitMessage, String branchName) {
114 103 : return filterLinks(
115 : resolveConflictsLinks,
116 : webLink ->
117 1 : webLink.getResolveConflictsWebLink(project.get(), commit, commitMessage, branchName));
118 : }
119 :
120 : /**
121 : * Returns links for patch sets
122 : *
123 : * @param project Project name.
124 : * @param revision SHA1 of the parent revision.
125 : * @param commitMessage the commit message of the parent revision.
126 : * @param branchName branch of the revision (and parent revision).
127 : */
128 : public ImmutableList<WebLinkInfo> getParentLinks(
129 : Project.NameKey project, String revision, String commitMessage, String branchName) {
130 98 : return filterLinks(
131 : parentLinks,
132 0 : webLink -> webLink.getParentWebLink(project.get(), revision, commitMessage, branchName));
133 : }
134 :
135 : /**
136 : * Returns links for editing
137 : *
138 : * @param project Project name.
139 : * @param revision SHA1 of revision.
140 : * @param file File name.
141 : */
142 : public ImmutableList<WebLinkInfo> getEditLinks(String project, String revision, String file) {
143 8 : return Patch.isMagic(file)
144 3 : ? ImmutableList.of()
145 7 : : filterLinks(editLinks, webLink -> webLink.getEditWebLink(project, revision, file));
146 : }
147 :
148 : /**
149 : * Returns links for files
150 : *
151 : * @param project Project name.
152 : * @param revision Name of the revision (e.g. branch or commit ID)
153 : * @param hash SHA1 of revision.
154 : * @param file File name.
155 : */
156 : public ImmutableList<WebLinkInfo> getFileLinks(
157 : String project, String revision, String hash, String file) {
158 8 : return Patch.isMagic(file)
159 3 : ? ImmutableList.of()
160 7 : : filterLinks(fileLinks, webLink -> webLink.getFileWebLink(project, revision, hash, file));
161 : }
162 :
163 : /**
164 : * Returns links for file history
165 : *
166 : * @param project Project name.
167 : * @param revision SHA1 of revision.
168 : * @param file File name.
169 : */
170 : public ImmutableList<WebLinkInfo> getFileHistoryLinks(
171 : String project, String revision, String file) {
172 9 : if (Patch.isMagic(file)) {
173 0 : return ImmutableList.of();
174 : }
175 9 : return Streams.stream(fileHistoryLinks)
176 9 : .map(webLink -> webLink.getFileHistoryWebLink(project, revision, file))
177 9 : .filter(WebLinks::isValid)
178 9 : .collect(toImmutableList());
179 : }
180 :
181 : /**
182 : * Returns links for file diffs
183 : *
184 : * @param project Project name.
185 : * @param patchSetIdA Patch set ID of side A, <code>null</code> if no base patch set was selected.
186 : * @param revisionA SHA1 of revision of side A.
187 : * @param fileA File name of side A.
188 : * @param patchSetIdB Patch set ID of side B.
189 : * @param revisionB SHA1 of revision of side B.
190 : * @param fileB File name of side B.
191 : */
192 : public ImmutableList<DiffWebLinkInfo> getDiffLinks(
193 : String project,
194 : int changeId,
195 : Integer patchSetIdA,
196 : String revisionA,
197 : String fileA,
198 : int patchSetIdB,
199 : String revisionB,
200 : String fileB) {
201 8 : if (Patch.isMagic(fileA) || Patch.isMagic(fileB)) {
202 3 : return ImmutableList.of();
203 : }
204 7 : return Streams.stream(diffLinks)
205 7 : .map(
206 : webLink ->
207 0 : webLink.getDiffLink(
208 : project,
209 : changeId,
210 : patchSetIdA,
211 : revisionA,
212 : fileA,
213 : patchSetIdB,
214 : revisionB,
215 : fileB))
216 7 : .filter(WebLinks::isValid)
217 7 : .collect(toImmutableList());
218 : }
219 :
220 : /**
221 : * Returns links for projects
222 : *
223 : * @param project Project name.
224 : */
225 : public ImmutableList<WebLinkInfo> getProjectLinks(String project) {
226 143 : return filterLinks(projectLinks, webLink -> webLink.getProjectWeblink(project));
227 : }
228 :
229 : /**
230 : * Returns links for branches
231 : *
232 : * @param project Project name
233 : * @param branch Branch name
234 : */
235 : public ImmutableList<WebLinkInfo> getBranchLinks(String project, String branch) {
236 15 : return filterLinks(branchLinks, webLink -> webLink.getBranchWebLink(project, branch));
237 : }
238 :
239 : /**
240 : * Returns links for the tag
241 : *
242 : * @param project Project name
243 : * @param tag Tag name
244 : */
245 : public ImmutableList<WebLinkInfo> getTagLinks(String project, String tag) {
246 7 : return filterLinks(tagLinks, webLink -> webLink.getTagWebLink(project, tag));
247 : }
248 :
249 : private <T extends WebLink> ImmutableList<WebLinkInfo> filterLinks(
250 : DynamicSet<T> links, Function<T, WebLinkInfo> transformer) {
251 145 : return Streams.stream(links)
252 145 : .map(transformer)
253 145 : .filter(WebLinks::isValid)
254 145 : .collect(toImmutableList());
255 : }
256 :
257 : private static boolean isValid(WebLinkInfo link) {
258 4 : if (link == null) {
259 0 : return false;
260 4 : } else if (Strings.isNullOrEmpty(link.name) || Strings.isNullOrEmpty(link.url)) {
261 0 : logger.atWarning().log("%s is missing name and/or url", link.getClass().getName());
262 0 : return false;
263 : }
264 4 : return true;
265 : }
266 : }
|