learn-submit 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3aa61bac053388e71aa1e001c0003092963f31f6
4
- data.tar.gz: 9731f01ba108bd80ed61889d69160484f2e06544
3
+ metadata.gz: 17df7ba067b579399ee3f9d6168e1111f88ddbea
4
+ data.tar.gz: 1bd750a9d75686516ac797618ce5a87426f82ce3
5
5
  SHA512:
6
- metadata.gz: fbb4ab3eb97378fa924032471ce81bc175c83422f658e3aef22ca0f6c22ae888e7b9c3f25ada8771b033a6f87294e858ea586890b1925937d4fded74eac1e877
7
- data.tar.gz: 6808c5bcdab2521f45c3019a25c0f58e40495ebcc29bcccacf0f6269695e4bc38e4c45349535e2b66fbfe1144e14c540270c6725f58b7eb963bd9b1705f5e272
6
+ metadata.gz: 5703136126606a185b4e6ce9bdab04c36db10172e4b62e31ac0f2ee989cec2330eec95644ee21a051de14a4c849a185c083bea170e85716e6bb0f5fbdb798370
7
+ data.tar.gz: 8c645027fc36df28a87644302aae767a098dd48a2dcca00931ed45bc671c28a901eb5d9bb31d12d04c1bfa6f7ac0b4ae40edf7ea714d46e684f4f612872654e1
data/bin/learn-submit CHANGED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'learn_submit'
4
+
5
+ # To prevent weird errors when random command line args are passed in
6
+ KNOWN_COMMANDS = []
7
+
8
+ message = ARGV[0] ? ARGV.join(' ') : nil
9
+
10
+ ARGV.map! do |arg|
11
+ KNOWN_COMMANDS.include?(arg) ? arg : nil
12
+ end.compact!
13
+
14
+ LearnSubmit::Submission.create(message: message)
data/learn-submit.gemspec CHANGED
@@ -19,4 +19,8 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "bundler", "~> 1.7"
21
21
  spec.add_development_dependency "rake", "~> 10.0"
22
+
23
+ spec.add_runtime_dependency "netrc"
24
+ spec.add_runtime_dependency "git", "~> 1.2"
25
+ spec.add_runtime_dependency "learn-web", ">= 1.0.0"
22
26
  end
data/lib/learn_submit.rb CHANGED
@@ -1,5 +1,12 @@
1
- require "learn_submit/version"
1
+ require 'timeout'
2
+ require 'netrc'
3
+ require 'git'
4
+ require 'learn_web'
2
5
 
3
- module Learn::Submit
4
- # Your code goes here...
6
+ require 'learn_submit/version'
7
+ require 'learn_submit/submission'
8
+ require 'learn_submit/submission/git_interactor'
9
+
10
+
11
+ module LearnSubmit
5
12
  end
@@ -0,0 +1,70 @@
1
+ module LearnSubmit
2
+ class Submission
3
+ attr_reader :git, :client
4
+
5
+ def self.create(message: nil)
6
+ new(message: message).create
7
+ end
8
+
9
+ def initialize(message:)
10
+ _login, token = Netrc.read['learn-config']
11
+
12
+ @client = LearnWeb::Client.new(token: token)
13
+ @git = LearnSubmit::Submission::GitInteractor.new(username: user.username, message: message)
14
+ end
15
+
16
+ def create
17
+ commit_and_push!
18
+ submit!
19
+ end
20
+
21
+ def user
22
+ @user ||= client.me
23
+ end
24
+
25
+ private
26
+
27
+ def commit_and_push!
28
+ git.commit_and_push
29
+
30
+ # Just to give GitHub a second to register the repo changes
31
+ sleep(1)
32
+ end
33
+
34
+ def submit!(retries=3)
35
+ puts 'Submitting lesson...'
36
+ repo_name = git.repo_name
37
+ branch_name = git.branch_name
38
+
39
+ begin
40
+ pr_response = Timeout::timeout(15) do
41
+ client.issue_pull_request(repo_name: repo_name, branch_name: branch_name)
42
+ end
43
+ rescue Timeout::Error
44
+ if retries > 0
45
+ puts "It seems like there's a problem connecting to Learn. Trying again..."
46
+ submit!(retries-1)
47
+ else
48
+ puts "Sorry, there's a problem reaching Learn right now. Please try again."
49
+ exit
50
+ end
51
+ end
52
+
53
+ case pr_response.status
54
+ when 200
55
+ puts "Done."
56
+ exit
57
+ when 404
58
+ puts 'Sorry, it seems like there was a problem connecting with Learn. Please try again.'
59
+ exit
60
+ else
61
+ puts pr_response.message
62
+
63
+ if retries > 0
64
+ puts "Trying again..."
65
+ submit!(retries-1)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,124 @@
1
+ module LearnSubmit
2
+ class Submission
3
+ class GitInteractor
4
+ attr_reader :username, :git, :message
5
+ attr_accessor :remote_name, :old_remote_name, :old_url
6
+
7
+ LEARN_ORG_NAMES = [
8
+ 'learn-co',
9
+ 'learn-co-curriculum',
10
+ 'learn-co-students'
11
+ ]
12
+
13
+ def initialize(username:, message:)
14
+ @username = username
15
+ @message = message || 'Done.'
16
+ @git = set_git
17
+ end
18
+
19
+ def commit_and_push
20
+ check_remote
21
+ add_changes
22
+ commit_changes
23
+
24
+ push!
25
+ end
26
+
27
+ def repo_name
28
+ url = git.remote(remote_name).url
29
+ url.match(/^.+\w+\/(.*?)(?:\.git)?$/)[1]
30
+ end
31
+
32
+ def branch_name
33
+ git.branch.name
34
+ end
35
+
36
+ private
37
+
38
+ def set_git
39
+ begin
40
+ Git.open(FileUtils.pwd)
41
+ rescue ArgumentError => e
42
+ if e.message.match(/path does not exist/)
43
+ puts "It doesn't look like you're in a lesson directory."
44
+ puts 'Please cd into an appropriate directory and try again.'
45
+
46
+ exit
47
+ else
48
+ puts 'Sorry, something went wrong. Please try again.'
49
+ exit
50
+ end
51
+ end
52
+ end
53
+
54
+ def check_remote
55
+ self.remote_name = if git.remote.url.match(/#{username}/).nil?
56
+ fix_remote!
57
+ else
58
+ git.remote.name
59
+ end
60
+ end
61
+
62
+ def fix_remote!
63
+ self.old_remote_name = git.remote.name
64
+ self.old_url = git.remote.url
65
+
66
+ add_backup_remote
67
+ remove_old_remote
68
+ add_correct_remote
69
+ end
70
+
71
+ def add_backup_remote
72
+ git.add_remote("#{old_remote_name}-bak", old_url)
73
+ end
74
+
75
+ def remove_old_remote
76
+ git.remote(old_remote_name).remove
77
+ end
78
+
79
+ def add_correct_remote
80
+ new_url = old_url.gsub(/#{LEARN_ORG_NAMES.join('|').gsub('-','\-')}/, username)
81
+ git.add_remote(old_remote_name, new_url)
82
+
83
+ old_remote_name
84
+ end
85
+
86
+ def add_changes
87
+ puts 'Adding changes...'
88
+ git.add(all: true)
89
+ end
90
+
91
+ def commit_changes
92
+ puts 'Committing changes...'
93
+ begin
94
+ git.commit(message)
95
+ rescue Git::GitExecuteError => e
96
+ if e.message.match(/nothing to commit/)
97
+ puts "It looks like you have no changes to commit. Will still try updating your submission..."
98
+ else
99
+ puts 'Sorry, something went wrong. Please try again.'
100
+ exit
101
+ end
102
+ end
103
+ end
104
+
105
+ def push!(retries=3)
106
+ puts 'Pushing changes to GitHub...'
107
+ push_remote = git.remote(self.remote_name)
108
+ begin
109
+ Timeout::timeout(15) do
110
+ git.push(push_remote)
111
+ end
112
+ rescue Timeout::Error
113
+ if retries > 0
114
+ puts "Seems there was an error pushing to GitHub. Trying again..."
115
+ puts!(retries-1)
116
+ else
117
+ puts "Can't reach GitHub right now. Please try again."
118
+ exit
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -1,3 +1,3 @@
1
1
  module LearnSubmit
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: learn-submit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flatiron School
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-21 00:00:00.000000000 Z
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,48 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: netrc
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: git
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: learn-web
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.0
41
83
  description:
42
84
  email:
43
85
  - learn@flatironschool.com
@@ -54,6 +96,8 @@ files:
54
96
  - bin/learn-submit
55
97
  - learn-submit.gemspec
56
98
  - lib/learn_submit.rb
99
+ - lib/learn_submit/submission.rb
100
+ - lib/learn_submit/submission/git_interactor.rb
57
101
  - lib/learn_submit/version.rb
58
102
  homepage: https://learn.co
59
103
  licenses: