class TYCiCore::GitlabODM

Attributes

config[RW]
name[RW]
path[RW]
visibility[RW]

Public Class Methods

new(config, path, visibility, username, password, email) click to toggle source
# 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

Public Instance Methods

add_project_group() click to toggle source
# 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) click to toggle source
# 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_group() click to toggle source
# 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_group() click to toggle source
# 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_user() click to toggle source
# 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_id() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
odm() click to toggle source
# 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