github.com/supabase/cli@v1.168.1/tools/bumpdoc/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/base64"
     7  	"fmt"
     8  	"io"
     9  	"log"
    10  	"os"
    11  	"os/signal"
    12  	"regexp"
    13  
    14  	"github.com/google/go-github/v62/github"
    15  	"github.com/supabase/cli/internal/utils"
    16  	"github.com/supabase/cli/tools/shared"
    17  )
    18  
    19  const (
    20  	SUPABASE_OWNER = "supabase"
    21  	SUPABASE_REPO  = "supabase"
    22  )
    23  
    24  func main() {
    25  	path := ""
    26  	if len(os.Args) > 1 {
    27  		path = os.Args[1]
    28  	}
    29  
    30  	ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
    31  	if err := updateRefDoc(ctx, path, os.Stdin); err != nil {
    32  		log.Fatalln(err)
    33  	}
    34  }
    35  
    36  var quotePattern = regexp.MustCompile(`(default_value|clispec): "(true|false|[0-9]+|\s*)"`)
    37  
    38  func updateRefDoc(ctx context.Context, path string, stdin io.Reader) error {
    39  	buf, err := io.ReadAll(stdin)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	buf = quotePattern.ReplaceAll(buf, []byte("$1: '$2'"))
    44  	if len(path) == 0 {
    45  		fmt.Print(string(buf))
    46  		return nil
    47  	}
    48  	fmt.Fprintf(os.Stderr, "Updating reference doc: %s\n", path)
    49  	client := utils.GetGtihubClient(ctx)
    50  	branch := "cli/ref-doc"
    51  	master := "master"
    52  	if err := shared.CreateGitBranch(ctx, client, SUPABASE_OWNER, SUPABASE_REPO, branch, master); err != nil {
    53  		return err
    54  	}
    55  	// Get original file
    56  	opts := github.RepositoryContentGetOptions{Ref: "heads/" + branch}
    57  	file, _, _, err := client.Repositories.GetContents(ctx, SUPABASE_OWNER, SUPABASE_REPO, path, &opts)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	content, err := base64.StdEncoding.DecodeString(*file.Content)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	if bytes.Equal(content, buf) {
    66  		fmt.Fprintln(os.Stderr, "All versions are up to date.")
    67  		return nil
    68  	}
    69  	// Update file content
    70  	message := "chore: update cli reference doc"
    71  	commit := github.RepositoryContentFileOptions{
    72  		Message: &message,
    73  		Content: buf,
    74  		SHA:     file.SHA,
    75  		Branch:  &branch,
    76  	}
    77  	resp, _, err := client.Repositories.UpdateFile(ctx, SUPABASE_OWNER, SUPABASE_REPO, path, &commit)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	fmt.Fprintln(os.Stderr, "Committed changes to", *resp.Commit.SHA)
    82  	// Create pull request
    83  	pr := github.NewPullRequest{
    84  		Title: &message,
    85  		Head:  &branch,
    86  		Base:  &master,
    87  	}
    88  	return shared.CreatePullRequest(ctx, client, SUPABASE_OWNER, SUPABASE_REPO, pr)
    89  }