Skip to content

Commit

Permalink
Merge pull request #81 from vitessio/enhance-error-code
Browse files Browse the repository at this point in the history
Enhance and optimize the error code generation
  • Loading branch information
frouioui authored Jul 19, 2024
2 parents 3415871 + efc33b2 commit 25818dc
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 59 deletions.
49 changes: 43 additions & 6 deletions go/documentation_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ limitations under the License.
package main

import (
"bufio"
"context"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"

"github.com/google/go-github/v53/github"
Expand Down Expand Up @@ -87,16 +90,50 @@ func cloneWebsiteAndGetCurrentVersionOfDocs(ctx context.Context, website *git.Re
return "", errors.Wrapf(err, "Failed to pull vitessio/website to generate error code on Pull Request %d", prInfo.num)
}

_, err := shell.NewContext(ctx, "cp", "./tools/get_release_from_docs.sh", website.LocalDir).Output()
currentVersionDocs, err := findCorrespondingDocumentationVersion(website, prInfo.base.GetRef())
if err != nil {
return "", errors.Wrapf(err, "Failed to copy ./tools/get_release_from_docs.sh to local clone of website repo to generate error code on Pull Request %d", prInfo.num)
return "", errors.Wrapf(err, "Failed to find corresponding documentation version for Pull Request %d", prInfo.num)
}
return currentVersionDocs, nil
}

currentVersionDocsBytes, err := shell.New("./get_release_from_docs.sh").InDir(website.LocalDir).Output()
if err != nil {
return "", errors.Wrapf(err, "Failed to get current documentation version from config.toml in vitessio/website to generate error code on Pull Request %d", prInfo.num)
func findCorrespondingDocumentationVersion(website *git.Repo, baseRef string) (string, error) {
// If our base is "main" we want to open the config.toml of the website repository
// and figure out what is the "next" release.
if baseRef == "main" {
file, err := os.Open(path.Join(website.LocalDir, "config.toml"))
if err != nil {
return "", errors.Wrapf(err, "Failed to open config.toml file")
}
defer file.Close()

var result string

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "next") {
parts := strings.Split(line, "\"")
if len(parts) > 1 {
result = parts[1]
}
break
}
}

if err := scanner.Err(); err != nil {
return "", errors.Wrapf(err, "Failed to scan config.toml file %s", file.Name())
}
return strings.TrimSpace(result), nil
}
return string(currentVersionDocsBytes), nil

re := regexp.MustCompile(`release-(\d+\.\d+)`)
matches := re.FindStringSubmatch(baseRef)
if len(matches) == 2 {
return matches[1], nil
}

return "", errors.Errorf("Failed to find corresponding documentation version in config.toml baseRef=%s", baseRef)
}

func generateErrorCodeDocumentation(
Expand Down
125 changes: 77 additions & 48 deletions go/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,59 +130,88 @@ func (h *PullRequestHandler) Handle(ctx context.Context, eventType, deliveryID s
return errors.Wrap(err, "failed to parse issue comment event payload")
}

var err error
switch event.GetAction() {
case "opened":
prInfo := getPRInformation(event)
if prInfo.repoName == "vitess" {
err := h.addReviewChecklist(ctx, event, prInfo)
if err != nil {
return err
}
err = h.addLabels(ctx, event, prInfo)
if err != nil {
return err
}
err = h.createDocsPreview(ctx, event, prInfo)
if err != nil {
return err
}
err = h.createErrorDocumentation(ctx, event, prInfo)
if err != nil {
return err
}
}
err = h.openedPullRequest(ctx, event)
case "closed":
prInfo := getPRInformation(event)
if prInfo.merged && prInfo.repoName == "vitess" {
err := h.backportPR(ctx, event, prInfo)
if err != nil {
return err
}
err = h.updateDocs(ctx, event, prInfo)
if err != nil {
return err
}
}
err = h.closedPullRequest(ctx, event)
case "labeled":
prInfo := getPRInformation(event)
if prInfo.repoName == "vitess" {
err := h.addArewefastyetComment(ctx, event, prInfo)
if err != nil {
return err
}
}
err = h.labeledPullRequest(ctx, event)
case "synchronize":
prInfo := getPRInformation(event)
if prInfo.repoName == "vitess" {
err := h.createDocsPreview(ctx, event, prInfo)
if err != nil {
return err
}
err = h.createErrorDocumentation(ctx, event, prInfo)
if err != nil {
return err
}
}
err = h.synchronizePullRequest(ctx, event)
}
return err
}

func (h *PullRequestHandler) openedPullRequest(ctx context.Context, event github.PullRequestEvent) error {
prInfo := getPRInformation(event)
if prInfo.repoName != "vitess" {
return nil
}

err := h.addReviewChecklist(ctx, event, prInfo)
if err != nil {
return err
}
err = h.addLabels(ctx, event, prInfo)
if err != nil {
return err
}
err = h.createDocsPreview(ctx, event, prInfo)
if err != nil {
return err
}
err = h.createErrorDocumentation(ctx, event, prInfo)
if err != nil {
return err
}
return nil
}

func (h *PullRequestHandler) closedPullRequest(ctx context.Context, event github.PullRequestEvent) error {
prInfo := getPRInformation(event)
if prInfo.repoName != "vitess" || !prInfo.merged {
return nil
}

err := h.backportPR(ctx, event, prInfo)
if err != nil {
return err
}
err = h.updateDocs(ctx, event, prInfo)
if err != nil {
return err
}
return nil
}

func (h *PullRequestHandler) labeledPullRequest(ctx context.Context, event github.PullRequestEvent) error {
prInfo := getPRInformation(event)
if prInfo.repoName != "vitess" {
return nil
}

err := h.addArewefastyetComment(ctx, event, prInfo)
if err != nil {
return err
}
return nil
}

func (h *PullRequestHandler) synchronizePullRequest(ctx context.Context, event github.PullRequestEvent) error {
prInfo := getPRInformation(event)
if prInfo.repoName != "vitess" {
return nil
}

err := h.createDocsPreview(ctx, event, prInfo)
if err != nil {
return err
}
err = h.createErrorDocumentation(ctx, event, prInfo)
if err != nil {
return err
}
return nil
}
Expand Down
5 changes: 0 additions & 5 deletions tools/get_release_from_docs.sh

This file was deleted.

0 comments on commit 25818dc

Please sign in to comment.