From 7af94e36b6463a2601ca6950103ef73e63d4e794 Mon Sep 17 00:00:00 2001 From: icyleaf Date: Sat, 28 Apr 2018 18:18:00 +0800 Subject: [PATCH] feat: search repository --- docs/_coverpage.md | 2 +- docs/api.md | 46 +++++++++++++++++++++++++++- docs/cli.md | 61 ++++++++++++++++++++++++++++++++++---- docs/en/_coverpage.md | 2 +- docs/en/api.md | 43 +++++++++++++++++++++++++++ docs/en/cli.md | 61 ++++++++++++++++++++++++++++++++++---- docs/en/install.md | 9 ++++-- docs/install.md | 9 ++++-- src/cli.cr | 50 +++++++++++++++++++++++-------- src/hpr/apis/repository.cr | 14 +++++++++ src/hpr/client.cr | 7 +++++ src/hpr/version.cr | 2 +- 12 files changed, 273 insertions(+), 33 deletions(-) diff --git a/docs/_coverpage.md b/docs/_coverpage.md index c6432eb..6bf3e27 100644 --- a/docs/_coverpage.md +++ b/docs/_coverpage.md @@ -1,6 +1,6 @@ ![logo](_media/icon.png) -# ḫpr 0.5.0 +# ḫpr 0.6.0 > 镜像任意 git 仓库到 gitlab 的同步工具,具有定时更新的功能。 diff --git a/docs/api.md b/docs/api.md index c3051fc..4e86417 100644 --- a/docs/api.md +++ b/docs/api.md @@ -55,6 +55,50 @@ GET /repositores } ``` +### 搜索镜像仓库 + +根据关键词搜索镜像仓库,只要关键词匹配到任意镜像仓库名的字符串均会命中。 + +``` +GET /repositores/search/?q=[query] +``` + +#### 参数 + +| 名称 | 类型 | 是否必须 | 描述 | +|---|---|---|---| +| q | String | true | 搜索关键词 | + +#### 返回样例 + +```json +{ + "total": 2, + "data": [ + { + "name": "coding-coding-docs", + "url": "https://git.coding.net/coding/coding-docs.git", + "mirror_url": "git@git.example.com:hpr-mirrors/coding-coding-docs.git", + "latest_version": "", + "status": "idle", + "created_at": "2018-03-23 16:27:59 +0800", + "updated_at": "2018-03-23 16:27:59 +0800", + "scheduled_at": "2018-03-23 17:28:02 +0800" + }, + { + "name": "spf13-viper", + "url": "https://github.com/spf13/viper.git", + "mirror_url": "git@git.example.com:hpr-mirrors/spf13-viper.git", + "latest_version": "v1.0.2", + "status": "idle", + "created_at": "2018-03-23 16:36:00 +0800", + "updated_at": "2018-03-23 16:36:00 +0800", + "scheduled_at": "2018-03-23 17:36:02 +0800" + } + ] +} +``` + ### 单个镜像仓库信息 ``` @@ -180,7 +224,7 @@ GET /info ```json { "hpr": { - "version": "0.5.0", + "version": "0.6.0", "repositroies": { "total": 2, "entry": [ diff --git a/docs/cli.md b/docs/cli.md index ed231d3..74ab4fd 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -5,7 +5,7 @@ hpr 本身是一个命令行工具,因此也提供了一个临时可用的辅 ## 运行 Web API 服务器 ```bash -$ hpr -s +$ hpr -s # 或者更改端口号 $ hpr -s --port 8848 _ @@ -23,17 +23,62 @@ $ hpr -s --port 8848 $ hpr -l # or $ hpr --list -2018-04-26 17:05:44 +08:00 INFO listing repositories (2): -* icyleaf-halite -* icyleaf-gitlab.cr +2018-04-28 18:01:32 +08:00 INFO listing repositories (2): + +=> Name: icyleaf-gitlab.cr + Path: /Users/icyleaf/data/repositories/icyleaf-gitlab.cr + OriginalUrl: https://github.com/icyleaf/gitlab.cr + MirrorUrl: git@git.example.com:hpr-mirrors/icyleaf-gitlab.cr.git + Status: idle + CreatedAt: 2018-04-26 17:05:44 +0800 + UpdatedAt: 2018-04-26 17:05:46 +0800 + ScheduledAt: 2018-04-29 05:05:46 +0800 + +=> Name: icyleaf-salt + Path: /Users/icyleaf/data/repositories/icyleaf-salt + OriginalUrl: https://github.com/icyleaf/salt.git + MirrorUrl: git@git.example.com:hpr-mirrors/icyleaf-salt.git + Status: idle + CreatedAt: 2018-04-28 18:00:56 +0800 + UpdatedAt: 2018-04-28 18:00:58 +0800 + ScheduledAt: 2018-05-01 06:00:58 +0800 +``` + +## 搜索镜像仓库 + +```bash +$ hpr -S icyleaf +# or +$ hpr --search icyleaf + +2018-04-28 18:07:34 +08:00 INFO searching repositories ... icyleaf +2018-04-28 18:07:34 +08:00 INFO found repositories (2): + +=> Name: icyleaf-gitlab.cr + Path: /Users/icyleaf/data/repositories/icyleaf-gitlab.cr + OriginalUrl: https://github.com/icyleaf/gitlab.cr + MirrorUrl: git@git.example.com:hpr-mirrors/icyleaf-gitlab.cr.git + Status: idle + CreatedAt: 2018-04-26 17:05:44 +0800 + UpdatedAt: 2018-04-26 17:05:46 +0800 + ScheduledAt: 2018-04-29 05:05:46 +0800 + +=> Name: icyleaf-salt + Path: /Users/icyleaf/data/repositories/icyleaf-salt + OriginalUrl: https://github.com/icyleaf/salt.git + MirrorUrl: git@git.example.com:hpr-mirrors/icyleaf-salt.git + Status: idle + CreatedAt: 2018-04-28 18:00:56 +0800 + UpdatedAt: 2018-04-28 18:00:58 +0800 + ScheduledAt: 2018-05-01 06:00:58 +0800 ``` ## 创建镜像仓库 ```bash -$ hpr --create --url https://github.com/icyleaf/salt.git icyleaf-salt +$ hpr -c -U https://github.com/icyleaf/salt.git icyleaf-salt # or -$ hpr -c -U https://github.com/icyleaf/salt.git +$ hpr --create --url https://github.com/icyleaf/salt.git 2018-04-26 17:04:39 +08:00 INFO creating repository ... ews-team/icyleaf-salt 2018-04-26 17:04:41 +08:00 INFO cloning https://github.com/icyleaf/salt.cr ... icyleaf-salt 2018-04-26 17:05:44 +08:00 INFO pushing to mirror ... icyleaf-salt @@ -44,6 +89,8 @@ $ hpr -c -U https://github.com/icyleaf/salt.git ```bash $ hpr -u icyleaf-salt +# or +$ hpr --update icyleaf-salt 2018-04-26 17:04:01 +08:00 INFO updating from origin ... icyleaf-salt 2018-04-26 17:04:06 +08:00 INFO pushing to mirror ... icyleaf-salt 2018-04-26 17:04:07 +08:00 INFO update repository ... done @@ -53,6 +100,8 @@ $ hpr -u icyleaf-salt ```bash $ hpr -d icyleaf-salt +# or +$ hpr --delete icyleaf-salt 2018-04-26 17:04:25 +08:00 INFO destroying project ... ews-team/icyleaf-salt 2018-04-26 17:04:25 +08:00 INFO deleting directory ... icyleaf-salt 2018-04-26 17:04:26 +08:00 INFO delete repository ... done diff --git a/docs/en/_coverpage.md b/docs/en/_coverpage.md index 1c03c7b..03f42b9 100644 --- a/docs/en/_coverpage.md +++ b/docs/en/_coverpage.md @@ -1,6 +1,6 @@ ![logo](../_media/icon.png) -# ḫpr 0.5.0 +# ḫpr 0.6.0 > Mirror git repositories to self-host gitlab services. diff --git a/docs/en/api.md b/docs/en/api.md index ddd8c15..c4117ce 100644 --- a/docs/en/api.md +++ b/docs/en/api.md @@ -55,6 +55,49 @@ GET /repositores } ``` +### Search repositories + +Search repositories by given query keywords, returns all include keyword matchs the name of mirrored reposotories. + +``` +GET /repositores/search/?q=[query] +``` + +#### Parameters + +| Name | Type | Required | Description | +|---|---|---|---| +| q | String | true | keyworkd | + +#### Example Response + +```json +{ + "total": 2, + "data": [ + { + "name": "coding-coding-docs", + "url": "https://git.coding.net/coding/coding-docs.git", + "mirror_url": "git@git.example.com:hpr-mirrors/coding-coding-docs.git", + "latest_version": "", + "status": "idle", + "created_at": "2018-03-23 16:27:59 +0800", + "updated_at": "2018-03-23 16:27:59 +0800", + "scheduled_at": "2018-03-23 17:28:02 +0800" + }, + { + "name": "spf13-viper", + "url": "https://github.com/spf13/viper.git", + "mirror_url": "git@git.example.com:hpr-mirrors/spf13-viper.git", + "latest_version": "v1.0.2", + "status": "idle", + "created_at": "2018-03-23 16:36:00 +0800", + "updated_at": "2018-03-23 16:36:00 +0800", + "scheduled_at": "2018-03-23 17:36:02 +0800" + } + ] +} +``` ### Get a repository info diff --git a/docs/en/cli.md b/docs/en/cli.md index 6b8df29..f1cce76 100644 --- a/docs/en/cli.md +++ b/docs/en/cli.md @@ -5,7 +5,7 @@ hpr is a command line tool, it also support some commands to manage mirror repos ## Run Web API server ```bash -$ hpr -s +$ hpr -s # Or customize server port $ hpr -s --port 8848 _ @@ -23,17 +23,62 @@ $ hpr -s --port 8848 $ hpr -l # or $ hpr --list -2018-04-26 17:05:44 +08:00 INFO listing repositories (2): -* icyleaf-halite -* icyleaf-gitlab.cr +2018-04-28 18:01:32 +08:00 INFO listing repositories (2): + +=> Name: icyleaf-gitlab.cr + Path: /Users/icyleaf/data/repositories/icyleaf-gitlab.cr + OriginalUrl: https://github.com/icyleaf/gitlab.cr + MirrorUrl: git@git.example.com:hpr-mirrors/icyleaf-gitlab.cr.git + Status: idle + CreatedAt: 2018-04-26 17:05:44 +0800 + UpdatedAt: 2018-04-26 17:05:46 +0800 + ScheduledAt: 2018-04-29 05:05:46 +0800 + +=> Name: icyleaf-salt + Path: /Users/icyleaf/data/repositories/icyleaf-salt + OriginalUrl: https://github.com/icyleaf/salt.git + MirrorUrl: git@git.example.com:hpr-mirrors/icyleaf-salt.git + Status: idle + CreatedAt: 2018-04-28 18:00:56 +0800 + UpdatedAt: 2018-04-28 18:00:58 +0800 + ScheduledAt: 2018-05-01 06:00:58 +0800 +``` + +## Search repositories + +```bash +$ hpr -S icyleaf +# or +$ hpr --search icyleaf + +2018-04-28 18:07:34 +08:00 INFO searching repositories ... icyleaf +2018-04-28 18:07:34 +08:00 INFO found repositories (2): + +=> Name: icyleaf-gitlab.cr + Path: /Users/icyleaf/data/repositories/icyleaf-gitlab.cr + OriginalUrl: https://github.com/icyleaf/gitlab.cr + MirrorUrl: git@git.example.com:hpr-mirrors/icyleaf-gitlab.cr.git + Status: idle + CreatedAt: 2018-04-26 17:05:44 +0800 + UpdatedAt: 2018-04-26 17:05:46 +0800 + ScheduledAt: 2018-04-29 05:05:46 +0800 + +=> Name: icyleaf-salt + Path: /Users/icyleaf/data/repositories/icyleaf-salt + OriginalUrl: https://github.com/icyleaf/salt.git + MirrorUrl: git@git.example.com:hpr-mirrors/icyleaf-salt.git + Status: idle + CreatedAt: 2018-04-28 18:00:56 +0800 + UpdatedAt: 2018-04-28 18:00:58 +0800 + ScheduledAt: 2018-05-01 06:00:58 +0800 ``` ## Create a new repository ```bash -$ hpr --create --url https://github.com/icyleaf/salt.git icyleaf-salt +$ hpr -c -U https://github.com/icyleaf/salt.git icyleaf-salt # or -$ hpr -c -U https://github.com/icyleaf/salt.git +$ hpr --create --url https://github.com/icyleaf/salt.git 2018-04-26 17:04:39 +08:00 INFO creating repository ... ews-team/icyleaf-salt 2018-04-26 17:04:41 +08:00 INFO cloning https://github.com/icyleaf/salt.cr ... icyleaf-salt 2018-04-26 17:05:44 +08:00 INFO pushing to mirror ... icyleaf-salt @@ -44,6 +89,8 @@ $ hpr -c -U https://github.com/icyleaf/salt.git ```bash $ hpr -u icyleaf-salt +# or +$ hpr --update icyleaf-salt 2018-04-26 17:04:01 +08:00 INFO updating from origin ... icyleaf-salt 2018-04-26 17:04:06 +08:00 INFO pushing to mirror ... icyleaf-salt 2018-04-26 17:04:07 +08:00 INFO update repository ... done @@ -53,6 +100,8 @@ $ hpr -u icyleaf-salt ```bash $ hpr -d icyleaf-salt +# or +$ hpr --delete icyleaf-salt 2018-04-26 17:04:25 +08:00 INFO destroying project ... ews-team/icyleaf-salt 2018-04-26 17:04:25 +08:00 INFO deleting directory ... icyleaf-salt 2018-04-26 17:04:26 +08:00 INFO delete repository ... done diff --git a/docs/en/install.md b/docs/en/install.md index 0a6399d..7704616 100644 --- a/docs/en/install.md +++ b/docs/en/install.md @@ -15,7 +15,7 @@ Check [Quick start](/en/quickstart.md)。 Pull specified version of hpr: ```bash -$ docker pull icyleafcn/hpr:0.5.0 +$ docker pull icyleafcn/hpr:0.6.0 ``` Or pull the latest version: @@ -95,6 +95,7 @@ Actions: -s, --server Run a web api server -l, --list List mirrored repositories + -S, --search Search mirrored repositories -c, --create Create a mirror repository -u, --update Updated a mirrored repository -d, --delete Delete a mirrored repository @@ -127,6 +128,10 @@ Examples: $ hpr -s --port 3001 + o Search all repositories include icyleaf keywords: + + $ hpr -S icyleaf + o Create a new repository: $ hpr -c --url https://github.com/icyleaf/hpr.git icyleaf-hpr @@ -145,5 +150,5 @@ Examples: More detail to check: https://icyleaf.github.io/hpr/ -hpr v0.5.0 in Crystal v0.24.2 +hpr v0.6.0 in Crystal v0.24.2 ``` diff --git a/docs/install.md b/docs/install.md index 178ab1f..bf85df2 100644 --- a/docs/install.md +++ b/docs/install.md @@ -15,7 +15,7 @@ hpr 使用 Crystal 编写的工具可以被安装在 macOS、Linux、树莓派 获取指定版本的 hpr: ```bash -$ docker pull icyleafcn/hpr:0.5.0 +$ docker pull icyleafcn/hpr:0.6.0 ``` 或者获取最新版本的 hpr: @@ -95,6 +95,7 @@ Actions: -s, --server Run a web api server -l, --list List mirrored repositories + -S, --search Search mirrored repositories -c, --create Create a mirror repository -u, --update Updated a mirrored repository -d, --delete Delete a mirrored repository @@ -127,6 +128,10 @@ Examples: $ hpr -s --port 3001 + o Search all repositories include icyleaf keywords: + + $ hpr -S icyleaf + o Create a new repository: $ hpr -c --url https://github.com/icyleaf/hpr.git icyleaf-hpr @@ -145,5 +150,5 @@ Examples: More detail to check: https://icyleaf.github.io/hpr/ -hpr v0.5.0 in Crystal v0.24.2 +hpr v0.6.0 in Crystal v0.24.2 ``` diff --git a/src/cli.cr b/src/cli.cr index 0033e3b..ad8d404 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -10,6 +10,7 @@ module Hpr None Server List + Search Create Update Delete @@ -30,6 +31,7 @@ module Hpr parser.separator("\nActions:\n") parser.on("-s", "--server", "Run a web api server") { @action = Action::Server } parser.on("-l", "--list", "List mirrored repositories") { @action = Action::List } + parser.on("-S", "--search", "Search mirrored repositories") { @action = Action::Search } parser.on("-c", "--create", "Create a mirror repository") { @action = Action::Create } parser.on("-u", "--update", "Updated a mirrored repository") { @action = Action::Update } parser.on("-d", "--delete", "Delete a mirrored repository") { @action = Action::Delete } @@ -60,6 +62,10 @@ module Hpr $ hpr -s --port 3001 + o Search all repositories include icyleaf keywords: + + $ hpr -S icyleaf + o Create a new repository: $ hpr -c --url https://github.com/icyleaf/hpr.git icyleaf-hpr @@ -97,6 +103,8 @@ EXAMPLES start_server when Action::List list_repositories + when Action::Search + search_repositories when Action::Create create_repository when Action::Update @@ -108,12 +116,24 @@ EXAMPLES private def list_repositories repositories = @client.list_repositories.each_with_object([] of Hash(String, String)) do |name, obj| - obj << Utils.repository_info(name) if Utils.repository_path?(name) + obj << Utils.repository_info(name) end Hpr.logger.info "listing repositories (#{repositories.size}):" - @client.list_repositories.each do |repository| - puts "* #{repository}" + repositories.each do |repo| + dump_repository(repo) + end + end + + private def search_repositories + Hpr.logger.info "searching repositories ... #{@repo_name}" + repositories = @client.search_repositories(@repo_name).each_with_object([] of Hash(String, String)) do |name, obj| + obj << Utils.repository_info(name) + end + + Hpr.logger.info "found repositories (#{repositories.size}):" + repositories.each do |repo| + dump_repository(repo) end end @@ -122,17 +142,9 @@ EXAMPLES @repo_name = Utils.project_name(@repo_url) if @repo_name.empty? if Utils.repository_path?(@repo_name) - project_path = Utils.repository_path(@repo_name) - project_info = Utils.repository_info(@repo_name) - Hpr.logger.info "repository exists ... #{@repo_name}" - puts "* path: #{project_path}" - puts "* original url: #{project_info["url"]}" - puts "* mirror url: #{project_info["mirror_url"]}" - puts "* status: #{project_info["status"]}" - puts "* created at: #{project_info["created_at"]}" - puts "* updated at: #{project_info["updated_at"]}" - puts "* scheduled at: #{project_info["scheduled_at"]}" + repo = Utils.repository_info(@repo_name) + dump_repository(repo) exit end @@ -180,6 +192,18 @@ EXAMPLES Hpr.logger.info "delete repository ... done" end + private def dump_repository(repo) + puts + puts "=> Name: #{repo["name"]}" + puts " Path: #{Utils.repository_path(repo["name"])}" + puts " OriginalUrl: #{repo["url"]}" + puts " MirrorUrl: #{repo["mirror_url"]}" + puts " Status: #{repo["status"]}" + puts " CreatedAt: #{repo["created_at"]}" + puts " UpdatedAt: #{repo["updated_at"]}" + puts " ScheduledAt: #{repo["scheduled_at"]}" + end + private def start_server print_banner start_worker diff --git a/src/hpr/apis/repository.cr b/src/hpr/apis/repository.cr index 5b8f037..9bd5d78 100644 --- a/src/hpr/apis/repository.cr +++ b/src/hpr/apis/repository.cr @@ -78,6 +78,20 @@ module Hpr::API::Repository true.to_json end + get "/repositories/search" do |env| + query = env.params.query["q"] + repositories = @@client.search_repositories(query).each_with_object([] of Hash(String, String)) do |name, obj| + obj << Utils.repository_info(name) + end + + env.response.content_type = "application/json" + env.response.status_code = 200 + { + total: repositories.size, + data: repositories + }.to_json + end + error 404 do |env| env.response.content_type = "application/json" { diff --git a/src/hpr/client.cr b/src/hpr/client.cr index 0c4a56e..df5c0c8 100644 --- a/src/hpr/client.cr +++ b/src/hpr/client.cr @@ -26,6 +26,13 @@ module Hpr end end + def search_repositories(query : String) + query = query.downcase + list_repositories.each_with_object([] of String) do |name, obj| + obj << name if name.downcase.includes?(query) + end + end + def create_repository(url : String, name : String? = nil, mirror_only = false) repo = Repository.new url project_name = (name && !name.empty?) ? name : repo.mirror_name diff --git a/src/hpr/version.cr b/src/hpr/version.cr index d1dd7f1..de48540 100644 --- a/src/hpr/version.cr +++ b/src/hpr/version.cr @@ -1,3 +1,3 @@ module Hpr - VERSION = "0.5.0" + VERSION = "0.6.0" end