git-semaphore 1.2.0 → 2.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d581bd5005d05232ba4b5a48c550869e5b0899a
4
- data.tar.gz: 11ebd99ca6deace07ea9bae7e99e8616316f78e0
3
+ metadata.gz: 59236af413fec5cad9c165b9450fdb9817be6379
4
+ data.tar.gz: 3a94ad7ec8bd108fc641fc8d046616459aa4737c
5
5
  SHA512:
6
- metadata.gz: 3dfe7c3bf1f6492b35649130e1dc6467841bcc6279172d0b8b7fb90bf2b8c27374a77594b66e5bd71261d650360511a3403f42baa0c0843a5bca5af1a338dfb6
7
- data.tar.gz: e8559616dbab2c11fcc95f8d61cce38a148c1225b3af54e0989d4cbbe8df7c4dd786e02256bd77cbbe073b87c734af3ae82a5822b9fde3609bb60dc6b84353c2
6
+ metadata.gz: a1116948c842d29d2f84197a8d3ba6c987f0f36ac08f95c72403b9b32d00107c34f9ee3c5939cd8c244e65c49a20ab2bcd5e18247dd06c1481306a8ab9131de9
7
+ data.tar.gz: e9b6969e61247975e23149de4ac36f335cffb5c1acef1fb5f25993a8e2d1ce640dbdbdfb2071b32e194b23c04d978e05eb1328e882c753a14107fadc67003694
data/.pryrc CHANGED
@@ -2,5 +2,9 @@
2
2
  load "exe/git-semaphore" unless Kernel.const_defined? 'Git::Semaphore'
3
3
 
4
4
  # utility function to set pry context
5
- # to an instance of <Git::Semaphore::App>
6
- def app() pry Git::Semaphore::App.new(Git::Semaphore.auth_token, Git::Semaphore.git_repo, ENV) ; end
5
+ # to an instance of <Rugged::Repository>
6
+ def repository() pry Git::Semaphore.git_repo ; end
7
+
8
+ # utility function to set pry context
9
+ # to an instance of <Git::Semaphore::Project>
10
+ def project() pry Git::Semaphore::Project.from_repo(Git::Semaphore.git_repo) ; end
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [![Travis CI](https://travis-ci.org/pvdb/git-semaphore.svg?branch=v0.0.6)](https://travis-ci.org/pvdb/git-semaphore)
6
6
 
7
- Integrate git repositories with their corresponding project on [semaphoreci.com][] _(via the semaphore API)_
7
+ Integrate git repositories with their corresponding project on [semaphoreci.com][] _(via the Semaphore API)_
8
8
 
9
9
  ## Features
10
10
 
@@ -108,27 +108,62 @@ When used inside a git repository, `git semaphore` uses [convention over configu
108
108
 
109
109
  | setting | inside git repo | pseudo-code | override |
110
110
  |--------------|--------------------|---------------------------------|---------------------------------|
111
- | project name | derived from pwd | `File.basename(Dir.pwd)` | `ENV['SEMAPHORE_PROJECT_NAME']` |
111
+ | owner & name | based on `${PWD}` | `Dir.pwd.split('/').last(2)` | `ENV['SEMAPHORE_PROJECT_NAME']` |
112
112
  | branch name | current git branch | `git symbolic-ref --short HEAD` | `ENV['SEMAPHORE_BRANCH_NAME']` |
113
113
  | commit SHA | current git head | `git rev-parse HEAD` | `ENV['SEMAPHORE_COMMIT_SHA']` |
114
114
  | build number | last branch build | `N/A` | `ENV['SEMAPHORE_BUILD_NUMBER']` |
115
115
 
116
116
  However, each of these defaults can be overridden by setting the corresponding environment variable, as documented in the above table. The same `ENV`-based override mechanism can be leveraged to use `git semaphore` outside of a git repository.
117
117
 
118
- The `git semaphore --settings` command can be used to print out the values for these various settings:
118
+ ## Using the "full name" convention
119
+
120
+ On your local filesystem, git repositories need to use paths that follow the "full name" convention in use on `github.com` and `semaphoreci.com`, ie. the last two path components for the `pvdb/git-semaphore` repository should be `pvdb` and `git-semaphore` respectively, as illustrated on this table:
121
+
122
+ | | full name | owner | name | URL / path |
123
+ |-----------|----------------------|--------|-----------------|----------------------------------------------|
124
+ | | `pvdb/git-semaphore` | `pvdb` | `git-semaphore` | |
125
+ | GitHub | | | | `https://github.com/pvdb/git-semaphore` |
126
+ | Semaphore | | | | `https://semaphoreci.com/pvdb/git-semaphore` |
127
+ | filesytem | | | | `${HOME}/Projects/pvdb/git-semaphore` |
128
+
129
+ Put differently: if you typically create your git repositories in `${HOME}/Projects`, and you have the following three git repos...
130
+
131
+ pvdb/git-meta
132
+ pvdb/git-semaphore
133
+ pvdb/git-switcher
134
+
135
+ ... then the directory tree should be as follows:
136
+
137
+ ${HOME}/Projects
138
+ └── pvdb
139
+ ├── git-meta
140
+ │   └── .git
141
+ ├── git-semaphore
142
+ │   └── .git
143
+ └── git-switcher
144
+ └── .git
145
+
146
+ So first you have a directory corresponding to the repository owner (`pvdb`) and one level down you have a directory corresponding to the repository name (`git-meta`, `git-semaphore` and `git-switcher` respectively).
147
+
148
+ ## A look behind the scences
149
+
150
+ The `git semaphore --settings` command can be used to print out the values for the most relevant settings:
119
151
 
120
152
  $ git semaphore --settings | jq '.'
121
153
  {
122
- "semaphore_auth_token": "Yds3w6o26FLfJTnVK2y9",
123
- "semaphore_project_name": "git-semaphore",
124
- "semaphore_branch_name": "master",
125
- "semaphore_commit_sha": "4b59c3e41ca4592dfb01f77f2163154f3d3532fe"
154
+ "auth_token": "Yds3w6o26FLfJTnVK2y9",
155
+ "project_name": "pvdb/git-semaphore",
156
+ "branch_name": "master",
157
+ "commit_sha": "4b59c3e41ca4592dfb01f77f2163154f3d3532fe",
158
+ "build_number": "35"
126
159
  }
127
160
  $ _
128
161
 
162
+ The `git semaphore --internals` command adds all internal settings to the above settings hash.
163
+
129
164
  ## Available commands
130
165
 
131
- > ⚠️ all of the below examples need to be run from within a git repository ⚠️
166
+ > ⚠️ all of the below examples need to be run from within a git repository that follows the "full name" convention documented above ⚠️
132
167
 
133
168
  ### list the Semaphore settings
134
169
 
@@ -189,6 +224,9 @@ After installing [the indispensable jq utility][jq] (`brew install jq`), the raw
189
224
  # pretty-print the git semaphore settings
190
225
  git semaphore --settings | jq '.'
191
226
 
227
+ # pretty-print the git semaphore internals
228
+ git semaphore --internals | jq '.'
229
+
192
230
  # list the full name of all Semaphore projects
193
231
  git semaphore --projects | jq -r '.[] | .full_name'
194
232
 
data/exe/git-semaphore CHANGED
@@ -17,7 +17,9 @@ if __FILE__ == $0
17
17
 
18
18
  banner Git::Semaphore::BANNER
19
19
 
20
- on :settings, 'Display the relevants settings'
20
+ on :settings, 'Display most relevant settings'
21
+ on :internals, 'Display all internal settings'
22
+
21
23
  on :browse, 'Open the project on https://semaphoreci.com/'
22
24
 
23
25
  on :clean, 'Deletes the cached API query results'
@@ -32,55 +34,64 @@ if __FILE__ == $0
32
34
 
33
35
  end
34
36
 
35
- app = Git::Semaphore::App.new(Git::Semaphore.auth_token, Git::Semaphore.git_repo, ENV)
37
+ if options.clean?
38
+ Git::Semaphore.empty_cache_dir
39
+ exit 0
40
+ end
36
41
 
37
- if options.settings?
38
- puts app.to_json
42
+ if options.projects?
43
+ puts Git::Semaphore::Project.all.to_json
39
44
  exit 0
40
45
  end
41
46
 
42
- if options.browse?
43
- `open #{app.branch_url}`
47
+ project = if (git_repo = Git::Semaphore.git_repo)
48
+ Git::Semaphore::Project.from_repo(git_repo)
49
+ else
50
+ Git::Semaphore::Project.from_config(ENV)
51
+ end
52
+
53
+ if options.settings?
54
+ puts project.settings.to_json
44
55
  exit 0
45
56
  end
46
57
 
47
- if options.clean?
48
- Git::Semaphore.empty_cache_dir
58
+ if options.internals?
59
+ puts project.internals.to_json
49
60
  exit 0
50
61
  end
51
62
 
52
- if options.projects?
53
- puts app.projects.to_json
63
+ if options.browse?
64
+ `open #{project.branch_url}`
54
65
  exit 0
55
66
  end
56
67
 
57
68
  if options.branches?
58
- puts app.branches.to_json
69
+ puts project.branches.to_json
59
70
  exit 0
60
71
  end
61
72
 
62
73
  if options.status?
63
- puts app.status.to_json
74
+ puts project.status.to_json
64
75
  exit 0
65
76
  end
66
77
 
67
78
  if options.history?
68
- puts app.history.to_json
79
+ puts project.history.to_json
69
80
  exit 0
70
81
  end
71
82
 
72
83
  if options.information?
73
- puts app.information.to_json
84
+ puts project.information.to_json
74
85
  exit 0
75
86
  end
76
87
 
77
88
  if options.log?
78
- puts app.log.to_json
89
+ puts project.log.to_json
79
90
  exit 0
80
91
  end
81
92
 
82
93
  if options.rebuild?
83
- puts app.rebuild
94
+ puts project.rebuild
84
95
  exit 0
85
96
  end
86
97
 
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_dependency "slop", "~> 3.0"
23
- spec.add_dependency "grit", "~> 2.0"
23
+ spec.add_dependency "rugged" , "~> 0.24"
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.12"
26
26
  spec.add_development_dependency "rake", "~> 10.0"
data/lib/git/semaphore.rb CHANGED
@@ -1,10 +1,17 @@
1
1
  require 'uri'
2
+ require 'date'
2
3
  require 'json'
3
4
  require 'openssl'
4
5
  require 'net/http'
5
6
  require 'fileutils'
6
7
 
7
- require 'grit'
8
+ require 'rugged'
9
+
10
+ class Rugged::Repository
11
+ def owner() File.basename(File.dirname(workdir)) ; end
12
+ def name() File.basename(workdir) ; end
13
+ def full_name() "#{owner}/#{name}" ; end
14
+ end
8
15
 
9
16
  module Git
10
17
  module Semaphore
@@ -44,9 +51,9 @@ module Git
44
51
  end
45
52
 
46
53
  def self.git_repo
47
- @git_repo ||= begin
48
- Grit::Repo.new(Dir.pwd)
49
- rescue Grit::InvalidGitRepositoryError
54
+ begin
55
+ Rugged::Repository.new(Dir.pwd)
56
+ rescue Rugged::RepositoryError
50
57
  nil
51
58
  end
52
59
  end
@@ -74,4 +81,4 @@ require 'git/semaphore/api'
74
81
  require 'git/semaphore/api_cache'
75
82
  require 'git/semaphore/api_enrich'
76
83
 
77
- require 'git/semaphore/app'
84
+ require 'git/semaphore/project'
@@ -0,0 +1,176 @@
1
+ class Git::Semaphore::Project
2
+
3
+ def self.env_overrides
4
+ ENV.to_h.select { |key, _| key.start_with? 'SEMAPHORE_' }
5
+ end
6
+
7
+ def self.from_repo git_repo
8
+ self.from_config({
9
+ 'SEMAPHORE_PROJECT_NAME' => git_repo.full_name,
10
+ 'SEMAPHORE_BRANCH_NAME' => git_repo.head.name.split('/').last,
11
+ 'SEMAPHORE_COMMIT_SHA' => git_repo.head.target.oid,
12
+ 'SEMAPHORE_BUILD_NUMBER' => nil
13
+ }.merge(env_overrides))
14
+ end
15
+
16
+ def self.from_config config
17
+ self.new(
18
+ full_name: config['SEMAPHORE_PROJECT_NAME'],
19
+ branch_name: config['SEMAPHORE_BRANCH_NAME'],
20
+ options: {
21
+ commit_sha: config['SEMAPHORE_COMMIT_SHA'],
22
+ build_number: config['SEMAPHORE_BUILD_NUMBER'],
23
+ }
24
+ )
25
+ end
26
+
27
+ def initialize full_name:, branch_name:, options:
28
+ @auth_token = Git::Semaphore.auth_token
29
+ @full_name = full_name
30
+ @owner, @name = full_name.split('/')
31
+ @branch_name = branch_name || 'master'
32
+ @commit_sha = options[:commit_sha]
33
+ @build_number = options[:build_number]
34
+ end
35
+
36
+ def settings
37
+ {
38
+ auth_token: @auth_token,
39
+ project_name: @full_name,
40
+ branch_name: @branch_name,
41
+ commit_sha: @commit_sha,
42
+ build_number: @build_number,
43
+ }
44
+ end
45
+
46
+ def internals
47
+ settings.merge({
48
+ project: {
49
+ owner: @owner,
50
+ name: @name,
51
+ full_name: @full_name,
52
+ hash_id: project_hash_id,
53
+ url: project_url,
54
+ },
55
+ branch: {
56
+ name: @branch_name,
57
+ id: branch_id,
58
+ url: branch_url,
59
+ },
60
+ build: {
61
+ number: build_number,
62
+ result: build_result,
63
+ url: build_url,
64
+ },
65
+ })
66
+ end
67
+
68
+ #
69
+ # build-related queries: default to latest one...
70
+ #
71
+
72
+ def build_number
73
+ @build_number ||= history['builds'].first['build_number'].to_s
74
+ end
75
+
76
+ def build_result
77
+ @build_result ||= history['builds'].first['result']
78
+ end
79
+
80
+ #
81
+ # direct links to semaphore.ci
82
+ #
83
+
84
+ def project_url
85
+ project_hash['html_url']
86
+ end
87
+
88
+ def branch_url
89
+ branch_hash = project_hash['branches'].find { |hash|
90
+ hash['branch_name'] == @branch_name
91
+ }
92
+ branch_hash['branch_url']
93
+ end
94
+
95
+ def build_url
96
+ build_hash = history['builds'].find { |hash|
97
+ hash['build_number'].to_s == @build_number
98
+ }
99
+ build_hash['build_url']
100
+ end
101
+
102
+ #
103
+ # API related queries
104
+ #
105
+
106
+ def self.all
107
+ Git::Semaphore::API::Cache.projects(Git::Semaphore.auth_token)
108
+ end
109
+
110
+ class << self
111
+ alias_method :projects, :all
112
+ end
113
+
114
+ def branches
115
+ Git::Semaphore::API::Cache.branches(project_hash_id, @auth_token)
116
+ end
117
+
118
+ def status
119
+ Git::Semaphore::API::Cache.status(project_hash_id, branch_id, @auth_token)
120
+ end
121
+
122
+ def history
123
+ Git::Semaphore::API::Cache.history(project_hash_id, branch_id, @auth_token)
124
+ end
125
+
126
+ def information
127
+ Git::Semaphore::API::Cache.information(project_hash_id, branch_id, build_number, @auth_token,)
128
+ end
129
+
130
+ def log
131
+ Git::Semaphore::API::Cache.log(project_hash_id, branch_id, build_number, @auth_token,)
132
+ end
133
+
134
+ def rebuild
135
+ Git::Semaphore::API.rebuild(project_hash_id, branch_id, @auth_token)
136
+ end
137
+
138
+ private
139
+
140
+ def project_hash_for owner, name
141
+ self.class.projects.find { |project_hash|
142
+ project_hash['owner'] == owner && project_hash['name'] == name
143
+ }
144
+ end
145
+
146
+ def project_hash
147
+ project_hash_for(@owner, @name)
148
+ end
149
+
150
+ def project_hash_id_for owner, name
151
+ project_hash_for(owner, name)['hash_id']
152
+ end
153
+
154
+ def project_hash_id
155
+ project_hash_id_for(@owner, @name)
156
+ end
157
+
158
+ def branch_hash_for branch_name
159
+ branches.find { |branch_hash|
160
+ branch_hash['name'] == branch_name
161
+ }
162
+ end
163
+
164
+ def branch_hash
165
+ branch_hash_for(@branch_name)
166
+ end
167
+
168
+ def branch_id_for branch_name
169
+ branch_hash_for(branch_name)['id'].to_s
170
+ end
171
+
172
+ def branch_id
173
+ branch_id_for(@branch_name)
174
+ end
175
+
176
+ end
@@ -1,5 +1,5 @@
1
1
  module Git
2
2
  module Semaphore
3
- VERSION = "1.2.0"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-semaphore
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Vandenberk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-28 00:00:00.000000000 Z
11
+ date: 2016-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slop
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: grit
28
+ name: rugged
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.0'
33
+ version: '0.24'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.0'
40
+ version: '0.24'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -134,9 +134,9 @@ files:
134
134
  - lib/git/semaphore/api.rb
135
135
  - lib/git/semaphore/api_cache.rb
136
136
  - lib/git/semaphore/api_enrich.rb
137
- - lib/git/semaphore/app.rb
138
137
  - lib/git/semaphore/banner.rb
139
138
  - lib/git/semaphore/copyright.rb
139
+ - lib/git/semaphore/project.rb
140
140
  - lib/git/semaphore/version.rb
141
141
  homepage: https://github.com/pvdb/git-semaphore
142
142
  licenses:
@@ -1,133 +0,0 @@
1
- class Git::Semaphore::App
2
-
3
- attr_writer :project_name
4
- attr_writer :branch_name
5
- attr_writer :commit_sha
6
- attr_writer :build_number
7
-
8
- attr_reader :branch_url
9
-
10
- def initialize auth_token, git_repo, config = ENV
11
- @auth_token = auth_token
12
- @git_repo = git_repo
13
-
14
- self.project_name = config['SEMAPHORE_PROJECT_NAME']
15
- self.branch_name = config['SEMAPHORE_BRANCH_NAME']
16
- self.commit_sha = config['SEMAPHORE_COMMIT_SHA']
17
- self.build_number = config['SEMAPHORE_BUILD_NUMBER']
18
- end
19
-
20
- def to_json
21
- {
22
- semaphore_auth_token: @auth_token,
23
- semaphore_project_name: self.project_name,
24
- semaphore_branch_name: self.branch_name,
25
- semaphore_commit_sha: self.commit_sha,
26
- semaphore_build_number: self.build_number,
27
- }.to_json
28
- end
29
-
30
- def project_name
31
- return @project_name unless @project_name.nil?
32
- File.basename @git_repo.git.work_tree if @git_repo
33
- end
34
-
35
- def branch_name
36
- return @branch_name unless @branch_name.nil?
37
- @git_repo.head.name if @git_repo
38
- end
39
-
40
- def commit_sha
41
- return @commit_sha unless @commit_sha.nil?
42
- @git_repo.head.commit.id if @git_repo
43
- end
44
-
45
- def build_number
46
- return @build_number unless @build_number.nil?
47
- history['builds'].first['build_number'].to_s
48
- end
49
-
50
- def projects
51
- Git::Semaphore::API::Cache.projects(@auth_token)
52
- end
53
-
54
- def branches
55
- Git::Semaphore::API::Cache.branches(project_hash_id, @auth_token)
56
- end
57
-
58
- def status
59
- Git::Semaphore::API::Cache.status(project_hash_id, branch_id, @auth_token)
60
- end
61
-
62
- def history
63
- Git::Semaphore::API::Cache.history(project_hash_id, branch_id, @auth_token)
64
- end
65
-
66
- def information
67
- Git::Semaphore::API::Cache.information(project_hash_id, branch_id, build_number, @auth_token,)
68
- end
69
-
70
- def log
71
- Git::Semaphore::API::Cache.log(project_hash_id, branch_id, build_number, @auth_token,)
72
- end
73
-
74
- def rebuild
75
- Git::Semaphore::API.rebuild(project_hash_id, branch_id, @auth_token)
76
- end
77
-
78
- def branch_url
79
- branch_hash = project_hash['branches'].find { |hash|
80
- hash['branch_name'] == branch_name
81
- }
82
- branch_hash['branch_url']
83
- end
84
-
85
- private
86
-
87
- def project_hash_for project_name
88
- projects.find { |project_hash|
89
- project_hash['name'] == project_name
90
- }
91
- end
92
-
93
- def project_hash
94
- project_hash_for(project_name)
95
- end
96
-
97
- def project_hash_id_for project_name
98
- project_hash_for(project_name)['hash_id']
99
- end
100
-
101
- def project_hash_id
102
- project_hash_id_for(project_name)
103
- end
104
-
105
- def branch_hash_for branch_name
106
- branches.find { |branch_hash|
107
- branch_hash['name'] == branch_name
108
- }
109
- end
110
-
111
- def branch_hash
112
- branch_hash_for(branch_name)
113
- end
114
-
115
- def branch_id_for branch_name
116
- branch_hash_for(branch_name)['id'].to_s
117
- end
118
-
119
- def branch_id
120
- branch_id_for(branch_name)
121
- end
122
-
123
- def build_status_for commit_sha
124
- history['builds'].find { |build_status|
125
- build_status['commit']['id'] == commit_sha
126
- }
127
- end
128
-
129
- def build_status
130
- build_status_for(commit_sha)
131
- end
132
-
133
- end