forked from patriotsoftware/slack-bot-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api.go
More file actions
52 lines (46 loc) · 1 KB
/
Copy pathgithub_api.go
File metadata and controls
52 lines (46 loc) · 1 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type EmailResponse struct {
Commit struct {
Author struct {
Email string
}
}
Message string
}
func GetCommitEmail(repo string, sha string, token string) (string, error) {
if repo == "" || sha == "" || token == "" {
return "", fmt.Errorf("the GitHub token, Repository, and SHA are required")
}
client := &http.Client{}
endpoint := fmt.Sprintf("https://api.github.com/repos/%s/commits/%s", repo, sha)
token = "token " + token
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", token)
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var email EmailResponse
err = json.Unmarshal(body, &email)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return email.Message, err
}
return email.Commit.Author.Email, nil
}