#!/usr/bin/env ruby require 'rest_client' require 'json' module HelixTeamHub class Client def initialize(endpoint, account_key, company_key) @endpoint = endpoint @account_key = account_key @company_key = company_key end #debug def debug() puts @endpoint.inspect puts @company_key.inspect puts @account_key.inspect end #Helper methods def auth_header %(hth.company_key="#{@company_key}",account_key="#{@account_key}") end def print_repo(repo) puts repo["id"] puts "=" * 20 puts "Type: #{repo["type"]}" puts "SSH access: #{repo["ssh_url"]}" puts "HTTP(s) access: #{repo["http_url"]}" puts end #Standard methods #A method to create a wiki def create_wiki(project_id) raise "No project given" if project_id.nil? #Send a post request to HTH API to create the wiki response = RestClient.post "#{@endpoint}/projects/#{project_id}/repositories", {id: "wiki", type: "git", "properties": { "kind": "wiki" }}.to_json, authorization: auth_header(), content_type: :json, accept: :json end def list_repos(project_id) raise "No project given" if project_id.nil? #Make an authorization request to HTH #Include the authorization header and the Accept header so we can get data back response = RestClient.get "#{@endpoint}/projects/#{project_id}/repositories", authorization: auth_header(), accept: :json JSON.parse(response)["results"].each do |repo| print_repo(repo) end end def create_repo(project_id, repo_id, repo_type) raise "No project given" if project_id.nil? raise "No repository given" if repo_id.nil? raise "No repository type given" if repo_type.nil? raise "Invalid repository type" unless %w(git mercurial subversion).include?(repo_type) #Send a post request to HTH API with the details of the repo we will create response = RestClient.post "#{@endpoint}/projects/#{project_id}/repositories", {repository: {id: repo_id, type: repo_type}}.to_json, authorization: auth_header(), content_type: :json, accept: :json print_repo(JSON.parse(response)) end def delete_repo(project_id, repo_id) raise "No project given" if project_id.nil? raise "No repository" if repo_id.nil? #We are not sending content and don't care about the response, so just include the auth #header RestClient.delete "#{@endpoint}/projects/#{project_id}/repositories/#{repo_id}", authorization: auth_header() puts "Deleted" end end #End class end command, project_id, repo_id, repo_type = ARGV client = HelixTeamHub::Client.new(ENV['HTH_ENDPOINT'], ENV['HTH_ACCOUNT_KEY'], ENV['HTH_COMPANY_KEY']) case command when 'list' then client.list_repos(project_id) when 'create' then client.create_repo(project_id, repo_id, repo_type) when 'delete' then client.delete_repo(project_id, repo_id) when 'debug' then client.debug() when 'wiki' then client.create_wiki(project_id) else puts "Unknown command: #{command}" end