pixab 1.9.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aefa3c5d3ed4fd4331affe5c8cc8523ed0403efb88aa98d78257617a4973ea89
4
- data.tar.gz: 01df65aa135899324a2819dc0a85bd2b5fdc2196862d445180c73fd45c677c82
3
+ metadata.gz: 6e343b6bb118ca7dea7ba7a5f1a09f925c662b60104c3cc1cec9f0903e257857
4
+ data.tar.gz: d05148fe7149a47ae7c06eeec57de5276c2f2ded8148a2bb675a72cc4d216eda
5
5
  SHA512:
6
- metadata.gz: c8370d3b0c946b4603104da4f1795ec34af95175a3241a1cb203b1d45b38292ec122aecbf75639259b1d91bc7b72ec21ff66d96190d2e01765bc8f17529ddb5e
7
- data.tar.gz: 856d429275f5217345ea86dcdc5c59be67b8fdc2ab9d1c641f550b407362763582dfdea098e54576613da728d3faf6ecc202926bfa724f90ea21046b0b2634d5
6
+ metadata.gz: bd5d261253f3b36eb5b5f7b4f7123ddfd922e36b8dfc5bab1afceebda3ea0ceec81a9f86a566f3ba7def345263b147b9dd9a0676d7fcfdbd9b38337a370f827c
7
+ data.tar.gz: c71932134caaa9fc477f1a8e4186ddd6758905b27970af104b3aef0e4f44d8e704c6e3507a896f6dd87d7e571f5db8563048b39a4abaec05a8320ec63cd0746a
data/exe/pixab CHANGED
@@ -11,6 +11,7 @@ require_relative '../lib/LocalizationSmartcat.rb'
11
11
  require_relative '../lib/LocalizationSmartcatImport.rb'
12
12
  require_relative '../lib/LocalizationSmartcatMerge.rb'
13
13
  require_relative '../lib/mbox/Mbox.rb'
14
+ require_relative '../lib/gitlab/GitlabMRManager.rb'
14
15
 
15
16
  case ARGV[0]
16
17
  when '--version'
@@ -55,6 +56,8 @@ when 'localize'
55
56
  end
56
57
  when 'mbox'
57
58
  Pixab::Mbox.new.run(ARGV[1..-1])
59
+ when 'mr'
60
+ Pixab::GitlabMRManager.new.run(ARGV[1..-1])
58
61
  else
59
62
  puts "Invalid command".red
60
63
  end
data/lib/GitUtils.rb CHANGED
@@ -99,7 +99,11 @@ module Pixab
99
99
 
100
100
  # 获取当前分支的远程分支
101
101
  def current_remote_branch
102
- local_branch = current_branch
102
+ return remote_branch(current_branch)
103
+ end
104
+
105
+ # 获取指定分支的远程分支
106
+ def remote_branch(local_branch)
103
107
  return nil if local_branch.nil?
104
108
 
105
109
  remote_branch, status = Open3.capture2("git for-each-ref --format='%(upstream:short)' refs/heads/#{local_branch}")
data/lib/Platform.rb ADDED
@@ -0,0 +1,21 @@
1
+ module Pixab
2
+
3
+ class Platform
4
+
5
+ attr_reader :platform
6
+
7
+ def initialize(platform)
8
+ @platform = platform
9
+ end
10
+
11
+ def is_iOS
12
+ platform == 'iOS'
13
+ end
14
+
15
+ def is_android
16
+ platform == 'android'
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,58 @@
1
+ require 'colored2'
2
+ require_relative '../GitUtils.rb'
3
+
4
+ module Pixab
5
+
6
+ class GitRepoInfo
7
+
8
+ attr_reader :branch, :remote_branch, :target_branch, :remote_target_branch
9
+
10
+ def initialize
11
+ if !GitUtils.is_git_repo
12
+ puts "当前目录不是 Git 仓库!, path: #{Dir.pwd}".red
13
+ return
14
+ end
15
+
16
+ `git fetch origin`
17
+
18
+ @branch = GitUtils.current_branch
19
+ @target_branch = get_target_branch(@branch)
20
+ @remote_branch = GitUtils.remote_branch(@branch)
21
+ @remote_target_branch = GitUtils.remote_branch(@target_branch)
22
+ end
23
+
24
+ def get_target_branch(branch)
25
+ splited_branchs = branch.split('/')
26
+ feature_name = splited_branchs[-1]
27
+ version = get_version(feature_name)
28
+
29
+ if version.nil?
30
+ puts "无法获取目标分支,分支:#{branch}".red
31
+ return
32
+ end
33
+
34
+ if splited_branchs.length > 1
35
+ return "#{splited_branchs[0]}/#{version}"
36
+ else
37
+ return version
38
+ end
39
+ end
40
+
41
+ def get_version(feature_name)
42
+ parts = feature_name.split('_')
43
+ parts.map do |part|
44
+ is_numeric = part.match?(/^\d+$/) # 检查是否为纯数字
45
+ if is_numeric
46
+ return part
47
+ end
48
+ end
49
+ end
50
+
51
+ # 判断远程分支是否代码是否同步到远程目标分支
52
+ def is_remote_branch_synced
53
+ GitUtils.is_branch_synced(@remote_branch, @remote_target_branch)
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,63 @@
1
+ module Pixab
2
+
3
+ class GitlabInfo
4
+
5
+ # Gitlab API endpoint
6
+ API_ENDPOINT = 'https://techgit.meitu.com/api/v4'
7
+
8
+ # Gitlab private token
9
+ TOKEN = 'glpat-BZWgys9gsAQ8mnGYTajr'
10
+
11
+ end
12
+
13
+ class << GitlabInfo
14
+
15
+ def iOS_reviewers
16
+ ['廖再润', '吴子琰', '黄丕臻', '唐竞全', '杜思保', '黄子汕', '庄善志']
17
+ end
18
+
19
+ def android_reviewers
20
+ ['朱乾', '张珂', '林朴', '容子钧', '李明杰', '李明邦', '陈全满']
21
+ end
22
+
23
+ def iOS_reviewer_id(reviewer)
24
+ case reviewer
25
+ when '吴子琰'
26
+ 979
27
+ when '黄丕臻'
28
+ 1385
29
+ when '廖再润'
30
+ 1569
31
+ when '唐竞全'
32
+ 1807
33
+ when '杜思保'
34
+ 1922
35
+ when '黄子汕'
36
+ 1926
37
+ when '庄善志'
38
+ 2056
39
+ end
40
+ end
41
+
42
+ def android_reviewer_id(reviewer)
43
+ case reviewer
44
+ when '林朴'
45
+ 1139
46
+ when '朱乾'
47
+ 1284
48
+ when '张珂'
49
+ 1993
50
+ when '陈全满'
51
+ 2031
52
+ when '容子钧'
53
+ 2073
54
+ when '李明杰'
55
+ 2130
56
+ when '李明邦'
57
+ 2213
58
+ end
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,114 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'colored2'
5
+ require_relative './GitlabInfo.rb'
6
+ require_relative './GitRepoInfo.rb'
7
+
8
+ module Pixab
9
+
10
+ class GitlabMR
11
+
12
+ def run(path, reviewers_ids)
13
+ unless Dir.exist?(path)
14
+ puts "当前目录不存在: #{path}".red
15
+ return
16
+ end
17
+
18
+ Dir.chdir(path) do
19
+ create_merge_request(path, reviewers_ids)
20
+ end
21
+ end
22
+
23
+ # 创建 Merge Request 方法
24
+ def create_merge_request(path, reviewers_ids)
25
+ # 获取项目 ID
26
+ project_id = get_project_id
27
+ if project_id.nil?
28
+ return
29
+ end
30
+
31
+ # 获取 git 分支信息
32
+ git_repo_info = GitRepoInfo.new
33
+ if git_repo_info.branch.nil?
34
+ return
35
+ end
36
+
37
+ if git_repo_info.is_remote_branch_synced # 远程分支代码已经同步到远程目标分支,无需创建 Merge Request
38
+ puts "#{File.basename(path)}:代码已同步。\n".green
39
+ return
40
+ end
41
+
42
+ title = "Merge branch #{git_repo_info.branch} into #{git_repo_info.target_branch}."
43
+
44
+ uri = URI("#{GitlabInfo::API_ENDPOINT}/projects/#{project_id}/merge_requests")
45
+
46
+ # 构建 HTTP 请求
47
+ request = Net::HTTP::Post.new(uri)
48
+ request['PRIVATE-TOKEN'] = GitlabInfo::TOKEN
49
+ request['Content-Type'] = 'application/json'
50
+
51
+ body = {
52
+ source_branch: git_repo_info.branch,
53
+ target_branch: git_repo_info.target_branch,
54
+ title: title,
55
+ }
56
+
57
+ if reviewers_ids.length > 0
58
+ body[:assignee_id] = reviewers_ids[0]
59
+ end
60
+
61
+ if reviewers_ids.length > 1
62
+ body[:reviewer_ids] = reviewers_ids[1..-1]
63
+ end
64
+
65
+ # 设置请求体
66
+ request.body = body.to_json
67
+
68
+ # 发送请求
69
+ http = Net::HTTP.new(uri.hostname, uri.port)
70
+ http.use_ssl = (uri.scheme == 'https') # 如果是 HTTPS,启用 SSL
71
+ response = http.request(request)
72
+
73
+ # 处理响应
74
+ if response.is_a?(Net::HTTPSuccess)
75
+ puts "#{File.basename(path)}:MR已创建,#{JSON.parse(response.body)['web_url']}\n".green
76
+ else
77
+ puts "创建 Merge Request 失败!".red
78
+ puts "错误信息: #{response.body}".red
79
+ end
80
+
81
+
82
+ end
83
+
84
+ # 获取项目 ID 方法
85
+ def get_project_id
86
+ git_url = `git remote get-url origin`.strip
87
+
88
+ project_path = nil
89
+ if git_url =~ %r{(?:https?:\/\/.*?\.com\/|git@.*?:)(.+?)(\.git)?$}
90
+ project_path = $1
91
+ else
92
+ puts "无法解析地址: #{git_url}".red
93
+ end
94
+
95
+ uri = URI("#{GitlabInfo::API_ENDPOINT}/projects/#{URI.encode_www_form_component(project_path)}")
96
+ request = Net::HTTP::Get.new(uri)
97
+ request['PRIVATE-TOKEN'] = GitlabInfo::TOKEN
98
+
99
+ http = Net::HTTP.new(uri.hostname, uri.port)
100
+ http.use_ssl = (uri.scheme == 'https') # 如果是 HTTPS,启用 SSL
101
+ response = http.request(request)
102
+
103
+ if response.is_a?(Net::HTTPSuccess)
104
+ data = JSON.parse(response.body)
105
+ return data['id']
106
+ else
107
+ puts "获取项目 ID 失败!错误信息: #{response.body}".red
108
+ return nil
109
+ end
110
+ end
111
+
112
+ end
113
+
114
+ end
@@ -0,0 +1,72 @@
1
+ require 'colored2'
2
+ require_relative '../Platform.rb'
3
+ require_relative './GitlabInfo.rb'
4
+ require_relative '../Utilities.rb'
5
+ require_relative './GitlabMR.rb'
6
+
7
+ module Pixab
8
+
9
+ class GitlabMRManager
10
+
11
+ def run(commands = nil)
12
+ platform = nil
13
+
14
+ if not commands.nil?
15
+ commands.each_index do |index|
16
+ command = commands[index]
17
+ case command
18
+ when '--android'
19
+ platform = Platform.new('android')
20
+ when '--iOS'
21
+ platform = Platform.new('iOS')
22
+ end
23
+ end
24
+ end
25
+
26
+ if platform.nil?
27
+ platform = Platform.new('iOS')
28
+ end
29
+
30
+ git_repos = Pixab::GitlabMRManager.new.find_git_repos(Dir.pwd)
31
+
32
+ if git_repos.empty?
33
+ puts "No Git repositories found.".red
34
+ return
35
+ end
36
+
37
+ is_iOS = platform.is_iOS
38
+ reviewers = is_iOS ? GitlabInfo.iOS_reviewers : GitlabInfo.android_reviewers
39
+ selected_reviewers = Utilities.display_choose_list(reviewers, nil, "审核人员", "请选择审核人员:", nil, nil, true)
40
+
41
+ if selected_reviewers.empty?
42
+ puts "未选择审核人员".red
43
+ return
44
+ end
45
+
46
+ reviewer_ids = []
47
+ if is_iOS
48
+ selected_reviewers.each do |reviewer|
49
+ reviewer_ids << GitlabInfo.iOS_reviewer_id(reviewer)
50
+ end
51
+ else
52
+ selected_reviewers.each do |reviewer|
53
+ reviewer_ids << GitlabInfo.android_reviewer_id(reviewer)
54
+ end
55
+ end
56
+
57
+ gitlab_mr = GitlabMR.new
58
+ git_repos.each do |repo|
59
+ gitlab_mr.run(repo, reviewer_ids)
60
+ end
61
+
62
+ end
63
+
64
+ # 查找包含 Git 仓库的目录
65
+ def find_git_repos(root_dir)
66
+ # 使用 Dir.glob 匹配所有包含 .git 子目录的目录
67
+ Dir.glob("#{root_dir}/**/.git").map { |git_dir| File.dirname(File.expand_path(git_dir)) }
68
+ end
69
+
70
+ end
71
+
72
+ end
data/lib/pixab/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pixab
4
- VERSION = "1.9.2"
4
+ VERSION = "2.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixab
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 廖再润
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-09 00:00:00.000000000 Z
11
+ date: 2024-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colored2
@@ -91,8 +91,13 @@ files:
91
91
  - lib/LocalizationSmartcatMerge.rb
92
92
  - lib/MergeRequest.rb
93
93
  - lib/Package.rb
94
+ - lib/Platform.rb
94
95
  - lib/RepoManager.rb
95
96
  - lib/Utilities.rb
97
+ - lib/gitlab/GitRepoInfo.rb
98
+ - lib/gitlab/GitlabInfo.rb
99
+ - lib/gitlab/GitlabMR.rb
100
+ - lib/gitlab/GitlabMRManager.rb
96
101
  - lib/mbox/AirBrushProjectInfo.rb
97
102
  - lib/mbox/Mbox.rb
98
103
  - lib/mbox/MboxAdd.rb