-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (116 loc) · 3.7 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/google/go-github/github"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "drone-github-commit-status",
Usage: "sends a commit status to github via drone",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "github_app_id",
Usage: "github app id",
EnvVars: []string{"PLUGIN_GITHUB_APP_ID"},
Required: true,
},
&cli.StringFlag{
Name: "github_installation_id",
Usage: "github installation id",
EnvVars: []string{"PLUGIN_GITHUB_INSTALLATION_ID"},
Required: true,
},
&cli.StringFlag{
Name: "github_app_private_key",
Usage: "github private key string",
EnvVars: []string{"PLUGIN_GITHUB_APP_PRIVATE_KEY"},
Required: true,
},
&cli.StringFlag{
Name: "commit_state",
Usage: "State is the current state of the repository. Possible values are: pending, success, error, or failure",
EnvVars: []string{"PLUGIN_COMMIT_STATE"},
DefaultText: "success",
},
&cli.StringFlag{
Name: "commit_context",
Usage: "A string label to differentiate this status from the statuses of other systems.",
EnvVars: []string{"PLUGIN_COMMIT_CONTEXT"},
DefaultText: "drone-github-commit-status",
},
&cli.StringFlag{
Name: "commit_target_url",
Usage: "TargetURL is the URL of the page representing this status. It will be linked from the GitHub UI to allow users to see the source of the status.",
EnvVars: []string{"PLUGIN_COMMIT_TARGET_URL"},
Required: false,
},
&cli.StringFlag{
Name: "commit_description",
Usage: "Description is a short high level summary of the status.",
EnvVars: []string{"PLUGIN_COMMIT_DESCRIPTION"},
Required: false,
},
},
Action: func(cCtx *cli.Context) error {
drone_repo_owner := os.Getenv("DRONE_REPO_OWNER")
drone_repo_name := os.Getenv("DRONE_REPO_NAME")
drone_commit_sha := os.Getenv("DRONE_COMMIT_SHA")
github_app_id, err := strconv.ParseInt(cCtx.String("github_app_id"), 10, 64)
if err != nil {
log.Fatal(err)
}
github_installation_id, err := strconv.ParseInt(cCtx.String("github_installation_id"), 10, 64)
if err != nil {
log.Fatal(err)
}
github_private_key := cCtx.String("github_app_private_key")
status_state := cCtx.String("commit_state")
status_context := cCtx.String("commit_context")
status_target_url := cCtx.String("commit_target_url")
status_description := cCtx.String("commit_description")
status := github.RepoStatus{
State: &status_state,
Context: &status_context,
TargetURL: &status_target_url,
Description: &status_description,
}
// Shared transport to reuse TCP connections.
tr := http.DefaultTransport
writePrivateKeyFile(github_private_key)
itr, err := ghinstallation.NewKeyFromFile(tr, github_app_id, github_installation_id, "private-key.pem")
if err != nil {
log.Fatal(err)
}
// Use installation transport with github.com/google/go-github
client := github.NewClient(&http.Client{Transport: itr})
res, _, err := client.Repositories.CreateStatus(context.Background(), drone_repo_owner, drone_repo_name, drone_commit_sha, &status)
if err != nil {
log.Fatal(err)
}
json, err := json.Marshal(res)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(json))
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func writePrivateKeyFile(privateKeyString string) {
b := []byte(privateKeyString)
err := os.WriteFile("private-key.pem", b, 0644)
if err != nil {
log.Fatal(err)
}
}