-
Notifications
You must be signed in to change notification settings - Fork 533
tools: add metadata linting step to linter job #1087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
842a0f1
f012c1c
555d198
93ade7c
36e98de
355b9e2
38ea128
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| FROM fedora | ||
| WORKDIR /workdir | ||
| COPY install-markdownlint.sh /tmp | ||
| RUN dnf install -y git | ||
| RUN dnf install -y git golang | ||
| RUN /tmp/install-markdownlint.sh | ||
| ENTRYPOINT /workdir/hack/markdownlint.sh |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,5 @@ | |
| markdownlint-cli2 '**/*.md' | ||
|
|
||
| $(dirname $0)/template-lint.sh | ||
|
|
||
| $(dirname $0)/metadata-lint.sh | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -x | ||
|
|
||
| # Ensure the enhancement metadata includes the required information | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
| set -o pipefail | ||
|
|
||
| # We only look at the files that have changed in the current PR, to | ||
| # avoid problems when the template is changed in a way that is | ||
| # incompatible with existing documents. | ||
| CHANGED_FILES=$(git log --name-only --pretty= "${PULL_BASE_SHA:-origin/master}.." -- \ | ||
| | (grep '^enhancements.*\.md$' || true) \ | ||
| | sort -u) | ||
|
|
||
| (cd tools && go build -o ../enhancement-tool -mod=mod ./main.go) | ||
|
|
||
| ./enhancement-tool metadata-lint ${CHANGED_FILES} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| Copyright © 2022 Doug Hellmann <[email protected]> | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/openshift/enhancements/tools/enhancements" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // metadataLintCmd represents the metadataLint command | ||
| var metadataLintCmd = &cobra.Command{ | ||
| Use: "metadata-lint", | ||
| Short: "Check the metadata of the enhancement files given as arguments", | ||
| ValidArgs: []string{"enhancement-filename"}, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| errorCount := 0 | ||
| for _, filename := range args { | ||
| reportError := func(msg string) { | ||
| fmt.Fprintf(os.Stderr, "%s: %s\n", filename, msg) | ||
| errorCount++ | ||
| } | ||
| content, err := ioutil.ReadFile(filename) | ||
| if err != nil { | ||
| reportError(fmt.Sprintf("%s", err)) | ||
| continue | ||
| } | ||
| metadata, err := enhancements.NewMetaData(content) | ||
| if err != nil { | ||
| reportError(fmt.Sprintf("%s", err)) | ||
| continue | ||
| } | ||
|
|
||
| // Doc title needs to match filename, minus the ".md" extension | ||
| fileBase := filepath.Base(filename) | ||
| fileBase = fileBase[0 : len(fileBase)-3] | ||
dhellmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if fileBase != metadata.Title { | ||
| reportError(fmt.Sprintf("the title %s and the file base name %s must match", | ||
| metadata.Title, fileBase, | ||
| )) | ||
| } | ||
|
|
||
| for _, msg := range metadata.Validate() { | ||
| reportError(msg) | ||
| } | ||
| } | ||
|
|
||
| if errorCount > 0 { | ||
| fmt.Fprintf(os.Stderr, "%d errors", errorCount) | ||
| os.Exit(1) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(metadataLintCmd) | ||
|
|
||
| // Here you will define your flags and configuration settings. | ||
|
|
||
| // Cobra supports Persistent Flags which will work for this command | ||
| // and all subcommands, e.g.: | ||
| // metadataLintCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
|
||
| // Cobra supports local flags which will only run when this command | ||
| // is called directly, e.g.: | ||
| // metadataLintCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package enhancements | |
|
|
||
| import ( | ||
| "fmt" | ||
| "net/url" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "gopkg.in/yaml.v2" | ||
|
|
@@ -43,3 +44,83 @@ func (s *Summarizer) GetMetaData(pr int) (*MetaData, error) { | |
| } | ||
| return NewMetaData(content) | ||
| } | ||
|
|
||
| // Validate returns a list of issues found with the metadata | ||
| func (m *MetaData) Validate() []string { | ||
| results := []string{} | ||
|
|
||
| reportError := func(msg string) { | ||
| results = append(results, msg) | ||
| } | ||
|
|
||
| // Must have one valid tracking link and no TBD | ||
| foundLink := false | ||
| for _, trackingLink := range m.TrackingLinks { | ||
| if trackingLink == "TBD" { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably ban
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could. I was focusing on the fields that seemed most important right now, based on issues I've seen with PRs. |
||
| reportError("'TBD' is not a valid value for tracking-link") | ||
| continue | ||
| } | ||
| if trackingLink == "" { | ||
| reportError("tracking-link must not be empty") | ||
| continue | ||
| } | ||
| if u, err := url.Parse(trackingLink); err != nil { | ||
| reportError(fmt.Sprintf("could not parse tracking-link %q: %s", | ||
| trackingLink, err, | ||
| )) | ||
| continue | ||
| } else { | ||
| if u.Scheme == "" { | ||
| reportError(fmt.Sprintf("could not parse tracking-link %q", | ||
| trackingLink, | ||
| )) | ||
| continue | ||
| } | ||
| } | ||
| foundLink = true | ||
| } | ||
| if !foundLink { | ||
| reportError("tracking-link must contain at least one valid URL") | ||
| } | ||
|
|
||
| // No TBD in required people fields | ||
| for _, field := range []struct { | ||
| Name string | ||
| Data []string | ||
| }{ | ||
| { | ||
| Name: "authors", | ||
| Data: m.Authors, | ||
| }, | ||
| { | ||
| Name: "reviewers", | ||
| Data: m.Reviewers, | ||
| }, | ||
| { | ||
| Name: "approvers", | ||
| Data: m.Approvers, | ||
| }, | ||
| { | ||
| Name: "api-approvers", | ||
| Data: m.APIApprovers, | ||
| }, | ||
| } { | ||
| valid := 0 | ||
| for _, value := range field.Data { | ||
| if value == "TBD" { | ||
dhellmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| reportError(fmt.Sprintf("%s must not have TBD as value", field.Name)) | ||
| continue | ||
| } | ||
| if value == "" { | ||
| reportError(fmt.Sprintf("%s must not be an empty string", field.Name)) | ||
| continue | ||
| } | ||
| valid++ | ||
| } | ||
| if valid < 1 { | ||
| reportError(fmt.Sprintf("%s must have at least one valid github id", field.Name)) | ||
| } | ||
| } | ||
|
|
||
| return results | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.