Class: Cloud::Projects

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud/projects.rb

Instance Method Summary (collapse)

Constructor Details

- (Projects) initialize(env: nil)

Returns a new instance of Projects



6
7
8
9
10
# File 'lib/cloud/projects.rb', line 6

def initialize(env: nil)
  @env = env
  @user = env['AUTH_CREDENTIALS'].first
  @token = env['AUTH_CREDENTIALS'].last
end

Instance Method Details

- (Object) create_client(client_name, project_id, root)

Generate a new client that only contains the project mapping.

The client name is a combination of user, project, and device. We prefix it with “_hve” just for clarity.

We do not host lock the client.

Parameters:

  • project_id (String)

    Our encoded project name

  • device (String)

    A device ID, like a hostname

  • root (String)

    The Root value for the client parameter



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cloud/projects.rb', line 75

def create_client(client_name, project_id, root)
  project = project_from_id(project_id)

  client_spec = p4.fetch_client(client_name)

  client_spec._root = root
  client_spec._host = nil

  client_spec._options = 'allwrite noclobber nocompress unlocked nomodtime rmdir';
  client_spec._stream = project['stream']

  results = p4.save_client(client_spec)
  client_name
end

- (Object) create_pending_change(project_id)

If the user doesn't have a current pending change for the project, create one, and return that.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/cloud/projects.rb', line 117

def create_pending_change(project_id)
  change = find_pending_change_for_project(project_id)

  return change if change

  change_spec = p4.fetch_change
  change_spec._description = "_hws_#{user}_#{project_id}"

  save_results = p4.save_change(change_spec)

  change = save_results.first.gsub(/Change (\d+) created./, '\1')

  # If we don't reset the client of this pending change, we won't be able
  # to cleanup the temporary client.
  change_spec = p4.fetch_change(change)
  change_spec._client = 'INVALID'
  p4.save_change(change_spec)

  change
end

- (Object) fetch(id, env: nil)



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cloud/projects.rb', line 35

def fetch(id, env: nil)
  # env.AUTH_CREDENTIALS has our token (in last)
  # GET (cloud url)/api/v1/project/:id
  uri = URI("#{Settings.cloud_settings[:helix_cloud_url]}/api/v1/projects/#{id}")
  # auth.credentials.first == username
  # auth.credentials.last == session token
  req = Net::HTTP::Get.new(uri)
  req['Cookie'] = @token
  req['Content-Type'] = 'application/json'

  res = Net::HTTP.start(uri.hostname, uri.port) { |http|
    http.request(req)
  }

  return nil if res.code != '200'

  project = JSON(res.body)
  # project[:service] = self
  return project
end

- (Object) fetch_by_name(name)

Returns the project's “details” based on the project name.

No validation is done to ensure this directory actually exists in the system.

Raises:

  • (Exception)


60
61
62
63
# File 'lib/cloud/projects.rb', line 60

def fetch_by_name(name)
  # TODO : do we need this?
  raise Exception
end

- (Object) find_latest_change_for_project(project_id)

Find the latest submitted change for the project

Use the 'p4 changes -m 1 -s submitted [depot path]'

Parameters:

  • project_id (String)

    The encoded project ID



95
96
97
98
99
100
# File 'lib/cloud/projects.rb', line 95

def find_latest_change_for_project(project_id)
  project = project_from_id(project_id)
  results = p4.run_changes('-m', '1', '-s', 'submitted', "#{project['stream']}/...")

  results.first['change'] unless results.empty?
end

- (Object) find_pending_change_for_project(project_id)

The HVE project 'changelist' is a pending changelist whose description is hws[user]_[project_id]

We use 'changes -l' to find the change to match potentially long project names.



107
108
109
110
111
112
113
# File 'lib/cloud/projects.rb', line 107

def find_pending_change_for_project(project_id)
  results = p4.run_changes('-l', '-u', user, '-s', 'pending')

  change = results.find { |r| r['desc'].include?(project_id) }

  change['change'] if change
end

- (Object) list(details: false, extension: nil, env: nil)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cloud/projects.rb', line 12

def list(details: false, extension: nil, env: nil)
  # GET (cloud url)/api/v1/projects
  uri = URI("#{Settings.cloud_settings[:helix_cloud_url]}/api/v1/projects")
  # auth.credentials.first == username
  # auth.credentials.last == session token
  req = Net::HTTP::Get.new(uri)
  req['Cookie'] = @token
  req['Content-Type'] = 'application/json'

  res = Net::HTTP.start(uri.hostname, uri.port) { |http|
    http.request(req)
  }

  return nil if res.code != '200'

  list = JSON(res.body)
  if details
    return list
  else
    return list.map { |n| HWSStrings.component_encode(n['name']) }
  end
end

- (Object) user



138
139
140
# File 'lib/cloud/projects.rb', line 138

def user
  @user
end