Class: Janky::Repository

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/janky/repository.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.by_name(name) ⇒ Object

Find a named repository.

name - The String name of the repository.

Returns a Repository or nil when it doesn’t exists.



48
49
50
# File 'lib/janky/repository.rb', line 48

def self.by_name(name)
  find_by_name(name)
end

.setup(nwo, name = nil, template = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/janky/repository.rb', line 13

def self.setup(nwo, name = nil, template = nil)
  if nwo.nil?
    raise ArgumentError, "nwo can't be nil"
  end

  if repo = Repository.find_by_name(nwo)
    repo.update_attributes!(:job_template => template)
    repo.setup
    return repo
  end

  repo = GitHub.repo_get(nwo)
  return if !repo

  uri    = repo["private"] ? repo["ssh_url"] : repo["git_url"]
  name ||= repo["name"]
  uri.gsub!(/\.git$/, "")

  repo =
    if repo = Repository.find_by_name(name)
      repo.update_attributes!(:uri => uri, :job_template => template)
      repo
    else
      Repository.create!(:name => name, :uri => uri, :job_template => template)
    end

  repo.setup
  repo
end

Instance Method Details

#branch_for(name) ⇒ Object

Create or retrieve the named branch.

name - The branch’s name as a String.

Returns a Branch record.



67
68
69
# File 'lib/janky/repository.rb', line 67

def branch_for(name)
  branches.find_or_create_by_name(name)
end

#build_sha(sha1, user, room_id = nil, compare = nil) ⇒ Object

Create a Janky::Build object given a sha

sha1 - a string of the target sha to build user - The login of the GitHub user who pushed. room_id - optional Fixnum Campfire room ID. Defaults to the room set on compare - optional String GitHub Compare View URL. Defaults to the

Returns the newly created Janky::Build



110
111
112
113
114
# File 'lib/janky/repository.rb', line 110

def build_sha(sha1, user, room_id = nil, compare = nil)
  return nil unless sha1 =~ /^[0-9a-fA-F]{7,40}$/
  commit = commit_for_sha(sha1)
  commit.build!(user, room_id, compare)
end

#builderObject

Jenkins host executing this repo’s builds.

Returns a Builder::Client.



119
120
121
# File 'lib/janky/repository.rb', line 119

def builder
  Builder.pick_for(self)
end

#campfire_roomObject

Name of the Campfire room receiving build notifications.

Returns the name as a String.



161
162
163
# File 'lib/janky/repository.rb', line 161

def campfire_room
  ChatService.room_name(room_id)
end

#commit_for(commit) ⇒ Object

Create or retrieve the given commit.

commit - The Hash representation of the Commit.

Returns a Commit record.



76
77
78
79
# File 'lib/janky/repository.rb', line 76

def commit_for(commit)
  commits.find_by_sha1(commit[:sha1]) ||
    commits.create!(commit)
end

#commit_for_sha(sha1) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/janky/repository.rb', line 81

def commit_for_sha(sha1)
  commit_data = GitHub.commit(nwo, sha1)
  commit_message = commit_data["commit"]["message"]
  commit_url = github_url("commit/#{sha1}")
  author_data = commit_data["commit"]["author"]
  commit_author =
    if email = author_data["email"]
      "#{author_data["name"]} <#{email}>"
    else
      author_data["name"]
    end

  commit = commit_for({
    :repository => self,
    :sha1 => sha1,
    :author => commit_author,
    :message => commit_message,
    :url => commit_url,
  })
end

#delete_hookObject



189
190
191
192
193
# File 'lib/janky/repository.rb', line 189

def delete_hook
  if self.hook_url? && GitHub.hook_exists?(self.hook_url)
    GitHub.hook_delete(self.hook_url)
  end
end

#github_nameObject

Name of this repository on GitHub.

Returns the name as a String.



133
134
135
# File 'lib/janky/repository.rb', line 133

def github_name
  uri[/.*[\/:]([a-zA-Z0-9\-_]+)\/([a-zA-Z0-9\-_\.]+)/] && $2
end

#github_ownerObject

GitHub user owning this repo.

Returns the user name as a String.



126
127
128
# File 'lib/janky/repository.rb', line 126

def github_owner
  uri[/.*[\/:]([a-zA-Z0-9\-_]+)\//] && $1
end

#github_url(path) ⇒ Object

Append the given path to the GitHub URL of this repository.

path - String path. No slash necessary at the front.

Examples

github_url("issues")
=> "https://github.com/github/janky/issues"

Returns the URL as a String.



154
155
156
# File 'lib/janky/repository.rb', line 154

def github_url(path)
  "#{GitHub.github_url}/#{nwo}/#{path}"
end

#job_config_pathObject

The path of the Jenkins configuration template. Try “<job_template>.xml.erb” first, “<repo-name>.xml.erb” second, and then fallback to “default.xml.erb” under the root config directory.

Returns the template path as a Pathname.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/janky/repository.rb', line 208

def job_config_path
  user_override = Janky.jobs_config_dir.join("#{job_template.downcase}.xml.erb") if job_template
  custom = Janky.jobs_config_dir.join("#{name.downcase}.xml.erb")
  default = Janky.jobs_config_dir.join("default.xml.erb")

  if user_override && user_override.readable?
    user_override
  elsif custom.readable?
    custom
  elsif default.readable?
    default
  else
    raise Error, "no config.xml.erb template for repo #{id.inspect}"
  end
end

#job_nameObject

Calculate the name of the Jenkins job.

Returns a String hash of this Repository name and uri.



234
235
236
237
238
239
240
241
# File 'lib/janky/repository.rb', line 234

def job_name
  md5 = Digest::MD5.new
  md5 << name
  md5 << uri
  md5 << job_config_path.read
  md5 << builder.callback_url.to_s
  "#{name}-#{md5.hexdigest[0,12]}"
end

#job_urlObject

Construct the URL pointing to this Repository’s Jenkins job.

Returns the String URL.



227
228
229
# File 'lib/janky/repository.rb', line 227

def job_url
  builder.url + "job/#{job_name}"
end

#nwoObject

Fully qualified GitHub name for this repository.

Returns the name as a String. Example: github/janky.



140
141
142
# File 'lib/janky/repository.rb', line 140

def nwo
  "#{github_owner}/#{github_name}"
end

#room_idObject

Ditto but returns the String room id. Defaults to the one set in Campfire.setup.



167
168
169
# File 'lib/janky/repository.rb', line 167

def room_id
  read_attribute(:room_id) || ChatService.default_room_id
end

#setupObject

Setups GitHub and Jenkins for building this repository.

Returns nothing.



174
175
176
177
# File 'lib/janky/repository.rb', line 174

def setup
  setup_job
  setup_hook
end

#setup_hookObject

Create a GitHub hook for this Repository and store its URL if needed.

Returns nothing.



182
183
184
185
186
187
# File 'lib/janky/repository.rb', line 182

def setup_hook
  delete_hook

  url = GitHub.hook_create("#{github_owner}/#{github_name}")
  update_attributes!(:hook_url => url)
end

#setup_jobObject

Creates a job on the Jenkins server for this repository configuration unless one already exists. Can safely be run multiple times.

Returns nothing.



199
200
201
# File 'lib/janky/repository.rb', line 199

def setup_job
  builder.setup(job_name, uri, job_config_path)
end

#toggle_auto_buildObject

Toggle auto-build feature of this repo. When enabled (default), all branches are built automatically.

Returns the new flag status as a Boolean.



56
57
58
59
60
# File 'lib/janky/repository.rb', line 56

def toggle_auto_build
  toggle(:enabled)
  save!
  enabled
end