Class: TYCiCore::GitlabODM

Inherits:
Object
  • Object
show all
Defined in:
lib/tuya/ci/core/git/gitlab/gitlab_odm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, path, visibility, username, password, email) ⇒ GitlabODM

Returns a new instance of GitlabODM.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 6

def initialize (config, path, visibility, username, password, email)
	@config = config
	@path = path
	@visibility = visibility

	@username = username
	@password = password
	@email = email

	@group_id = ""
	@user_id = ""

	@hook_project_ios = ""
	@hook_project_android = ""
	@hook_project_id_ios = ""
	@hook_project_id_android = ""
	@hook_project_url_ios = ""
	@hook_project_url_android = ""
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 4

def config
  @config
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 4

def name
  @name
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 4

def path
  @path
end

#visibilityObject

Returns the value of attribute visibility.



4
5
6
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 4

def visibility
  @visibility
end

Instance Method Details

#add_project_groupObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 90

def add_project_group

	@hook_project_android = "#{path}Repository"
	@hook_project_ios = "#{path}Specs"

	@hook_project_url_ios = "#{@config.host}/#{path}/#{@hook_project_ios}.git"
	@hook_project_url_android = "#{@config.host}/#{path}/#{@hook_project_android}.git"

	TYCiCore::Git.git_create_empty_repos @hook_project_ios, @hook_project_url_ios
	TYCiCore::Git.git_create_empty_repos @hook_project_android, @hook_project_url_android
end

#add_project_hook(id, url) ⇒ Object



125
126
127
128
129
130
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 125

def add_project_hook(id, url)
	puts "ODM add hook url:#{url} to project #{id}".green
	result = `curl #{@config.host}/api/v4/projects/#{id}/hooks?private_token=#{@config.private_token} -X POST -d "id=#{id}&url=#{url}&push_events=true"`
	json = JSON result
	puts json
end

#add_user_groupObject



80
81
82
83
84
85
86
87
88
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 80

def add_user_group
	puts 'ODM add user to group ...'.green
	result = `curl #{@config.host}/api/v4/groups/#{@group_id}/members?private_token=#{@config.private_token} -X POST -d "id=#{@group_id}&user_id=#{@user_id}&access_level=50"`
	json = JSON result

	result_bool = not_empty?(json["state"]) && not_empty?(json["username"])
	puts json unless result_bool
	result_bool
end

#create_groupObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 57

def create_group
	puts 'ODM create group ...'.green
	result = `curl #{@config.host}/api/#{@config.version}/groups -X POST -d "private_token=#{@config.private_token}&name=#{@path}&path=#{@path}&visibility=#{@visibility}"`
	json = JSON result
	@group_id = json["id"]

	result_bool = not_empty?(json["name"]) && not_empty?(json["web_url"])
	puts json unless result_bool
	result_bool
end

#create_userObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 68

def create_user
	puts 'ODM create user ...'.green
	result = `curl #{@config.host}/api/v4/users?private_token=#{@config.private_token} -X POST -d "name=#{@username}&username=#{@username}&password=#{@password}&email=#{@email}&skip_confirmation=true"`

	json = JSON result
	@user_id = json["id"]

	result_bool = not_empty?(json["state"]) && not_empty?(json["created_at"])
	puts json unless result_bool
	result_bool
end

#get_project_idObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 102

def get_project_id
	puts 'ODM get project id in group'.green

	result = `curl #{@config.host}/api/v4/groups/#{@group_id}/projects?private_token=#{@config.private_token}&id=#{@group_id}`

	json = JSON result

	get_project_id_temp json, 0
	get_project_id_temp json, 1

	puts "Project iOS id is: #{@hook_project_id_ios}, android id is #{@hook_project_id_android}"
end

#get_project_id_temp(json, index) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 115

def get_project_id_temp(json, index)
	temp_name = json[index]['name']
	temp_id = json[index]['id']
	if temp_name == @hook_project_android
		@hook_project_id_android = temp_id
	else
		@hook_project_id_ios = temp_id
	end
end

#not_empty?(str) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 132

def not_empty?(str)
	if str.nil?
		return false
	else
		if str.empty?
			return false
		end
		return true
	end
end

#odmObject



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
# File 'lib/tuya/ci/core/git/gitlab/gitlab_odm.rb', line 26

def odm

	if create_group
		if create_user
			if add_user_group
				add_project_group
				get_project_id
				add_project_hook @hook_project_id_ios, @config.hook_ios
				add_project_hook @hook_project_id_android, @config.hook_android

				TYCiCore::Git.git_delete_temp_path @hook_project_ios
				TYCiCore::Git.git_delete_temp_path @hook_project_android

				puts "odm create gitlab group json is:[start:success:end]".green
			else
				puts 'ODM add user to group failed'.red
				puts "odm create gitlab group json is:[start:failed:end]".red
			end
		else
			puts 'ODM create user failed'.red
			puts "odm create gitlab group json is:[start:failed:end]".red
			return false
		end
	else
		puts 'ODM create group failed'.red
		puts "odm create gitlab group json is:[start:failed:end]".red
		return false
	end

end