languages to a JSON file private function cache_languages(): int|bool { $cache_filename = $_ENV["forgejo_languages"]["cache_file"]; // Bail out if cache file is not configured if (empty($cache_filename)) { return true; } return file_put_contents($cache_filename, json_encode($this->languages)); } // Fetch and add languages to total from a fully-qualified Forgejo URL private function add_repository_languages(string $url): void { foreach(self::fetch_json($url) as $language => $bytes) { // Create key for language if it doesn't exist if (!array_key_exists($language, $this->languages)) { $this->languages[$language] = 0; } // Add bytes to language in total $this->languages[$language] += $bytes; } } // Tally languages from public repositories for user id private function add_public_repositores(int $uid): bool { $resp = self::fetch_endpoint(sprintf(FORGEJO_ENDPOINT_SEARCH, $uid)); // Bail out if request failed or if response indicated a problem if (!$resp or $resp["ok"] === false) { return false; } // Add langauges for each public repository foreach ($resp["data"] as $repo) { $this->add_repository_languages($repo["languages_url"]); } return true; } // Add languages from all public repositories for profiles in config private function add_repositories_from_config_profiles(): void { foreach(explode(",", $_ENV["forgejo_languages"]["scan_profiles"]) as $profile) { // Resolve user data from username $user = self::fetch_endpoint(sprintf(FORGEJO_ENDPOINT_USER, $profile)); if (!$this->add_public_repositores($user["id"])) { $this->errors[] = $profile; } } } public function main(): Response { $this->add_repositories_from_config_profiles(); // Sort langauges bytes tally by largest in descending order arsort($this->languages); // Save languages to cache if (!$this->cache_languages()) { $this->errors[] = ERRNO_CACHE_FAILED; } return new Response($this->languages); } }