Skip to content

Commit

Permalink
Merge pull request #11677 from sharifelgamal/auto-gen-docs
Browse files Browse the repository at this point in the history
Automatically run `make generate-docs` after every PR merge
  • Loading branch information
sharifelgamal authored Jun 18, 2021
2 parents c3f46d1 + c8fb432 commit 811ff18
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 39 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "generate-docs"
on:
push:
branches:
- master

jobs:
generate-docs:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.16.4
stable: true
- name: gendocs
run: |
./hack/generate_docs.sh ${{ secrets.MINIKUBE_BOT_PAT }}
2 changes: 1 addition & 1 deletion .github/workflows/time-to-k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
run: git submodule update --init
- uses: actions/setup-go@v2
with:
go-version: 1.16.5
go-version: 1.16.4
stable: true
- name: Benchmark
run: |
Expand Down
38 changes: 0 additions & 38 deletions cmd/minikube/cmd/generate-docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,22 @@ limitations under the License.
package cmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/spf13/pflag"
"k8s.io/minikube/pkg/generate"
)

func TestGenerateDocs(t *testing.T) {
pflag.BoolP("help", "h", false, "") // avoid 'Docs are not updated. Please run `make generate-docs` to update commands documentation' error
dir := "../../../site/content/en/docs/commands/"

for _, sc := range RootCmd.Commands() {
t.Run(sc.Name(), func(t *testing.T) {
if sc.Hidden {
t.Skip()
}
fp := filepath.Join(dir, fmt.Sprintf("%s.md", sc.Name()))
expectedContents, err := ioutil.ReadFile(fp)
if err != nil {
t.Fatalf("Docs are not updated. Please run `make generate-docs` to update commands documentation: %v", err)
}
actualContents, err := generate.DocForCommand(sc)
if err != nil {
t.Fatalf("error getting contents: %v", err)
}
if diff := cmp.Diff(actualContents, string(expectedContents)); diff != "" {
t.Fatalf("Docs are not updated. Please run `make generate-docs` to update commands documentation: %s", diff)
}
})
}
}

func TestGenerateTestDocs(t *testing.T) {
tempdir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("creating temp dir failed: %v", err)
}
defer os.RemoveAll(tempdir)
docPath := filepath.Join(tempdir, "tests.md")
realPath := "../../../site/content/en/docs/contrib/tests.en.md"

expectedContents, err := ioutil.ReadFile(realPath)
if err != nil {
t.Fatalf("error reading existing file: %v", err)
}

err = generate.TestDocs(docPath, "../../../test/integration")
if err != nil {
Expand All @@ -77,10 +43,6 @@ func TestGenerateTestDocs(t *testing.T) {
t.Fatalf("error reading generated file: %v", err)
}

if diff := cmp.Diff(string(actualContents), string(expectedContents)); diff != "" {
t.Errorf("Test docs are not updated. Please run `make generate-docs` to update documentation: %s", diff)
}

rest := string(actualContents)
for rest != "" {
rest = checkForNeedsDoc(t, rest)
Expand Down
53 changes: 53 additions & 0 deletions hack/generate_docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

# Copyright 2021 The Kubernetes Authors All rights reserved.
#
# 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.

set -x

if [ "$#" -ne 1 ]; then
# there's no secret and therefore no reason to run this script
exit 0
fi

install_gh() {
export access_token="$1"

# Make sure gh is installed and configured
./hack/jenkins/installers/check_install_gh.sh
}

config_git() {
git config user.name "minikube-bot"
git config user.email "minikube-bot@google.com"
}

make generate-docs

# If there are changes, open a PR
git diff-index --quiet HEAD --
if [ $? -gt 0 ]; then
install_gh $1
config_git

branch=gendocs$(date +%s%N)
git checkout -b $branch

git add .
git commit -m "Update generate-docs"

git remote add minikube-bot https://minikube-bot:$1@github.com/minikube-bot/minikube.git
git push -u minikube-bot $branch
gh pr create --repo kubernetes/minukube --base master --title "Update generate-docs" --body "Committing changes resulting from \`make generate-docs\`"
fi
10 changes: 10 additions & 0 deletions hack/update/golang_version/update_golang_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ var (
`go-version: '.*`: `go-version: '{{.StableVersion}}'`,
},
},
".github/workflows/docs.yml": {
Replace: map[string]string{
`go-version: '.*`: `go-version: '{{.StableVersion}}'`,
},
},
".github/workflows/time-to-k8s.yml": {
Replace: map[string]string{
`go-version: '.*`: `go-version: '{{.StableVersion}}'`,
},
},
".travis.yml": {
Replace: map[string]string{
`go:\n - .*`: `go:{{printf "\n - %s" .StableVersion}}`,
Expand Down

0 comments on commit 811ff18

Please sign in to comment.