-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
96 lines (85 loc) · 2.34 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
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"time"
jwt "github.com/dgrijalva/jwt-go"
)
var (
OutDir string
IntegrationId string
InstallationId int64
PrivateKeyFile string
)
type GHResponse struct {
Token string `json:"token,omitempty"`
ExpiresAt time.Time `json:"expires_at,omitempty"`
}
func init() {
flag.StringVar(&OutDir, "git-config-out", "", "path where we will write the .gitconfig file")
flag.StringVar(&IntegrationId, "gh-integration-id", "1234", "Github Integration's id")
flag.StringVar(&PrivateKeyFile, "gh-private-key", "private_key.pem", "Full path to the Github Integration's private key")
flag.Int64Var(&InstallationId, "gh-installation-id", 5678, "Github Integtation's installation id")
}
func fatalErr(err error) {
if err != nil {
log.Fatalln(err)
}
}
func main() {
flag.Parse()
data, err := ioutil.ReadFile(PrivateKeyFile)
fatalErr(err)
key, err := jwt.ParseRSAPrivateKeyFromPEM(data)
fatalErr(err)
token, err := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(10 * time.Minute).Unix(),
Issuer: IntegrationId,
}).SignedString(key)
fatalErr(err)
var result GHResponse
req, err := http.NewRequest("POST", fmt.Sprintf("https://api.github.com/app/installations/%d/access_tokens", InstallationId), nil)
fatalErr(err)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Add("Accept", "application/vnd.github.machine-man-preview+json")
fatalErr(JsonResponse(req, &result))
if OutDir == "" {
fmt.Print(result.Token)
} else {
path, err := filepath.Abs(OutDir)
fatalErr(err)
f, err := os.Create(path)
fatalErr(err)
defer f.Close()
t := template.Must(template.New("t1").
Parse(`[url "https://x-access-token:{{.}}@github.com/"]
insteadOf = https://github.com/`))
fatalErr(t.Execute(f, result.Token))
}
}
func JsonResponse(req *http.Request, target interface{}) error {
client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
log.Fatalln(string(data))
}
return json.NewDecoder(resp.Body).Decode(target)
}