| 1 |
// Copyright 2019 The Hugo Authors. 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 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hugolib |
| 15 |
|
| 16 |
import ( |
| 17 |
"io" |
| 18 |
"path" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/common/herrors" |
| 21 |
"github.com/gohugoio/hugo/resources/page" |
| 22 |
"github.com/hairyhenderson/go-codeowners" |
| 23 |
"github.com/spf13/afero" |
| 24 |
) |
| 25 |
|
| 26 |
var afs = afero.NewOsFs() |
| 27 |
|
| 28 |
func findCodeOwnersFile(dir string) (io.Reader, error) { |
| 29 |
for _, p := range []string{".", "docs", ".github", ".gitlab"} { |
| 30 |
f := path.Join(dir, p, "CODEOWNERS") |
| 31 |
|
| 32 |
_, err := afs.Stat(f) |
| 33 |
if err != nil { |
| 34 |
if herrors.IsNotExist(err) { |
| 35 |
continue |
| 36 |
} |
| 37 |
return nil, err |
| 38 |
} |
| 39 |
|
| 40 |
return afs.Open(f) |
| 41 |
} |
| 42 |
|
| 43 |
return nil, nil |
| 44 |
} |
| 45 |
|
| 46 |
type codeownerInfo struct { |
| 47 |
owners *codeowners.Codeowners |
| 48 |
} |
| 49 |
|
| 50 |
func (c *codeownerInfo) forPage(p page.Page) []string { |
| 51 |
return c.owners.Owners(p.File().Filename()) |
| 52 |
} |
| 53 |
|
| 54 |
func newCodeOwners(workingDir string) (*codeownerInfo, error) { |
| 55 |
r, err := findCodeOwnersFile(workingDir) |
| 56 |
if err != nil || r == nil { |
| 57 |
return nil, err |
| 58 |
} |
| 59 |
|
| 60 |
owners, err := codeowners.FromReader(r, workingDir) |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
|
| 65 |
return &codeownerInfo{owners: owners}, nil |
| 66 |
} |