stash-clone-tool 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8a5b16ad7b2f2453bda2a6df8f5f598f28dfd62c
4
+ data.tar.gz: 8f0051adefd7fd7d49ae4505379935807205b3cd
5
+ SHA512:
6
+ metadata.gz: 17681e71932ff4700ac63cccddbaa697c8a3ca6833045abfa217bfead859335aae9a3176efa99a87dc1cb0207ddc047d196c719c35896429150c74201dfa6175
7
+ data.tar.gz: 4c6a3764529733a25d2e27cd556a9c7f81256b7bdfceb85d161bca808d0c97516904760754e2ce90678798f5c7e6cc415c788d20ff166d5caa8f2000eb4b4ab4
@@ -0,0 +1,7 @@
1
+ /.idea
2
+ /.bundle/
3
+ /Gemfile.lock
4
+ /doc/
5
+ /pkg/
6
+ /spec/reports/
7
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README ADDED
@@ -0,0 +1,5 @@
1
+ Clones all repositories in all projects a user has access to from a stash server.
2
+
3
+ Simple to use, enter the stash url, username and password.
4
+
5
+ Feel free to contribute at https://github.com/paul-ridgway/stash-clone-tool.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'release_tool/release_tool'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ require 'bundler/setup'
2
+ require 'stash_clone_tool/stash_cloner'
3
+ require 'optparse'
4
+ require 'highline/import'
5
+ options = {directory: '.'}
6
+ cli = OptionParser.new do |opts|
7
+ opts.banner = 'Usage: stash-clone-tool [options]'
8
+ opts.on('-s', '--stash URL', 'Stash Server URL (required)') do |stash_url|
9
+ options[:stash_url] = stash_url
10
+ end
11
+ opts.on('-u', '--username USERNAME', 'Stash username (required)') do |username|
12
+ options[:username] = username
13
+ end
14
+ opts.on('-p', '--password PASSWORD', 'Stash password') do |password|
15
+ options[:password] = password
16
+ end
17
+ opts.on('-d', '--directory DIRECTORY_PATH', 'Path to clone stash projects into (default: working directory).') do |directory|
18
+ options[:directory] = directory
19
+ end
20
+ end.parse!
21
+
22
+ fail 'Must specify Stash server URL (-s).' if options[:stash_url].nil?
23
+ fail 'Must specify Stash username (-u).' if options[:username].nil?
24
+
25
+ # Prompt for password if not provided on CLI
26
+ options[:password] ||= ask('Password: ') { |q| q.echo = '*' }
27
+
28
+ # TODO: Write CLI
29
+ cloner = StashCloneTool::StashCloner.new(options[:stash_url], options[:username], options[:password], options[:directory])
30
+ cloner.go
@@ -0,0 +1,69 @@
1
+ # Stash Cloner
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'colorize'
5
+ require 'stash_clone_tool/stash_exception'
6
+ module StashCloneTool
7
+ class StashCloner
8
+
9
+ def initialize(stash_url, username, password, directory)
10
+ @stash_url = stash_url
11
+ @username = username
12
+ @password = password
13
+ @directory = directory
14
+ end
15
+
16
+ def go
17
+ get_projects['values'].each do |project|
18
+ process_project(project)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def process_project(prj)
25
+ puts " - #{prj['name']}...".light_yellow
26
+ project = request("/rest/api/1.0/projects/#{prj['key']}/repos")
27
+ project['values'].each do |repo|
28
+ process_repo(prj['key'], repo)
29
+ end
30
+ end
31
+
32
+ def process_repo(project_key, repo)
33
+ puts " - #{repo['name']}...".light_yellow
34
+ clone_links = repo['links']['clone']
35
+ clone_link = clone_links.select { |l| l['name'] == 'ssh' }.first['href']
36
+ folder = File.join(@directory, project_key, repo['slug'])
37
+ git_command = "git clone #{clone_link} #{folder}"
38
+ puts " Cloning ".light_blue + clone_link.light_white + " to ".light_blue + folder.light_white
39
+ if Dir.exists?(folder)
40
+ puts " Target already exists".light_red
41
+ else
42
+ puts " #{git_command}".light_green
43
+ puts
44
+ system(git_command)
45
+ puts
46
+ end
47
+ end
48
+
49
+ def request(url)
50
+ uri = URI("#{@stash_url}#{url}")
51
+ req = Net::HTTP::Get.new(uri.to_s)
52
+ req.basic_auth @username, @password
53
+ response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
54
+ http.request(req)
55
+ end
56
+ json = JSON.parse(response.body)
57
+ if json['errors']
58
+ error_msg = json['errors'].map { |e| e['message'] }.join('. ')
59
+ raise StashException.new(error_msg)
60
+ end
61
+ json
62
+ end
63
+
64
+ def get_projects
65
+ request('/rest/api/1.0/projects?limit=100')
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,4 @@
1
+ module StashCloneTool
2
+ class StashException < Exception
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ # Version
2
+ module StashCloneTool
3
+ VERSION = '1.0.0'
4
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stash_clone_tool/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'stash-clone-tool'
8
+ spec.version = StashCloneTool::VERSION
9
+ spec.authors = ['Paul Ridgway']
10
+ spec.email = ['paul@thefloow.com']
11
+
12
+ spec.summary = 'Tool for cloning all projects from a Stash server.'
13
+ spec.description = 'Tool for cloning all projects from a Stash server.'
14
+ spec.homepage = 'https://github.com/paul-ridgway/stash-clone-tool'
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ else
21
+ fail 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = 'bin'
26
+ spec.executables = ['stash-clone-tool']
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.10'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_dependency 'colorize'
32
+ spec.add_dependency 'highline'
33
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stash-clone-tool
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Ridgway
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: highline
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Tool for cloning all projects from a Stash server.
70
+ email:
71
+ - paul@thefloow.com
72
+ executables:
73
+ - stash-clone-tool
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - README
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - bin/stash-clone-tool
85
+ - lib/stash_clone_tool/stash_cloner.rb
86
+ - lib/stash_clone_tool/stash_exception.rb
87
+ - lib/stash_clone_tool/version.rb
88
+ - stash-clone-tool.gemspec
89
+ homepage: https://github.com/paul-ridgway/stash-clone-tool
90
+ licenses: []
91
+ metadata:
92
+ allowed_push_host: 'TODO: Set to ''http://mygemserver.com'''
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.14
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Tool for cloning all projects from a Stash server.
113
+ test_files: []