Skip to content

Commit

Permalink
Fix pr checks exit code (cli#9452)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
thecaffeinedev authored Aug 16, 2024
1 parent 255f530 commit 1886fb4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/docs/man.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
Expand Down
25 changes: 25 additions & 0 deletions internal/docs/man_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/pr/checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/root/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command> <subcommand> --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
Expand Down

0 comments on commit 1886fb4

Please sign in to comment.