-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (98 loc) · 2.42 KB
/
main.go
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
110
111
112
113
114
package main
import (
"os"
"sort"
"github.com/otakakot/repository-dispatch/githubx"
"github.com/urfave/cli/v2"
)
func main() {
tokenKeyFlag := &cli.StringFlag{
Name: "token",
Usage: "GitHub Apps Token",
Required: false,
Aliases: []string{"t"},
}
idFlag := &cli.StringFlag{
Name: "id",
Usage: "GitHub Apps ID",
Required: false,
Aliases: []string{"i"},
}
privateKeyFlag := &cli.StringFlag{
Name: "private-key",
Usage: "GitHub Apps Private Key",
Required: false,
Aliases: []string{"k"},
}
repoOwnerFlag := &cli.StringFlag{
Name: "repository-owner",
Usage: "GitHub Repository Owner",
Required: true,
Aliases: []string{"o"},
}
repoNameFlag := &cli.StringFlag{
Name: "repository-name",
Usage: "GitHub Repository Name",
Required: true,
Aliases: []string{"n"},
}
eventTypeFlag := &cli.StringFlag{
Name: "event-type",
Usage: "GitHub Event Type",
Required: true,
Aliases: []string{"e"},
}
clientPayloadFlag := &cli.StringFlag{
Name: "client-payload",
Usage: "GitHub Client Payload",
Required: true,
Aliases: []string{"p"},
}
app := &cli.App{
Name: "repository-dispatch",
Usage: "Repository Dispatch a GitHub Actions workflow",
Description: "Dispatch a GitHub Actions workflow for a repository. Please specify github apps token or github apps app id and github apps app private key.", //nolint:lll
Flags: []cli.Flag{
tokenKeyFlag,
idFlag,
privateKeyFlag,
repoOwnerFlag,
repoNameFlag,
eventTypeFlag,
clientPayloadFlag,
},
Action: func(ctx *cli.Context) error {
token := ctx.String("token")
if token == "" {
res, _, err := githubx.CreateGitHubAppsToken(
ctx.Context,
githubx.CreateGitHubAppsTokenInput{
GitHubAppID: ctx.String("app-id"),
GitHubAppPrivateKey: ctx.String("app-private-key"),
},
)
if err != nil {
return err
}
token = res.GetToken()
}
if _, _, err := githubx.Dispatch(
ctx.Context,
githubx.DispatchInput{
GitHubAppsToken: token,
RepositoryOwner: ctx.String("repository-owner"),
RepositoryName: ctx.String("repository-name"),
EventType: ctx.String("event-type"),
ClientPayload: ctx.String("client-payload"),
},
); err != nil {
return err
}
return nil
},
}
sort.Sort(cli.FlagsByName(app.Flags))
if err := app.Run(os.Args); err != nil {
panic(err)
}
}