From 7d140f2e6e06572ccdadf1ea1c4bb5139a0f7ac4 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Mon, 29 Aug 2022 15:00:22 +0300 Subject: [PATCH] Adding initial github packages support --- chirp.php | 72 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/chirp.php b/chirp.php index e015b09..18df6ca 100644 --- a/chirp.php +++ b/chirp.php @@ -143,6 +143,32 @@ function gitea_get_commit($auth_token, $gitea_url, $repo, $sha) return $res; } +function github_get_tags($github_repo) +{ + $api_url = "https://api.github.com/repos/$github_repo/tags"; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $api_url); + curl_setopt($ch, CURLOPT_HEADER, true); + curl_setopt($ch, CURLOPT_USERAGENT, "test"); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + $response = curl_exec($ch); + + $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + $header = substr($response, 0, $header_size); + $json = substr($response, $header_size); + + $info = curl_getinfo($ch); + + curl_close($ch); + + if($info['http_code'] != 200) + return $info['http_code']; + + return json_decode($json, true); +} + function _is_composer_git_repo(array $repos, $repo) { foreach($repos as $item) @@ -153,6 +179,22 @@ function _is_composer_git_repo(array $repos, $repo) return false; } +function _is_composer_github_package(array $repos, $repo, &$github_repo) +{ + foreach($repos as $item) + { + if($item['type'] == 'package' && + $item['package']['name'] == $repo && + preg_match('~https?://github.com/([^/]+)/([^/]+)~', $item['package']['dist']['url'], $matches)) + { + $github_repo = $matches[1].'/'.$matches[2]; + return true; + } + } + return false; +} + + function gitea_check_repo( $auth_token, $gitea_url, @@ -201,18 +243,28 @@ function gitea_check_repo( if(strpos($repo, "bit/") !== 0) continue; - if(!_is_composer_git_repo($composer_arr['repositories'], "$gitea_url/$repo")) - continue; - - $tags = gitea_get_tags($auth_token, $gitea_url, $repo); - if(isset($tags[0])) + if(_is_composer_git_repo($composer_arr['repositories'], "$gitea_url/$repo")) { - $last_tag_info = $tags[0]; - if(ltrim($last_tag_info["name"], 'v') != ltrim($version, 'v')) - echo "Package '$repo': current $version, latest {$last_tag_info["name"]}\n"; + $tags = gitea_get_tags($auth_token, $gitea_url, $repo); + if(isset($tags[0])) + { + $last_tag_info = $tags[0]; + if(ltrim($last_tag_info["name"], 'v') != ltrim($version, 'v')) + echo "Package '$repo': current $version, latest {$last_tag_info["name"]}\n"; + } + else + echo "Could not fetch tags for $repo\n"; + } + else if(_is_composer_github_package($composer_arr['repositories'], $repo, $github_repo)) + { + $tags = github_get_tags($github_repo); + if(isset($tags[0])) + { + $last_tag_info = $tags[0]; + if(ltrim($last_tag_info["name"], 'v') != ltrim($version, 'v')) + echo "Package '$repo': current $version, latest {$last_tag_info["name"]}\n"; + } } - else - echo "Could not fetch tags for $repo\n"; } }