Skip to content
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

Fix false negatives in the docpaths workflow #321

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions bot/internal/bot/docpaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bot
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -36,19 +35,19 @@ type DocsRedirect struct {
// directory that lists redirects in the redirects field.
func (b *Bot) CheckDocsPathsForMissingRedirects(ctx context.Context, teleportClonePath string) error {
if teleportClonePath == "" {
return trace.Wrap(errors.New("unable to load Teleport documentation config with an empty path"))
return trace.BadParameter("unable to load Teleport documentation config with an empty path")
}

docsConfigPath := filepath.Join(teleportClonePath, "docs", "config.json")
f, err := os.Open(docsConfigPath)
if err != nil {
return trace.BadParameter("unable to load Teleport documentation config with an empty path")
return trace.BadParameter("unable to load Teleport documentation config at %v: %v", teleportClonePath, err)
}
defer f.Close()

var c DocsConfig
if err := json.NewDecoder(f).Decode(&c); err != nil {
return trace.Wrap(err, "unable to load redirect configuration from %v: %v", docsConfigPath)
return trace.BadParameter("unable to load redirect configuration from %v: %v", docsConfigPath, err)
}

files, err := b.c.GitHub.ListFiles(ctx, b.c.Environment.Organization, b.c.Environment.Repository, b.c.Environment.Number)
Expand All @@ -58,7 +57,7 @@ func (b *Bot) CheckDocsPathsForMissingRedirects(ctx context.Context, teleportClo

m := missingRedirectSources(c.Redirects, files)
if len(m) > 0 {
return trace.Wrap(err, "docs config at %v is missing redirects for the following renamed or deleted pages: %v", docsConfigPath, strings.Join(m, ","))
return trace.Errorf("docs config at %v is missing redirects for the following renamed or deleted pages: %v", docsConfigPath, strings.Join(m, ","))
}

return nil
Expand Down
83 changes: 83 additions & 0 deletions bot/internal/bot/docpaths_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,95 @@
package bot

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/gravitational/shared-workflows/bot/internal/env"
"github.com/gravitational/shared-workflows/bot/internal/github"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCheckDocsPathsForMissingRedirects(t *testing.T) {
cases := []struct {
description string
teleportClonePath string
docsConfig string
errorSubstring string
}{
{
description: "valid clone path with no error",
teleportClonePath: "/teleport",
docsConfig: `{
"navigation": [],
"variables": {},
"redirects": [
{
"source": "/database-access/get-started/",
"destination": "/enroll-resources/database-access/get-started/",
"permanent": true
}
]
}`,
errorSubstring: "",
},
{
description: "valid clone path with missing redirect",
teleportClonePath: "/teleport",
docsConfig: `{
"navigation": [],
"variables": {},
"redirects": []
}`,
errorSubstring: "missing redirects for the following renamed or deleted pages: /database-access/get-started/",
},
{
description: "invalid config file",
teleportClonePath: "/teleport",
docsConfig: `This file is not JSON.`,
errorSubstring: "docs/config.json: invalid character 'T' looking for beginning of value",
},
}

for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
tmpdir := t.TempDir()
err := os.MkdirAll(filepath.Join(tmpdir, "teleport", "docs"), 0777)
require.NoError(t, err)

f, err := os.Create(filepath.Join(tmpdir, "teleport", "docs", "config.json"))
require.NoError(t, err)

_, err = f.WriteString(c.docsConfig)
require.NoError(t, err)

b := &Bot{
c: &Config{
Environment: &env.Environment{},
GitHub: &fakeGithub{
files: []github.PullRequestFile{
{
Name: "docs/pages/enroll-resources/database-access/get-started.mdx",
Status: "renamed",
PreviousName: "docs/pages/database-access/get-started.mdx",
},
},
},
},
}

err = b.CheckDocsPathsForMissingRedirects(context.Background(), filepath.Join(tmpdir, c.teleportClonePath))
if c.errorSubstring == "" {
assert.NoError(t, err)
return
}
assert.ErrorContains(t, err, c.errorSubstring)
})
}
}

func TestMissingRedirectSources(t *testing.T) {
cases := []struct {
description string
Expand Down
Loading