github.com/googleapis/api-linter@v1.65.2/rules/aip0192/absolute_links.go (about) 1 // Copyright 2020 Google LLC 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 // https://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 aip0192 16 17 import ( 18 "regexp" 19 "strings" 20 21 "github.com/googleapis/api-linter/lint" 22 "github.com/googleapis/api-linter/locations" 23 "github.com/googleapis/api-linter/rules/internal/utils" 24 "github.com/jhump/protoreflect/desc" 25 ) 26 27 // absoluteLinks ensures that a descriptor has only absolute links. 28 var absoluteLinks = &lint.DescriptorRule{ 29 Name: lint.NewRuleName(192, "absolute-links"), 30 LintDescriptor: func(d desc.Descriptor) []lint.Problem { 31 comment := strings.Join( 32 utils.SeparateInternalComments(d.GetSourceInfo().GetLeadingComments()).External, 33 "\n", 34 ) 35 36 // Absolute links can appear in either free text or Markdown links, 37 // so we need to spot both, but we also do not want a ton of false 38 // positives. 39 // 40 // It seems infeasible to check free text, but we can check Markdown links. 41 for _, match := range mdLink.FindAllStringSubmatch(comment, -1) { 42 uri := match[1] 43 if !strings.Contains(uri, "://") { 44 return []lint.Problem{{ 45 Message: "Links in comments should be absolute and begin with https://", 46 Descriptor: d, 47 Location: locations.DescriptorName(d), 48 }} 49 } 50 } 51 52 return nil 53 }, 54 } 55 56 var mdLink = regexp.MustCompile(`\[.*?\]\(([\w\d:/.%_-]+)\)`)