-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_projects_data.php
199 lines (171 loc) · 6.66 KB
/
fetch_projects_data.php
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
//include env.php
require 'env.php';
require 'vendor/autoload.php';
// MongoDB setup
// $client = new MongoDB\Client("mongodb://localhost:27017");
// $database = $client->your_database_name; // Replace with your actual database name
// $collection = $database->repositories; // Replace with your actual collection name
// $username = 'github_username'; // Replace with the GitHub username
// $token = 'your_github_access_token'; // Replace with your GitHub Personal Access Token
$client = new MongoDB\Client($MG_CONNECTION_STRING);
$database = $client->$MG_DB_NAME;
$collection = $database->$MG_COLLECTION_NAME;
$username = $GH_USERNAME;
$token = $GH_PAT;
function checkFilePresence($username, $repoName, $token)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Authorization: token ' . $token,
'User-Agent: PHP Script',
'Accept: application/vnd.github.v3+json'
]);
$files = ['.personal', '.mycrolinks'];
$tags = [];
foreach ($files as $file) {
curl_setopt($curl, CURLOPT_URL, "https://api.github.com/repos/$username/$repoName/contents/$file");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_NOBODY, true); // We're only interested in the response code
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($httpCode == 200) {
$tags[] = $file == '.personal' ? 'personal' : 'mycrolinks';
}
}
if (count($tags) == 2) {
return 'common';
} elseif (count($tags) == 1) {
return $tags[0];
} else {
return 'private';
}
}
function getTags($username, $repo, $token)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.github.com/repos/$username/$repo/contents/.tags",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: token $token",
"User-Agent: GitHub-Repo-Tag-Fetcher"
),
)
);
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response, true);
if (!isset($data['content'])) {
// Handle the error appropriately. Here, we're returning an empty array.
return [];
}
$content = base64_decode($data['content']);
$tags = explode("\n", $content);
$tags = array_map('trim', $tags); // Remove leading and trailing spaces from each tag
return $tags;
}
function fetchRepositories($username, $token)
{
global $collection;
// Check if data is older than 7 days
$lastUpdate = $collection->findOne([], ['sort' => ['updated_at' => -1], 'projection' => ['updated_at' => 1]]);
if ($lastUpdate && strtotime($lastUpdate['updated_at']) > strtotime('-7 days')) {
$storedData = $collection->find([], ['projection' => ['_id' => 0, 'data' => 1]])->toArray()[0]['data'];
// Convert MongoDB\Model\BSONArray to array before using array_map
$storedDataArray = iterator_to_array($storedData); // Fix applied here
$filteredData = array_map(function ($repo) {
return [
'name' => $repo['name'],
'html_url' => $repo['html_url'],
'description' => $repo['description'],
'created_at' => $repo['created_at'],
'updated_at' => $repo['updated_at'],
'visibility' => $repo['visibility'],
'file_presence' => $repo['file_presence'],
'category' => $repo['category'],
'tags' => $repo['tags']
];
}, $storedDataArray); // Use the converted array
return $filteredData; // Return filtered data
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Authorization: token ' . $token,
'User-Agent: PHP Script',
'Accept: application/vnd.github.v3+json'
]);
$page = 1;
$allRepos = [];
do {
curl_setopt($curl, CURLOPT_URL, "https://api.github.com/user/repos?type=all&page=$page&per_page=100");
$response = curl_exec($curl);
$repos = json_decode($response, true);
if (!empty($repos)) {
foreach ($repos as &$repo) {
$filePresence = checkFilePresence($username, $repo['name'], $token);
$tags = getTags($username, $repo['name'], $token);
$category = $tags[0] ?? '';
$tags = array_slice($tags, 1);
if (empty($tags)) {
$tags = [];
}
$repo = [
'name' => $repo['name'],
'html_url' => $repo['html_url'],
'description' => $repo['description'],
'created_at' => $repo['created_at'],
'updated_at' => $repo['updated_at'],
'visibility' => $repo['visibility'],
'file_presence' => $filePresence,
'category' => $category,
'tags' => $tags
];
}
$allRepos = array_merge($allRepos, $repos);
$page++;
}
} while (!empty($repos));
// Update MongoDB with filtered data
//delete all
$collection->deleteMany([]);
$collection->insertOne([
'updated_at' => date('c'), // ISO 8601 format
'data' => $allRepos
]);
return $allRepos;
}
set_time_limit(300);
$projects = fetchRepositories($username, $token);
$uniqueTags = fetchUniqueTags($projects);
function fetchUniqueTags($projects)
{
$tags = [];
if (is_array($projects)) {
foreach ($projects as $project) {
// Check if 'tags' is an instance of ArrayObject
if ($project['tags'] instanceof ArrayObject) {
// Convert ArrayObject to PHP array
$projectTags = $project['tags']->getArrayCopy();
$tags = array_merge($tags, $projectTags);
} else if (is_array($project['tags'])) {
// If 'tags' is a simple array, just merge it
$tags = array_merge($tags, $project['tags']);
}
}
}
$uniqueTags = array_unique($tags);
return $uniqueTags;
}
$filteredProject = array_filter($projects, function ($project) {
return $project['file_presence'] == 'mycrolinks' || $project['file_presence'] == 'common';
});
$projects = array_values($filteredProject);