@@ -72,24 +72,26 @@ public function handle()
7272 $ this ->log = Log::channel ($ this ->signature );
7373 $ this ->log ->info ('Start job ' );
7474
75- while ($ job = $ this ->takeJob ()) {
76- if (!$ this ->service ) {
77- $ this ->createGitHubService ();
78- $ this ->whitelist = ConfigWhitelist::all ()->keyBy ('value ' );
79- }
75+ if (!QueueJob::count ()) {
76+ $ this ->log ->info ('The queue is empty ' );
77+ exit ;
78+ }
79+
80+ $ this ->createGitHubService ();
81+ $ this ->whitelist = ConfigWhitelist::all ()->keyBy ('value ' );
8082
83+ while ($ job = $ this ->takeJob ()) {
8184 $ page = 1 ;
8285 $ keyword = $ job ->keyword ;
8386 $ configJob = ConfigJob::where ('keyword ' , $ keyword )->first ();
8487 $ configJob ->last_scan_at = date ('Y-m-d H:i:s ' );
8588 do {
8689 $ client = $ this ->service ->getClient ();
87- $ data = $ this ->searchCode ($ client , $ keyword , $ nextPage ?? null );
88- $ count = $ this ->store ($ data , $ keyword );
90+ $ data = $ this ->searchCode ($ client , $ keyword , $ page );
91+ $ count = $ this ->store ($ data , $ configJob );
8992 $ this ->log ->info ('Stored ' , ['keyword ' => $ keyword , 'page ' => $ page , 'count ' => $ count ]);
9093 $ lastResponse = ResponseMediator::getPagination ($ client ->getLastResponse ());
91- $ nextPage = $ lastResponse ['next ' ] ?? false ;
92- } while ($ nextPage && (++$ page <= $ configJob ->scan_page ));
94+ } while ($ lastResponse ['next ' ] && (++$ page <= $ configJob ->scan_page ));
9395 $ configJob ->save ();
9496 }
9597
@@ -117,7 +119,6 @@ private function createGitHubService()
117119 private function takeJob ()
118120 {
119121 if (!$ job = QueueJob::orderBy ('created_at ' )->first ()) {
120- $ this ->log ->info ('The queue is empty ' );
121122 return false ;
122123 }
123124 $ job ->delete ();
@@ -129,17 +130,14 @@ private function takeJob()
129130 *
130131 * @param $client
131132 * @param $keyword
132- * @param null $url
133+ * @param int $page
133134 * @return array|bool|string
134135 */
135- private function searchCode ($ client , $ keyword , $ url = null )
136+ private function searchCode ($ client , $ keyword , $ page = 1 )
136137 {
137138 try {
138- if ($ url ) { // 非首页
139- return ResponseMediator::getContent ($ client ->getHttpClient ()->get ($ url ));
140- }
141139 $ keyword = sprintf ('"%s" ' , $ keyword ); // 精确匹配
142- return $ client ->api ('search ' )->code ($ keyword , 'indexed ' ); // 首页
140+ return $ client ->api ('search ' )->setPage ( $ page )-> code ($ keyword , 'indexed ' );
143141 } catch (Exception $ e ) {
144142 $ this ->log ->warning ($ e ->getMessage ());
145143 return false ;
@@ -150,10 +148,10 @@ private function searchCode($client, $keyword, $url = null)
150148 * 保存数据
151149 *
152150 * @param $data
153- * @param $keyword
154- * @return array
151+ * @param $configJob
152+ * @return int[]
155153 */
156- private function store ($ data , $ keyword )
154+ private function store ($ data , $ configJob )
157155 {
158156 $ count = ['leak ' => 0 , 'fragment ' => 0 ];
159157
@@ -162,8 +160,8 @@ private function store($data, $keyword)
162160 }
163161
164162 foreach ($ data ['items ' ] as $ item ) {
165- $ item ['keyword ' ] = $ keyword ;
166- if (!$ uuid = $ this ->storeLeak ($ item )) {
163+ $ item ['keyword ' ] = $ configJob -> keyword ;
164+ if (!$ uuid = $ this ->storeLeak ($ item, $ configJob -> store_type )) {
167165 continue ;
168166 }
169167 $ count ['leak ' ]++;
@@ -181,9 +179,10 @@ private function store($data, $keyword)
181179 * 保存代码泄露数据
182180 *
183181 * @param $item
182+ * @param $storeType
184183 * @return bool|string
185184 */
186- private function storeLeak ($ item )
185+ private function storeLeak ($ item, $ storeType )
187186 {
188187 $ repoOwner = $ item ['repository ' ]['owner ' ]['login ' ];
189188 $ repoName = $ item ['repository ' ]['name ' ];
@@ -200,10 +199,22 @@ private function storeLeak($item)
200199 }
201200
202201 // 数据入库
202+ $ where = [];
203203 $ uuid = md5 ("$ repoOwner/ $ repoName/ $ blob/ {$ item ['path ' ]}" );
204- $ leak = CodeLeak::firstOrCreate (
205- ['uuid ' => $ uuid ],
206- [
204+ switch ($ storeType ) {
205+ case configJob::STORE_TYPE_ALL :
206+ $ where = ['uuid ' => $ uuid ];
207+ break ;
208+ case configJob::STORE_TYPE_FILE_STORE_ONCE :
209+ $ where = ['repo_owner ' => $ repoOwner , 'repo_name ' => $ repoName , 'path ' => $ item ['path ' ]];
210+ break ;
211+ case configJob::STORE_TYPE_REPO_STORE_ONCE :
212+ $ where = ['repo_owner ' => $ repoOwner , 'repo_name ' => $ repoName ];
213+ break ;
214+ }
215+
216+ $ leak = CodeLeak::firstOrCreate ($ where , [
217+ 'uuid ' => $ uuid ,
207218 'keyword ' => $ item ['keyword ' ],
208219 'repo_owner ' => $ repoOwner ,
209220 'repo_name ' => $ repoName ,
0 commit comments