-
Notifications
You must be signed in to change notification settings - Fork 2
/
issuesv2.go
178 lines (159 loc) · 6.48 KB
/
issuesv2.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"context"
"net/http"
"net/url"
"os"
"path"
"strings"
"dmitri.shuralyov.com/route/github"
"github.com/shurcooL/events"
"github.com/shurcooL/home/httputil"
issues "github.com/shurcooL/home/internal/exp/service/issue"
"github.com/shurcooL/home/internal/exp/service/issue/fs"
"github.com/shurcooL/home/internal/exp/service/issue/githubapi"
"github.com/shurcooL/home/internal/exp/service/issue/httphandler"
"github.com/shurcooL/home/internal/exp/service/issue/httproute"
"github.com/shurcooL/home/internal/exp/service/notification"
"github.com/shurcooL/httperror"
"github.com/shurcooL/users"
"golang.org/x/net/webdav"
)
func newIssuesServiceV2(root webdav.FileSystem, notification notification.Service, events events.ExternalService, users users.Service, router github.Router) (issues.Service, error) {
local, err := fs.NewService(root, notification, events, users)
if err != nil {
return nil, err
}
dmitshurGitHubIssues := githubapi.NewService(
dmitshurPublicRepoGHV3,
dmitshurPublicRepoGHV4,
notification,
router,
)
return dmitshurSeesExternalIssues{
local: local,
dmitshurGitHubIssues: dmitshurGitHubIssues,
users: users,
}, nil
}
type issueCounter interface {
// Count issues.
Count(ctx context.Context, repo issues.RepoSpec, opt issues.IssueListOptions) (uint64, error)
}
// TODO: Remove unused parameters in initIssuesV2.
// initIssuesV2 registers handlers for the issues service HTTP API,
// and handlers for the issues app.
func initIssuesV2(mux *http.ServeMux, issuesService issues.Service, issuesApp httperror.Handler, users users.Service) {
// Register issue service HTTP API endpoints.
apiHandler := httphandler.Issues{Issues: issuesService}
mux.Handle(path.Join("/api/issue", httproute.List), headerAuth{httputil.ErrorHandler(users, apiHandler.List)})
mux.Handle(path.Join("/api/issue", httproute.Count), headerAuth{httputil.ErrorHandler(users, apiHandler.Count)})
mux.Handle(path.Join("/api/issue", httproute.Get), headerAuth{httputil.ErrorHandler(users, apiHandler.Get)})
mux.Handle(path.Join("/api/issue", httproute.ListTimeline), headerAuth{httputil.ErrorHandler(users, apiHandler.ListTimeline)})
mux.Handle(path.Join("/api/issue", httproute.Create), headerAuth{httputil.ErrorHandler(users, apiHandler.Create)})
mux.Handle(path.Join("/api/issue", httproute.CreateComment), headerAuth{httputil.ErrorHandler(users, apiHandler.CreateComment)})
mux.Handle(path.Join("/api/issue", httproute.Edit), headerAuth{httputil.ErrorHandler(users, apiHandler.Edit)})
mux.Handle(path.Join("/api/issue", httproute.EditComment), headerAuth{httputil.ErrorHandler(users, apiHandler.EditComment)})
mux.Handle(path.Join("/api/issue", httproute.ThreadType), headerAuth{httputil.ErrorHandler(users, apiHandler.ThreadType)})
issuesHandler := cookieAuth{httputil.ErrorHandler(users, func(w http.ResponseWriter, req *http.Request) error {
returnURL := req.URL.Path
err := issuesApp.ServeHTTP(w, req)
// TODO: Factor out this os.IsPermission(err) && u == nil check somewhere, if possible. (But this shouldn't apply for APIs.)
if s := req.Context().Value(sessionContextKey).(*session); os.IsPermission(err) && s == nil {
loginURL := (&url.URL{
Path: "/login",
RawQuery: url.Values{returnParameterName: {returnURL}}.Encode(),
}).String()
return httperror.Redirect{URL: loginURL}
}
return err
})}
mux.Handle("/issues/", issuesHandler)
}
// dmitshurSeesExternalIssues gives dmitshur access to issues on GitHub,
// in addition to local ones.
type dmitshurSeesExternalIssues struct {
local issues.Service
dmitshurGitHubIssues issues.Service
users users.Service
}
func (s dmitshurSeesExternalIssues) List(ctx context.Context, repo issues.RepoSpec, opt issues.IssueListOptions) ([]issues.Issue, error) {
service, err := s.service(ctx, repo)
if err != nil {
return nil, err
}
return service.List(ctx, repo, opt)
}
func (s dmitshurSeesExternalIssues) Count(ctx context.Context, repo issues.RepoSpec, opt issues.IssueListOptions) (uint64, error) {
service, err := s.service(ctx, repo)
if err != nil {
return 0, err
}
return service.Count(ctx, repo, opt)
}
func (s dmitshurSeesExternalIssues) Get(ctx context.Context, repo issues.RepoSpec, id uint64) (issues.Issue, error) {
service, err := s.service(ctx, repo)
if err != nil {
return issues.Issue{}, err
}
return service.Get(ctx, repo, id)
}
func (s dmitshurSeesExternalIssues) ListTimeline(ctx context.Context, repo issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]interface{}, error) {
service, err := s.service(ctx, repo)
if err != nil {
return nil, err
}
return service.ListTimeline(ctx, repo, id, opt)
}
func (s dmitshurSeesExternalIssues) Create(ctx context.Context, repo issues.RepoSpec, issue issues.Issue) (issues.Issue, error) {
service, err := s.service(ctx, repo)
if err != nil {
return issues.Issue{}, err
}
return service.Create(ctx, repo, issue)
}
func (s dmitshurSeesExternalIssues) CreateComment(ctx context.Context, repo issues.RepoSpec, id uint64, comment issues.Comment) (issues.Comment, error) {
service, err := s.service(ctx, repo)
if err != nil {
return issues.Comment{}, err
}
return service.CreateComment(ctx, repo, id, comment)
}
func (s dmitshurSeesExternalIssues) Edit(ctx context.Context, repo issues.RepoSpec, id uint64, ir issues.IssueRequest) (issues.Issue, []issues.Event, error) {
service, err := s.service(ctx, repo)
if err != nil {
return issues.Issue{}, nil, err
}
return service.Edit(ctx, repo, id, ir)
}
func (s dmitshurSeesExternalIssues) EditComment(ctx context.Context, repo issues.RepoSpec, id uint64, cr issues.CommentRequest) (issues.Comment, error) {
service, err := s.service(ctx, repo)
if err != nil {
return issues.Comment{}, err
}
return service.EditComment(ctx, repo, id, cr)
}
func (s dmitshurSeesExternalIssues) ThreadType(ctx context.Context, repo issues.RepoSpec) (string, error) {
service, err := s.service(ctx, repo)
if err != nil {
return "", err
}
return service.ThreadType(ctx, repo)
}
func (s dmitshurSeesExternalIssues) service(ctx context.Context, repo issues.RepoSpec) (issues.Service, error) {
switch {
default:
return s.local, nil
case strings.HasPrefix(repo.URI, "github.com/") &&
repo.URI != "github.com/shurcooL/issuesapp" &&
repo.URI != "github.com/shurcooL/notificationsapp":
currentUser, err := s.users.GetAuthenticatedSpec(ctx)
if err != nil {
return nil, err
}
if currentUser != dmitshur {
return nil, os.ErrPermission
}
return s.dmitshurGitHubIssues, nil
}
}