Skip to content

Commit

Permalink
feat(actions): automatically generate help for the actions (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobe4 authored Aug 11, 2024
1 parent 659ba43 commit 8e6b6ae
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ jobs:
go-version-file: 'go.mod'

- name: Build
run: go build -v ./cmd/gh-not
run: |
go generate ./...
go build -v ./cmd/gh-not
test:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
# Local testing: goreleaser --snapshot --clean

version: 2

before:
hooks:
- go mod tidy
- go generate ./...

builds:
- main: ./cmd/gh-not
Expand Down
91 changes: 91 additions & 0 deletions cmd/gen-docs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//go:build ignore
// +build ignore

package main

import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)

func main() {
parts := []string{}

err := filepath.Walk("../../internal/actions/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() ||
filepath.Ext(path) != ".go" ||
filepath.Base(path) == "actions.go" {
return nil
}

raw, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("could not read %s: %w", path, err)
}

out, err := format(string(raw))
if err != nil {
return fmt.Errorf("could not get headers from %s: %w", path, err)
}

parts = append(parts, out)

return nil
})

if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}

if err := os.WriteFile("../../internal/cmd/actions-help.txt", []byte(strings.Join(parts, "\n\n")), 0644); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}

func format(content string) (string, error) {
parts := strings.Split(content, "/*")
if len(parts) < 2 {
return "", errors.New("no header found")
}

parts = strings.Split(parts[1], "*/")
if len(parts) < 2 {
return "", errors.New("no header end found")
}

header := strings.Trim(parts[0], "\n")
parts = strings.SplitN(header, "\n", 2)

re := regexp.MustCompile(`Package (\w+) implements an \[actions.Runner\] that (.*)\.`)
matches := re.FindStringSubmatch(parts[0])

if len(matches) < 3 {
return "", fmt.Errorf("header does not match the expected format")
}

outParts := []string{
fmt.Sprintf("%s: %s", matches[1], matches[2]),
}

if len(parts) == 2 {
tail := strings.Trim(parts[1], "\n")
tail = indent(tail)
outParts = append(outParts, tail)
}

return strings.Join(outParts, "\n"), nil
}

func indent(s string) string {
return " " + strings.ReplaceAll(s, "\n", "\n ")
}
2 changes: 2 additions & 0 deletions cmd/gh-not/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/nobe4/gh-not/internal/cmd"
)

//go:generate go run ../gen-docs/main.go

func main() {
if err := cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down
3 changes: 3 additions & 0 deletions internal/actions/debug/debug.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
Package debug implements an [actions.Runner] that prints a notification with a DEBUG prefix.
*/
package debug

import (
Expand Down
8 changes: 6 additions & 2 deletions internal/actions/done/done.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
Package done implements an [actions.Runner] that marks a notification as done.
It updates Meta.Done and marks the notification's thread as done on GitHub.
The notification will be hidden until the thread is updated.
Ref: https://docs.github.com/en/rest/activity/notifications?apiVersion=2022-11-28#mark-a-thread-as-done
*/
package done

import (
Expand All @@ -11,8 +17,6 @@ import (
"github.com/nobe4/gh-not/internal/notifications"
)

// Runner that marks a notification as done.
// Ref: https://docs.github.com/en/rest/activity/notifications?apiVersion=2022-11-28#mark-a-thread-as-done
type Runner struct {
Client *gh.Client
}
Expand Down
4 changes: 4 additions & 0 deletions internal/actions/hide/hide.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
Package hide implements an [actions.Runner] that hides a notification.
It hides the notifications completely.
*/
package hide

import (
Expand Down
3 changes: 3 additions & 0 deletions internal/actions/open/open.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
Package open implements an [actions.Runner] that opens a notification in the browser.
*/
package open

import (
Expand Down
3 changes: 3 additions & 0 deletions internal/actions/pass/pass.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
Package pass implements an [actions.Runner] that does nothing.
*/
package pass

import (
Expand Down
3 changes: 3 additions & 0 deletions internal/actions/print/print.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
Package print implements an [actions.Runner] that prints a notification.
*/
package print

import (
Expand Down
7 changes: 5 additions & 2 deletions internal/actions/read/read.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
Package read implements an [actions.Runner] that marks a notification as read.
It updates Unread and marks the notification's thread as read on GitHub.
Ref: https://docs.github.com/en/rest/activity/notifications?apiVersion=2022-11-28#mark-a-thread-as-read
*/
package read

import (
Expand All @@ -10,8 +15,6 @@ import (
"github.com/nobe4/gh-not/internal/notifications"
)

// Runner that marks a notification as read.
// Ref: https://docs.github.com/en/rest/activity/notifications?apiVersion=2022-11-28#mark-a-thread-as-read
type Runner struct {
Client *gh.Client
}
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
actions-help.txt
26 changes: 9 additions & 17 deletions internal/cmd/actions.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package cmd

import "github.com/spf13/cobra"
import (
_ "embed"

"github.com/spf13/cobra"
)

//go:embed actions-help.txt

Check failure on line 9 in internal/cmd/actions.go

View workflow job for this annotation

GitHub Actions / test

pattern actions-help.txt: no matching files found
var longHelp string

var (
actionsCmd = &cobra.Command{
Use: "actions",
Short: "Show information about the actions",
Long: `
'gh-not' has multiple actions that perform different actions:
open: Open the notification in a web browser.
hide: Mark the notification as hidden in the cache.
It won't show the notification again.
done: Mark the notification as done in the cache and on the API.
It hides the notification until an update happens.
read: Mark the notification as read in the cache and on the API.
It hides the notification until an update happens.
TODO: remove debug/print/pass
`,
Long: "'gh-not' has multiple actions that perform different actions:\n\n" + longHelp,
}
)

Expand Down

0 comments on commit 8e6b6ae

Please sign in to comment.