|
| 1 | +/* |
| 2 | +Copyright © 2023 Chandler <chandler@chand1012.dev> |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/chand1012/ottodocs/pkg/ai" |
| 12 | + "github.com/chand1012/ottodocs/pkg/config" |
| 13 | + "github.com/chand1012/ottodocs/pkg/constants" |
| 14 | + "github.com/chand1012/ottodocs/pkg/gh" |
| 15 | + "github.com/chand1012/ottodocs/pkg/git" |
| 16 | + "github.com/chand1012/ottodocs/pkg/utils" |
| 17 | + l "github.com/charmbracelet/log" |
| 18 | + "github.com/spf13/cobra" |
| 19 | +) |
| 20 | + |
| 21 | +// releaseCmd represents the release command |
| 22 | +var releaseCmd = &cobra.Command{ |
| 23 | + Use: "release", |
| 24 | + Short: "Generate GitHub release notes from git commit logs", |
| 25 | + Long: `This command generates GitHub release notes from git commit logs. |
| 26 | +It will create a new release given a tag and post it to GitHub as a draft.`, |
| 27 | + Aliases: []string{"r"}, |
| 28 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 29 | + if verbose { |
| 30 | + log.SetLevel(l.DebugLevel) |
| 31 | + } |
| 32 | + }, |
| 33 | + Run: func(cmd *cobra.Command, args []string) { |
| 34 | + c, err := config.Load() |
| 35 | + if err != nil { |
| 36 | + log.Errorf("Error loading config: %s", err) |
| 37 | + os.Exit(1) |
| 38 | + } |
| 39 | + |
| 40 | + if previousTag == "" { |
| 41 | + previousTag, err = utils.Input("Previous tag: ") |
| 42 | + if err != nil { |
| 43 | + log.Errorf("Error getting previous tag: %s", err) |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if currentTag == "" { |
| 49 | + currentTag, err = utils.Input("Current tag: ") |
| 50 | + if err != nil { |
| 51 | + log.Errorf("Error getting current tag: %s", err) |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + fmt.Print("Release notes: ") |
| 57 | + |
| 58 | + // get the log between the tags |
| 59 | + gitLog, err := git.LogBetween(previousTag, currentTag) |
| 60 | + if err != nil { |
| 61 | + log.Errorf("Error getting log between tags: %s", err) |
| 62 | + os.Exit(1) |
| 63 | + } |
| 64 | + |
| 65 | + prompt := constants.RELEASE_PROMPT + gitLog |
| 66 | + |
| 67 | + stream, err := ai.SimpleStreamRequest(prompt, c) |
| 68 | + if err != nil { |
| 69 | + log.Errorf("Error getting response: %s", err) |
| 70 | + os.Exit(1) |
| 71 | + } |
| 72 | + |
| 73 | + releaseNotes, err := utils.PrintChatCompletionStream(stream) |
| 74 | + if err != nil { |
| 75 | + log.Errorf("Error printing completion stream: %s", err) |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | + |
| 79 | + if !force { |
| 80 | + confirm, err := utils.Input("Create release? (y/n): ") |
| 81 | + if err != nil { |
| 82 | + log.Errorf("Error getting confirmation: %s", err) |
| 83 | + os.Exit(1) |
| 84 | + } |
| 85 | + confirm = strings.ToLower(confirm) |
| 86 | + if confirm != "y" { |
| 87 | + os.Exit(0) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + origin, err := git.GetRemote("origin") |
| 92 | + if err != nil { |
| 93 | + log.Errorf("Error getting remote: %s", err) |
| 94 | + os.Exit(1) |
| 95 | + } |
| 96 | + |
| 97 | + owner, repo, err := git.ExtractOriginInfo(origin) |
| 98 | + if err != nil { |
| 99 | + log.Errorf("Error extracting origin info: %s", err) |
| 100 | + os.Exit(1) |
| 101 | + } |
| 102 | + |
| 103 | + err = gh.CreateDraftRelease(owner, repo, currentTag, releaseNotes, currentTag, c) |
| 104 | + if err != nil { |
| 105 | + log.Errorf("Error creating release: %s", err) |
| 106 | + os.Exit(1) |
| 107 | + } |
| 108 | + |
| 109 | + fmt.Println("Release created successfully!") |
| 110 | + }, |
| 111 | +} |
| 112 | + |
| 113 | +func init() { |
| 114 | + RootCmd.AddCommand(releaseCmd) |
| 115 | + |
| 116 | + releaseCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output") |
| 117 | + releaseCmd.Flags().BoolVarP(&force, "force", "f", false, "Force overwrite of existing file") |
| 118 | + releaseCmd.Flags().StringVarP(&previousTag, "prev-tag", "p", "", "Previous tag") |
| 119 | + releaseCmd.Flags().StringVarP(¤tTag, "tag", "t", "", "Current tag") |
| 120 | +} |
0 commit comments