From 1886fb46aba416e01e5b6ab43fc8e744342d8fb1 Mon Sep 17 00:00:00 2001 From: Prabhat Kumar Sahu Date: Sat, 17 Aug 2024 03:00:11 +0530 Subject: [PATCH] Fix pr checks exit code (#9452) * Enhance with exit code documentation * Add new error message for PR check * Refine gh pr checks: Add exit code 8 * Update EXIT CODES section format in man page generation --- internal/docs/man.go | 10 ++++++++++ internal/docs/man_test.go | 25 +++++++++++++++++++++++++ pkg/cmd/pr/checks/checks.go | 3 +++ pkg/cmd/root/help.go | 1 + 4 files changed, 39 insertions(+) diff --git a/internal/docs/man.go b/internal/docs/man.go index 5ac353a46..66878d278 100644 --- a/internal/docs/man.go +++ b/internal/docs/man.go @@ -198,6 +198,15 @@ func manPrintJSONFields(buf *bytes.Buffer, command *cobra.Command) { buf.WriteString("\n") } +func manPrintExitCodes(buf *bytes.Buffer) { + buf.WriteString("# EXIT CODES\n") + buf.WriteString("0: Successful execution\n\n") + buf.WriteString("1: Error\n\n") + buf.WriteString("2: Command canceled\n\n") + buf.WriteString("4: Authentication required\n\n") + buf.WriteString("NOTE: Specific commands may have additional exit codes. Refer to the command's help for more information.\n\n") +} + func genMan(cmd *cobra.Command, header *GenManHeader) []byte { cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpFlag() @@ -217,6 +226,7 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte { manPrintOptions(buf, cmd) manPrintAliases(buf, cmd) manPrintJSONFields(buf, cmd) + manPrintExitCodes(buf) if len(cmd.Example) > 0 { buf.WriteString("# EXAMPLE\n") buf.WriteString(fmt.Sprintf("```\n%s\n```\n", cmd.Example)) diff --git a/internal/docs/man_test.go b/internal/docs/man_test.go index 2a0f6a7f7..4db6b7459 100644 --- a/internal/docs/man_test.go +++ b/internal/docs/man_test.go @@ -129,6 +129,31 @@ func TestGenManJSONFields(t *testing.T) { checkStringContains(t, output, "baz") } +func TestGenManDocExitCodes(t *testing.T) { + header := &GenManHeader{ + Title: "Project", + Section: "1", + } + cmd := &cobra.Command{ + Use: "test-command", + Short: "A test command", + Long: "A test command for checking exit codes section", + } + buf := new(bytes.Buffer) + if err := renderMan(cmd, header, buf); err != nil { + t.Fatal(err) + } + output := buf.String() + + // Check for the presence of the exit codes section + checkStringContains(t, output, ".SH EXIT CODES") + checkStringContains(t, output, "0: Successful execution") + checkStringContains(t, output, "1: Error") + checkStringContains(t, output, "2: Command canceled") + checkStringContains(t, output, "4: Authentication required") + checkStringContains(t, output, "NOTE: Specific commands may have additional exit codes. Refer to the command's help for more information.") +} + func TestManPrintFlagsHidesShortDeprecated(t *testing.T) { c := &cobra.Command{} c.Flags().StringP("foo", "f", "default", "Foo flag") diff --git a/pkg/cmd/pr/checks/checks.go b/pkg/cmd/pr/checks/checks.go index bbf2f453b..40b395897 100644 --- a/pkg/cmd/pr/checks/checks.go +++ b/pkg/cmd/pr/checks/checks.go @@ -66,6 +66,9 @@ func NewCmdChecks(f *cmdutil.Factory, runF func(*ChecksOptions) error) *cobra.Co Without an argument, the pull request that belongs to the current branch is selected. + + Additional exit codes: + 8: Checks pending `), Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/pkg/cmd/root/help.go b/pkg/cmd/root/help.go index ee5db5e68..a7daa7b84 100644 --- a/pkg/cmd/root/help.go +++ b/pkg/cmd/root/help.go @@ -182,6 +182,7 @@ func rootHelpFunc(f *cmdutil.Factory, command *cobra.Command, args []string) { helpEntries = append(helpEntries, helpEntry{"LEARN MORE", heredoc.Docf(` Use %[1]sgh --help%[1]s for more information about a command. Read the manual at https://cli.github.com/manual + Learn about exit codes using %[1]sgh help exit-codes%[1]s `, "`")}) out := f.IOStreams.Out