1 | <?php |
||
2 | |||
3 | namespace App\Hacktoberfest\GitHub; |
||
4 | |||
5 | use GuzzleHttp\Client; |
||
6 | |||
7 | class PullRequestChecker |
||
8 | { |
||
9 | /** |
||
10 | * API base URL consisting of scheme and hostname. |
||
11 | */ |
||
12 | const API_BASE = 'https://api.github.com'; |
||
13 | |||
14 | /** |
||
15 | * Search API endpoint URL. |
||
16 | */ |
||
17 | const API_ENDPOINT_SEARCH = '/search/issues'; |
||
18 | |||
19 | /** |
||
20 | * Repository info API endpoint URL. |
||
21 | */ |
||
22 | const API_ENDPOINT_REPO = '/repos'; |
||
23 | |||
24 | /** |
||
25 | * Instance of the Guzzle HTTP client. |
||
26 | */ |
||
27 | protected $client; |
||
28 | |||
29 | /** |
||
30 | * Instance of the cache provider. |
||
31 | */ |
||
32 | protected $cache; |
||
33 | |||
34 | /** |
||
35 | * Creates a new instance of the pull request checker. |
||
36 | * |
||
37 | * @param \Illuminate\Contracts\Cache\Repository $cache |
||
38 | * @return void |
||
39 | */ |
||
40 | public function __construct($cache) |
||
41 | { |
||
42 | $this->client = new Client([ |
||
43 | 'base_uri' => self::API_BASE, |
||
44 | 'timeout' => 2.0 |
||
45 | ]); |
||
46 | |||
47 | $this->cache = $cache; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Returns the start date of the hacktober month. |
||
52 | * |
||
53 | * @return string the date |
||
54 | */ |
||
55 | protected function getStartDate() |
||
56 | { |
||
57 | return date('Y') . '-09-30T00:00:00-12:00'; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Returns the start end of the hacktober month. |
||
62 | * |
||
63 | * @return string the date |
||
64 | */ |
||
65 | protected function getEndDate() |
||
66 | { |
||
67 | return date('Y') . '-10-31T23:59:59-12:00'; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Builds the query string for querying the GitHub API. |
||
72 | * |
||
73 | * @param string $username |
||
74 | * |
||
75 | * @return string the query |
||
76 | */ |
||
77 | protected function getQuery($username) |
||
78 | { |
||
79 | return 'q=-label:invalid+author:' . $username . '+is:public+type:pr+created:' . |
||
80 | $this->getStartDate() . '..' . $this->getEndDate(); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Returns the instance of the Guttle HTTP client. |
||
85 | * |
||
86 | * @return \GuzzleHttp\Client client |
||
87 | */ |
||
88 | protected function getClient() |
||
89 | { |
||
90 | return $this->client; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Fetches information about a repository from the GitHub API and caches it. |
||
95 | * |
||
96 | * @param string $repoName |
||
97 | * @param string $token |
||
98 | * @return mixed information |
||
99 | */ |
||
100 | protected function getRepositoryInfo($repoName, $token) |
||
101 | { |
||
102 | $cacheKey = 'repo_' . $repoName; |
||
103 | |||
104 | if ($this->cache->has($cacheKey)) { |
||
105 | return $this->cache->get($cacheKey); |
||
106 | } |
||
107 | |||
108 | $response = $this->getClient()->request('GET', self::API_ENDPOINT_REPO . $repoName, [ |
||
109 | 'headers' => [ |
||
110 | 'Authorization' => 'token ' . $token, |
||
111 | ], |
||
112 | ]); |
||
113 | |||
114 | $repo = json_decode($response->getBody()); |
||
115 | |||
116 | $this->cache->put($cacheKey, $repo, 60 * 24); |
||
117 | |||
118 | return $repo; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Extracts the repo name from the repo URL. |
||
123 | * |
||
124 | * @param string $repoUrl |
||
125 | * @return mixed name |
||
126 | */ |
||
127 | protected function getRepositoryName($repoUrl) |
||
128 | { |
||
129 | return str_ireplace(self::API_BASE . self::API_ENDPOINT_REPO, '', $repoUrl); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Fetches all qualified pull requests from the GitHub API. |
||
134 | * |
||
135 | * @param \App\User $user |
||
136 | * @return array the list of qualified pull requests |
||
137 | */ |
||
138 | public function getQualifiedPullRequests($user) |
||
139 | { |
||
140 | $cacheKey = 'prs_' . $user->github_username; |
||
141 | |||
142 | if ($this->cache->has($cacheKey)) { |
||
143 | return $this->cache->get($cacheKey); |
||
144 | } |
||
145 | |||
146 | $response = $this->getClient()->request('GET', self::API_ENDPOINT_SEARCH, [ |
||
147 | 'query' => $this->getQuery($user->github_username), |
||
148 | 'headers' => [ |
||
149 | 'Authorization' => 'token ' . $user->github_token, |
||
150 | ], |
||
151 | ]); |
||
152 | |||
153 | $prs = json_decode($response->getBody()); |
||
154 | |||
155 | if ($prs->total_count > 0 && count($prs->items) > 0) { |
||
156 | foreach ($prs->items as $pr) { |
||
157 | $pr->repo = $this->getRepositoryInfo( |
||
158 | $this->getRepositoryName($pr->repository_url), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
159 | $user->github_token |
||
160 | ); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | //Put in Cache |
||
165 | $this->cache->put($cacheKey, $prs, 0.33); // 20 seconds Timeout |
||
166 | |||
167 | return $prs; |
||
168 | } |
||
169 | } |
||
170 |