sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/gerrit/source/source.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package source contains functions that help with Gerrit source control
    18  // specific logics.
    19  package source
    20  
    21  import (
    22  	"fmt"
    23  	"strings"
    24  )
    25  
    26  // IsGerritOrg tells whether the org is a Gerrit org or not. It returns true
    27  // when the org string starts with https:// or http://.
    28  func IsGerritOrg(org string) bool {
    29  	return strings.HasPrefix(org, "https://") || strings.HasPrefix(org, "http://")
    30  }
    31  
    32  // CloneURIFromOrgRepo returns normalized cloneURI from org and repo. The
    33  // returns cloneURI will always have https:// or http:// prefix, and there is no
    34  // trailing slash at the end.
    35  func CloneURIFromOrgRepo(org, repo string) string {
    36  	return NormalizeCloneURI(orgRepo(org, repo))
    37  }
    38  
    39  // NormalizeOrg returns normalized org. It ensures that org always has https://
    40  // or http:// prefix, and there is no trailing slash at the end. This function
    41  // should be used everywhere that Gerrit org is referenced.
    42  func NormalizeOrg(org string) string {
    43  	return strings.TrimRight(ensuresHTTPSPrefix(org), "/")
    44  }
    45  
    46  // NormalizeCloneURI returns normalized cloneURI. It ensures that cloneURI
    47  // always has https:// or http:// prefix, and there is no trailing slash at the
    48  // end. This function should be used everywhere that Gerrit cloneURI is
    49  // referenced.
    50  func NormalizeCloneURI(cloneURI string) string {
    51  	return strings.TrimRight(ensuresHTTPSPrefix(cloneURI), "/")
    52  }
    53  
    54  // OrgRepoFromCloneURI returns org and repo from cloneURI. The returned org
    55  // always has https:// or http:// prefix even if cloneURI doesn't have it.
    56  func OrgRepoFromCloneURI(cloneURI string) (string, string, error) {
    57  	scheme := "https://"
    58  	if strings.HasPrefix(cloneURI, "http://") {
    59  		scheme = "http://"
    60  	}
    61  	cloneURIWithoutPrefix := TrimHTTPSPrefix(cloneURI)
    62  	var org, repo string
    63  	parts := strings.SplitN(cloneURIWithoutPrefix, "/", 2)
    64  	if len(parts) != 2 {
    65  		return org, repo, fmt.Errorf("should have 2 parts: %v", parts)
    66  	}
    67  	return NormalizeOrg(scheme + parts[0]), strings.TrimRight(parts[1], "/"), nil
    68  }
    69  
    70  func ensuresHTTPSPrefix(in string) string {
    71  	scheme := "https://"
    72  	if strings.HasPrefix(in, "http://") {
    73  		scheme = "http://"
    74  	}
    75  	return fmt.Sprintf("%s%s", scheme, strings.Trim(TrimHTTPSPrefix(in), "/"))
    76  }
    77  
    78  // TrimHTTPSPrefix trims https:// and http:// from input, also remvoes all
    79  // trailing slashes from the end.
    80  func TrimHTTPSPrefix(in string) string {
    81  	in = strings.TrimPrefix(in, "https://")
    82  	in = strings.TrimPrefix(in, "http://")
    83  	return strings.TrimRight(in, "/")
    84  }
    85  
    86  // orgRepo returns <org>/<repo>, removes all extra slashs.
    87  func orgRepo(org, repo string) string {
    88  	org = strings.Trim(org, "/")
    89  	repo = strings.Trim(repo, "/")
    90  	return org + "/" + repo
    91  }
    92  
    93  const reviewSuffix = "-review"
    94  
    95  // CodeURL converts code review URL into source code URL, simply
    96  // trimming the `-review` suffix from the name of the org.
    97  //
    98  // Gerrit URL for sourcecode looks like
    99  // https://android.googlesource.com, and the code review URL looks like
   100  // https://android-review.googlesource.com/c/platform/frameworks/support/+/2260382.
   101  func CodeURL(reviewURL string) (string, error) {
   102  	ps := strings.SplitN(reviewURL, ".", 2)
   103  	if len(ps) == 1 || !strings.HasSuffix(ps[0], reviewSuffix) {
   104  		return "", fmt.Errorf("cannot find %q suffix from the first part of url %v", reviewSuffix, ps)
   105  	}
   106  	ps[0] = strings.TrimSuffix(ps[0], reviewSuffix)
   107  	return strings.Join(ps, "."), nil
   108  }
   109  
   110  // EnsureCodeURL works as CodeRootURL, without errors.
   111  func EnsureCodeURL(url string) string {
   112  	if u, err := CodeURL(url); err == nil {
   113  		return u
   114  	}
   115  	return url
   116  }