ghsync 0.1.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 835ab5a5c960c7784585e9e198f29570b68f8aee
4
+ data.tar.gz: 0518600c222d945ccb934f0837158449b7acf8e3
5
+ SHA512:
6
+ metadata.gz: af60824868973df6b972e35ffb04debbc77af55ecc8ee9609b383e79cedce72669779a71aaddd303e30382ff5079685ccd36d5002af8f31a2a1a7166f8398e5b
7
+ data.tar.gz: 2f73fd90aa627d0140c6716f2805526f1cbef6ab493bf0be9875362cb96a01d4a934385707a5ad5e68b3c97522ecde322fa40badb63d32df17a117a7c54c610e
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at brimil01@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ghsync.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Brian Miller
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,90 @@
1
+ # Ghsync - The GitHub Repository Syncronizer
2
+
3
+ Sync your github organization repos in one command. It clones missing repositories and fetches existing one. Makes joining a new organization a snap!
4
+
5
+ ## Installation
6
+
7
+ You can easily install `ghsync` with the following command:
8
+
9
+ $ gem install ghsync
10
+
11
+ ## Configuration
12
+
13
+ Your ghsync configuration should be saved to ~/.ghsync/config.json.
14
+ ```json
15
+ {
16
+ "base_path": "~/dev",
17
+ "username": "<github username>",
18
+ "password": "<github password>",
19
+ "organizations": [
20
+ {
21
+ "name": "codebreakdown",
22
+ "exclude": [],
23
+ "base_path": "~/dev/codebreakdown"
24
+ }
25
+ ],
26
+ "repositories": [
27
+ {
28
+ "owner": "codebreakdown",
29
+ "name": "ghsync"
30
+ }
31
+ ]
32
+ }
33
+ ```
34
+
35
+ ### GitHub Authentication
36
+
37
+ #### Multi-Factor Authentication
38
+
39
+ Sadly, at the moment `ghsync` doesn't support GitHub's Multi-Factor
40
+ Authentication.
41
+
42
+ #### OAuth
43
+
44
+ It is also lacking support for GitHub's OAuth mechanism.
45
+
46
+ #### HTTP Basic Auth
47
+
48
+ The HTTP Basic Auth will work as long as you don't have multi-factor
49
+ authentication enabled for your account.
50
+
51
+ #### Personal Access Token
52
+
53
+ Personal Access Token authentication is currently supported and this is the
54
+ recommended authentication mechanism to use right now. It is the only
55
+ authentication mechanism you can use at the moment if you have multi-factor
56
+ authentication enabled.
57
+
58
+ Simply go to your GitHub **Account Settings**, select **Applications**, click
59
+ the **Create new token** button in the **Personal Access Token** section. Give
60
+ it the name "GHsync" and submit. This will generate your personal access token.
61
+ Then simply put your personal access token in the `~/.ghsync/config.json` as your GitHub
62
+ username and "x-oauth-basic" as your GitHub password.
63
+
64
+ ## Usage
65
+
66
+ Once you have configured `ghsync` as described above you can launch it by simply
67
+ running the following command:
68
+
69
+ ghsync
70
+
71
+ Once it launches, it will use the information provided in the
72
+ `~/.ghsync/config.json` configuration file to fetch all the configured
73
+ repositories and either fetch them if they exist, or clone them if they are
74
+ missing.
75
+
76
+ ## Development
77
+
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
79
+
80
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/codebreakdown/ghsync. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
85
+
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
90
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ghsync"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift ::File.expand_path(::File.dirname(__FILE__) + '/../lib')
4
+
5
+ require 'rubygems'
6
+
7
+ require 'ghsync/app'
8
+
9
+ app = Ghsync::App.new
10
+ app.run
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ghsync/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ghsync"
8
+ spec.version = Ghsync::VERSION
9
+ spec.authors = ["Brian Miller"]
10
+ spec.email = ["brimil01@gmail.com"]
11
+
12
+ spec.summary = %q{Sync github repositories}
13
+ spec.description = %q{Sync github repositories}
14
+ spec.homepage = "https://github.com/codebreakdown/ghsync"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "octokit", "~> 4.3"
31
+ spec.add_development_dependency "bundler", "~> 1.12"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "pry"
35
+ end
@@ -0,0 +1,5 @@
1
+ require "ghsync/version"
2
+
3
+ module Ghsync
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'config'
2
+ require_relative 'repo_sync'
3
+ require 'pry'
4
+
5
+ module Ghsync
6
+ class App
7
+ def run
8
+ config = Config.load_config
9
+ repo_syncer = RepoSync.new(config)
10
+ repo_syncer.sync
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,83 @@
1
+ require 'json'
2
+
3
+ module Ghsync
4
+ class Config
5
+ attr_reader :organizations, :repositories, :username, :password, :base_path
6
+
7
+ # Organization config
8
+ # {
9
+ # "name": "org",
10
+ # "exclude": ["repo"],
11
+ # "base_path": "org" #optional
12
+ # }
13
+ # Repository config
14
+ # {
15
+ # "owner": "owner",
16
+ # "name": "repo",
17
+ # "base_path": "repo" #optional
18
+ # }
19
+ def initialize(config)
20
+ @organizations = config["organizations"] || []
21
+ @repositories = config["repositories"] || []
22
+ @username = config["username"]
23
+ @password = config["password"]
24
+ @base_path = config["base_path"]
25
+ import_from_pra if config["use_pra"]
26
+ end
27
+
28
+ def self.create(import_from_pra: true)
29
+ end
30
+
31
+ def self.import_from_pra
32
+ pra_config = Config.parse_pra_config_file
33
+ github_config = pra_config["pull_sources"].select {|source| source["type"] == "github"}.first
34
+ unless github_config.nil?
35
+ @organizations += github_config["config"]["organizations"]
36
+ @repositories += github_config["config"]["repositories"]
37
+ @username ||= github_config["config"]["username"]
38
+ @password ||= github_config["config"]["password"]
39
+ end
40
+ end
41
+
42
+ def self.load_config
43
+ return self.new(self.parse_config_file)
44
+ end
45
+
46
+ def self.parse_config_file
47
+ self.json_parse(self.read_config_file(config_path))
48
+ end
49
+
50
+ def self.parse_pra_config_file
51
+ self.json_parse(self.read_config_file(pra_config_path))
52
+ end
53
+
54
+ def self.read_config_file(path)
55
+ file = File.open(path, "r")
56
+ contents = file.read
57
+ file.close
58
+ return contents
59
+ end
60
+
61
+ def self.config_path
62
+ if File.exists?(File.join(self.users_home_directory, '.ghsync', 'config.json'))
63
+ return File.join(self.users_home_directory, '.ghsync', 'config.json')
64
+ end
65
+ end
66
+
67
+ def self.pra_config_path
68
+ if File.exists?(File.join(self.users_home_directory, '.pra', 'config.json'))
69
+ return File.join(self.users_home_directory, '.pra', 'config.json')
70
+ else
71
+ return File.join(self.users_home_directory, '.pra.json')
72
+ end
73
+ end
74
+
75
+ def self.users_home_directory
76
+ return ENV['HOME']
77
+ end
78
+
79
+ def self.json_parse(content)
80
+ return JSON.parse(content)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,106 @@
1
+ require 'octokit'
2
+ module Ghsync
3
+ class RepoSync
4
+ def initialize(config)
5
+ @config = config
6
+ end
7
+
8
+ def sync
9
+ org_sync
10
+ repo_sync
11
+ end
12
+
13
+ def repo_list(org_name)
14
+ organization_repositories(org_name).collect {|repo| repo[:name] }
15
+ end
16
+
17
+ # Organization Sync should for each org
18
+ # 1. Collect all the known projects in the target folder
19
+ # 2. Collect all the organization repositories
20
+ # 3. Exclude repos from the blacklist
21
+ # 4. Exclude already pulled projects
22
+ # 5. Clone projects using the ssh_url
23
+ # 6. Fetch in existing projects for the org?
24
+ def org_sync
25
+ @config.organizations.each do |org|
26
+ target_folder = File.expand_path(org["base_path"] || @config.base_path)
27
+ known_projects = cloned_projects(org["name"], target_folder)
28
+ project_names = known_projects.keys
29
+ Dir.chdir target_folder
30
+
31
+ organization_repositories(org["name"]).each do |repo|
32
+ unless project_names.include?(repo[:name]) || org["exclude"].include?(repo[:name])
33
+ # TODO There should be a library to handle IO
34
+ puts "Cloning #{repo[:name]}"
35
+ `git clone #{repo[:ssh_url]}`
36
+ end
37
+ end
38
+ known_projects.each do |name, project|
39
+ puts "Syncing #{name}"
40
+ `cd #{project[:path]} && git fetch`
41
+ end
42
+ end
43
+ end
44
+
45
+ # Repo sync for each owner/repo should
46
+ # 1. Find all known projects from the owner in the target folder
47
+ # 2. Find the specific project by repo name
48
+ # 3. Clone the project if it doesn't exist, else fetch it
49
+ def repo_sync
50
+ @config.repositories.each do |repo_config|
51
+ target_folder = File.expand_path(repo_config["base_path"] || @config.base_path)
52
+ # TODO This seem expensive to search for projects for each repo, maybe
53
+ # turn this into something that is done for all target folders for
54
+ # orgs and repos?
55
+ projects = cloned_projects(repo_config["owner"], target_folder)
56
+ project = projects[repo_config["name"]]
57
+ Dir.chdir target_folder
58
+ if project.nil?
59
+ repo = repository(repo_config["owner"], repo_config["name"])
60
+ puts "Cloning #{repo_config["owner"]}/#{repo_config["name"]}"
61
+ `git clone #{repo[:ssh_url]}`
62
+ else
63
+ puts "Syncing #{repo[:name]}"
64
+ `cd #{project[:path]} && git fetch`
65
+ end
66
+ end
67
+ end
68
+
69
+ def projects_in_folder(target_folder)
70
+ # TODO There are probably libraries to make this less hacky
71
+ `cd #{target_folder} && for d in $(ls -d */); do (cd $d && echo "$(pwd)|$(git config --get remote.origin.url)"); done`.split
72
+ end
73
+
74
+ def cloned_projects(org_name, target_folder)
75
+ projects = projects_in_folder(target_folder)
76
+ projects.inject({}) do |hsh, project|
77
+ path, url = project.split("|")
78
+ if url =~ /#{org_name}\/(.*)\.git/
79
+ name = $1
80
+ hsh[name] = {
81
+ url: url,
82
+ path: path
83
+ }
84
+ end
85
+ hsh
86
+ end
87
+ end
88
+
89
+ def repository(owner, name)
90
+ client.repository("#{owner}/#{name}")
91
+ end
92
+
93
+ def organization_repositories(org_name)
94
+ client.organization_repositories(org_name)
95
+ end
96
+
97
+
98
+ def client
99
+ @client ||= Octokit::Client.new(
100
+ login: @config.username,
101
+ password: @config.password,
102
+ auto_paginate: true
103
+ )
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ module Ghsync
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ghsync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Miller
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: octokit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Sync github repositories
84
+ email:
85
+ - brimil01@gmail.com
86
+ executables:
87
+ - ghsync
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - exe/ghsync
102
+ - ghsync.gemspec
103
+ - lib/ghsync.rb
104
+ - lib/ghsync/app.rb
105
+ - lib/ghsync/config.rb
106
+ - lib/ghsync/repo_sync.rb
107
+ - lib/ghsync/version.rb
108
+ homepage: https://github.com/codebreakdown/ghsync
109
+ licenses:
110
+ - MIT
111
+ metadata:
112
+ allowed_push_host: https://rubygems.org
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.5.1
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Sync github repositories
133
+ test_files: []