blob: 3a34f6593f786a99d126d3f7b372d2c955a21b4d [file] [log] [blame]
Ronald Bhuleskar999a71d2021-11-19 15:24:51 -08001// Copyright 2021 Google LLC. All Rights Reserved.
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
15package com.google.gitiles;
16
17import static com.google.gitiles.FormatType.HTML;
18
19import java.util.Optional;
20import javax.servlet.http.HttpServletRequest;
21import org.eclipse.jgit.lib.Repository;
22
23/**
24 * Utility that provides information to replace the URL string that contains a branch name to a new
Sven Selbergdff134a2022-01-10 14:36:15 +010025 * branch name. The updated branch mapping is provided by {@code BranchRedirect#getRedirectBranch}
26 * method. If it should update the branch then it is the caller's responsibility to update the URL
27 * with updated branch name as redirect.
Ronald Bhuleskar999a71d2021-11-19 15:24:51 -080028 *
29 * <p>This implementation does not provide a branch redirect mapping. Hence, including this as-is
30 * would be a no-op. To make this effective {@code BranchRedirect#getRedirectBranch} needs to be
31 * overridden that provides a mapping to the requested repo/branch.
32 */
33public class BranchRedirect {
34
35 static final BranchRedirect EMPTY = new BranchRedirect();
36
37 /**
38 * Provides an extendable interface that can be used to provide implementation for determining
39 * redirect branch
40 *
41 * @param repo Repository
42 * @param sourceBranch full branch name eg. refs/heads/master
43 * @return Returns the branch that should be redirected to on a given repo. {@code
44 * Optional.empty()} means no redirect.
45 */
46 protected Optional<String> getRedirectBranch(Repository repo, String sourceBranch) {
47 return Optional.empty();
48 }
49
50 static boolean isForAutomation(HttpServletRequest req) {
51 FormatType formatType = FormatType.getFormatType(req).orElse(HTML);
52 switch (formatType) {
53 case HTML:
54 case DEFAULT:
55 return false;
56 case JSON:
57 case TEXT:
58 default:
59 return true;
60 }
61 }
62}