2
2
3
3
final class DiffusionServeController extends DiffusionController {
4
4
5
+ private $ serviceViewer ;
6
+ private $ serviceRepository ;
7
+
8
+ public function setServiceViewer (PhabricatorUser $ viewer ) {
9
+ $ this ->serviceViewer = $ viewer ;
10
+ return $ this ;
11
+ }
12
+
13
+ public function getServiceViewer () {
14
+ return $ this ->serviceViewer ;
15
+ }
16
+
17
+ public function setServiceRepository (PhabricatorRepository $ repository ) {
18
+ $ this ->serviceRepository = $ repository ;
19
+ return $ this ;
20
+ }
21
+
22
+ public function getServiceRepository () {
23
+ return $ this ->serviceRepository ;
24
+ }
25
+
5
26
public function isVCSRequest (AphrontRequest $ request ) {
6
27
$ identifier = $ this ->getRepositoryIdentifierFromRequest ($ request );
7
28
if ($ identifier === null ) {
@@ -45,6 +66,75 @@ public function isVCSRequest(AphrontRequest $request) {
45
66
}
46
67
47
68
public function handleRequest (AphrontRequest $ request ) {
69
+ $ service_exception = null ;
70
+ $ response = null ;
71
+
72
+ try {
73
+ $ response = $ this ->serveRequest ($ request );
74
+ } catch (Exception $ ex ) {
75
+ $ service_exception = $ ex ;
76
+ }
77
+
78
+ try {
79
+ $ remote_addr = $ request ->getRemoteAddr ();
80
+ $ remote_addr = ip2long ($ remote_addr );
81
+
82
+ $ pull_event = id (new PhabricatorRepositoryPullEvent ())
83
+ ->setEpoch (PhabricatorTime::getNow ())
84
+ ->setRemoteAddress ($ remote_addr )
85
+ ->setRemoteProtocol ('http ' );
86
+
87
+ if ($ response ) {
88
+ $ pull_event
89
+ ->setResultType ('wild ' )
90
+ ->setResultCode ($ response ->getHTTPResponseCode ());
91
+
92
+ if ($ response instanceof PhabricatorVCSResponse) {
93
+ $ pull_event ->setProperties (
94
+ array (
95
+ 'response.message ' => $ response ->getMessage (),
96
+ ));
97
+ }
98
+ } else {
99
+ $ pull_event
100
+ ->setResultType ('exception ' )
101
+ ->setResultCode (500 )
102
+ ->setProperties (
103
+ array (
104
+ 'exception.class ' => $ ex ->getClass (),
105
+ 'exception.message ' => $ ex ->getMessage (),
106
+ ));
107
+ }
108
+
109
+ $ viewer = $ this ->getServiceViewer ();
110
+ if ($ viewer ) {
111
+ $ pull_event ->setPullerPHID ($ viewer ->getPHID ());
112
+ }
113
+
114
+ $ repository = $ this ->getServiceRepository ();
115
+ if ($ repository ) {
116
+ $ pull_event ->setRepositoryPHID ($ repository ->getPHID ());
117
+ }
118
+
119
+ $ unguarded = AphrontWriteGuard::beginScopedUnguardedWrites ();
120
+ $ pull_event ->save ();
121
+ unset($ unguarded );
122
+
123
+ } catch (Exception $ ex ) {
124
+ if ($ service_exception ) {
125
+ throw $ service_exception ;
126
+ }
127
+ throw $ ex ;
128
+ }
129
+
130
+ if ($ service_exception ) {
131
+ throw $ service_exception ;
132
+ }
133
+
134
+ return $ response ;
135
+ }
136
+
137
+ private function serveRequest (AphrontRequest $ request ) {
48
138
$ identifier = $ this ->getRepositoryIdentifierFromRequest ($ request );
49
139
50
140
// If authentication credentials have been provided, try to find a user
@@ -65,6 +155,8 @@ public function handleRequest(AphrontRequest $request) {
65
155
$ viewer = new PhabricatorUser ();
66
156
}
67
157
158
+ $ this ->setServiceViewer ($ viewer );
159
+
68
160
$ allow_public = PhabricatorEnv::getEnvConfig ('policy.allow-public ' );
69
161
$ allow_auth = PhabricatorEnv::getEnvConfig ('diffusion.allow-http-auth ' );
70
162
if (!$ allow_public ) {
@@ -111,6 +203,8 @@ public function handleRequest(AphrontRequest $request) {
111
203
}
112
204
}
113
205
206
+ $ this ->setServiceRepository ($ repository );
207
+
114
208
if (!$ repository ->isTracked ()) {
115
209
return new PhabricatorVCSResponse (
116
210
403 ,
0 commit comments