Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
109 lines (95 loc) · 2.99 KB
/
Copy pathmain.go
File metadata and controls
109 lines (95 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"os"
"strings"
api "github.com/quackduck/devzat/devzatapi"
)
const botName = "\x1b[1;36mgithub\x1b[0m"
const helpText = "**gh** — look up GitHub issues and pull requests\n" +
"```\n" +
"gh issue <owner/repo> <number> Show an issue\n" +
"gh pr <owner/repo> <number> Show a pull request\n" +
"gh issues <owner/repo> List open issues\n" +
"gh prs <owner/repo> List open pull requests\n" +
"gh help Show this help\n" +
"```\n" +
"Set `GITHUB_DEFAULT_REPO` to omit <owner/repo> (e.g. `gh pr 123`)."
const usage = `Usage:
devzat-github <host:port> Connect and serve the 'gh' command
devzat-github --print <args...> Format one lookup and print it (no server)
Configuration (environment):
DEVZAT_ADDRESS Server plugin-API address, e.g. localhost:5556 (arg overrides)
DEVZAT_TOKEN Plugin API token (mint with 'grant <you> github-bot' in chat)
GITHUB_TOKEN Optional; raises GitHub API limits and enables private repos
GITHUB_DEFAULT_REPO Optional owner/repo so 'gh pr 123' works without it
Example:
devzat-github --print pr quackduck/devzat 5
`
func main() {
args := os.Args[1:]
if len(args) > 0 && (args[0] == "-h" || args[0] == "--help") {
fmt.Print(usage)
return
}
if len(args) > 0 && args[0] == "--print" {
runPrint(strings.Join(args[1:], " "))
return
}
run()
}
func runPrint(args string) {
client := newGHClient(os.Getenv("GITHUB_TOKEN"), os.Getenv("GITHUB_DEFAULT_REPO"))
msg, err := handle(client, args)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
fmt.Println(msg)
}
func run() {
address := os.Getenv("DEVZAT_ADDRESS")
token := os.Getenv("DEVZAT_TOKEN")
if len(os.Args) > 1 && !strings.HasPrefix(os.Args[1], "-") {
address = os.Args[1]
}
if address == "" {
fmt.Fprintln(os.Stderr, "no server address: pass it as the first argument or set DEVZAT_ADDRESS")
os.Exit(1)
}
if token == "" {
fmt.Fprintln(os.Stderr, "no token: set DEVZAT_TOKEN (mint one with 'grant <you> github-bot' in Devzat)")
os.Exit(1)
}
client := newGHClient(os.Getenv("GITHUB_TOKEN"), os.Getenv("GITHUB_DEFAULT_REPO"))
s, err := api.NewSession(address, token)
if err != nil {
fmt.Fprintln(os.Stderr, "error creating session:", err)
os.Exit(1)
}
defer s.Close()
err = s.RegisterCmd("gh", "<issue|pr|issues|prs> <owner/repo> [number]", "Look up GitHub issues and pull requests", func(cmd api.CmdCall, e error) {
if e != nil {
return
}
msg, herr := handle(client, cmd.Args)
if herr != nil {
msg = "⚠️ " + herr.Error()
}
if msg == "" {
return
}
if err := s.SendMessage(api.Message{Room: cmd.Room, From: botName, Data: msg}); err != nil {
fmt.Fprintln(os.Stderr, "error sending message:", err)
}
})
if err != nil {
fmt.Fprintln(os.Stderr, "error registering command:", err)
os.Exit(1)
}
fmt.Printf("github plugin connected to %s\n", address)
if err := <-s.ErrorChan; err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}