-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (63 loc) · 1.75 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-tools/go-steputils/stepconf"
"io/ioutil"
"net/http"
"os"
"strings"
)
type Config struct {
AuthToken stepconf.Secret `env:"personal_access_token,required"`
Body string `env:"body,required"`
RepositoryURL string `env:"repository_url,required"`
IssueNumber string `env:"issue_number,required"`
APIBaseURL string `env:"api_base_url,required"`
}
type Payload struct {
Body string `json:"body"`
}
func ownerAndRepo(url string) (string, string) {
url = strings.TrimPrefix(strings.TrimPrefix(url, "https://"), "git@")
paths := strings.FieldsFunc(url, func(r rune) bool { return r == '/' || r == ':' })
return paths[1], strings.TrimSuffix(paths[2], ".git")
}
func main() {
var conf Config
if err := stepconf.Parse(&conf); err != nil {
log.Errorf("Error: %s\n", err)
os.Exit(1)
}
stepconf.Print(conf)
owner, repo := ownerAndRepo(conf.RepositoryURL)
url := fmt.Sprintf("%s/repos/%s/%s/issues/%s/comments", conf.APIBaseURL, owner, repo, conf.IssueNumber)
data := Payload{
conf.Body,
}
payloadBytes, err := json.Marshal(data)
if err != nil {
log.Errorf("Error: %s\n", err)
os.Exit(1)
}
body := bytes.NewReader(payloadBytes)
req, err := http.NewRequest("POST", url, body)
if err != nil {
log.Errorf("Error: %s\n", err)
os.Exit(1)
}
req.Header.Set("Authorization", "token " + string(conf.AuthToken))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Errorf("Error: %s\n", err)
os.Exit(1)
}
respBody, _ := ioutil.ReadAll(resp.Body)
log.Successf("Success: %s\n", respBody)
defer resp.Body.Close()
os.Exit(0)
}