forked from stakwork/sphinx-tribes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.go
117 lines (109 loc) · 2.73 KB
/
github.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"github.com/go-chi/chi"
"github.com/google/go-github/v39/github"
"golang.org/x/oauth2"
)
func getGithubIssue(w http.ResponseWriter, r *http.Request) {
owner := chi.URLParam(r, "owner")
repo := chi.URLParam(r, "repo")
issueString := chi.URLParam(r, "issue")
issueNum, err := strconv.Atoi(issueString)
if err != nil || issueNum < 1 {
w.WriteHeader(http.StatusNotAcceptable)
return
}
issue, err := GetIssue(owner, repo, issueNum)
if err != nil {
fmt.Println("Github error: ", err.Error())
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(issue)
}
func getOpenGithubIssues(w http.ResponseWriter, r *http.Request) {
issue_count, err := DB.getOpenGithubIssues(r)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
return
}
json.NewEncoder(w).Encode(issue_count)
}
func githubClient() *github.Client {
gh_token := os.Getenv("GITHUB_TOKEN")
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: gh_token},
)
tc := oauth2.NewClient(ctx, ts)
gc := github.NewClient(tc)
return gc
}
func GetRepoIssues(owner string, repo string) ([]GithubIssue, error) {
client := githubClient()
issues, _, err := client.Issues.ListByRepo(context.Background(), owner, repo, nil)
ret := []GithubIssue{}
if err == nil {
for _, iss := range issues {
assignee := ""
if iss.Assignee != nil {
assignee = *iss.Assignee.Login
}
ret = append(ret, GithubIssue{
Title: *iss.Title,
Status: *iss.State,
Assignee: assignee,
})
}
}
return ret, err
}
func GetIssue(owner string, repo string, id int) (GithubIssue, error) {
client := githubClient()
iss, _, err := client.Issues.Get(context.Background(), owner, repo, id)
issue := GithubIssue{}
if err == nil && iss != nil {
assignee := ""
if iss.Assignee != nil {
assignee = *iss.Assignee.Login
}
issue = GithubIssue{
Title: *iss.Title,
Status: *iss.State,
Assignee: assignee,
Description: *iss.Body,
}
}
return issue, err
}
func PubkeyForGithubUser(owner string) (string, error) {
client := githubClient()
gs, _, err := client.Gists.List(context.Background(), owner, nil)
if err == nil && gs != nil {
for _, g := range gs {
if g.Files != nil {
for k := range g.Files {
if strings.Contains(string(k), "Sphinx Verification") {
// get the actual gist
gist, _, err := client.Gists.Get(context.Background(), *g.ID)
gistFile := gist.Files[k]
pubkey, err := VerifyArbitrary(*gistFile.Content, "Sphinx Verification")
if err != nil {
return "", err
}
return pubkey, nil
}
}
}
}
}
return "", errors.New("nope")
}