Class: GitlabCli::Util::Snippet

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_cli/util/snippet.rb

Class Method Summary collapse

Class Method Details

.create(project, title, file_name, code) ⇒ Object

Snippet - Create



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab_cli/util/snippet.rb', line 21

def self.create(project, title, file_name, code)
  ## Adapted from https://github.com/ripienaar/snipper/blob/master/lib/snipper/util.rb
  if STDIN.tty?
    if File.readable?(code)
      content = File.read(code)
    else
      GitlabCli.ui.error "Cannot read the specified file."
      exit 1
    end
  else
    content = STDIN.read
  end
  ##
  
  id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)

  url = "api/v3/projects/%s/snippets%s" % [id, GitlabCli::Util.url_token]
  payload = {:title => title, :file_name => file_name, :code => content}

  begin 
    response = RestClient.post URI.join(GitlabCli::Config[:gitlab_url],url).to_s, payload
  rescue SocketError => e
    GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
    exit 1
  rescue Exception => e
    GitlabCli::Util.check_response_code(e.response)
  end
  data = JSON.parse(response)
  GitlabCli::Snippet.new(data['id'],data['title'],data['file_name'],data['expires_at'],data['updated_at'],data['created_at'],id,data['author'])
end

.delete(project, snippet) ⇒ Object

Snippet - Delete



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gitlab_cli/util/snippet.rb', line 89

def self.delete(project, snippet)
  id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)

  snippet_get = get(project, snippet)
  
  if snippet_get
    begin 
      response = RestClient.delete URI.join(GitlabCli::Config[:gitlab_url],"api/v3/projects/#{id}/snippets/#{snippet}#{GitlabCli::Util.url_token}").to_s
    rescue SocketError => e
      GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
      exit 1
    rescue Exception => e
      GitlabCli::Util.check_response_code(e.response)
    end
  end
end

.download(project, snippet, file_path) ⇒ Object

/snippet - Download/Save



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gitlab_cli/util/snippet.rb', line 107

def self.download(project, snippet, file_path)
  id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)
  snippet_content = view(project, snippet)
  
  begin
    File.open(file_path, 'w') { |file| file.write(snippet_content) }
  rescue IOError => e
    GitlabCli.ui.error "Cannot save snippet as file. Please check permissions for %s" % [file_path]
    exit 1
  rescue Errno::ENOENT => e
    GitlabCli.ui.error "Specified directory does not exist.  Directory must exist to save the snippet file."
    exit 1
  end
end

.get(project, snippet) ⇒ Object

Snippet - Get snippet object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gitlab_cli/util/snippet.rb', line 5

def self.get(project, snippet)
  id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)

  begin 
    response = RestClient.get URI.join(GitlabCli::Config[:gitlab_url],"api/v3/projects/#{id}/snippets/#{snippet}#{GitlabCli::Util.url_token}").to_s
  rescue SocketError => e
    GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
    exit 1
  rescue Exception => e
    GitlabCli::Util.check_response_code(e.response)
  end
  data = JSON.parse(response)
  GitlabCli::Snippet.new(data['id'],data['title'],data['file_name'],data['expires_at'],data['updated_at'],data['created_at'],id,data['author'])
end

.update(project, snippet, content) ⇒ Object

Snippet - Update



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gitlab_cli/util/snippet.rb', line 69

def self.update(project, snippet, content)
  id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)

  url = "api/v3/projects/%s/snippets/%s%s" % [id, snippet.id, GitlabCli::Util.url_token]
  payload = {:title => snippet.title, :file_name => snippet.file_name, :code => content}

  begin 
    response = RestClient.put URI.join(GitlabCli::Config[:gitlab_url],url).to_s, payload
  rescue SocketError => e
    GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
    exit 1
  rescue Exception => e
    GitlabCli::Util.check_response_code(e.response)
  end

  data = JSON.parse(response)
  GitlabCli::Snippet.new(data['id'],data['title'],data['file_name'],data['expires_at'],data['updated_at'],data['created_at'],id,data['author'])
end

.view(project, snippet) ⇒ Object

Snippet - View



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gitlab_cli/util/snippet.rb', line 53

def self.view(project, snippet)
  id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)

  url = "api/v3/projects/%s/snippets/%s/raw%s" % [id, snippet, GitlabCli::Util.url_token]

  begin 
    response = RestClient.get URI.join(GitlabCli::Config[:gitlab_url],url).to_s
  rescue SocketError => e
    GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
    exit 1
  rescue Exception => e
    GitlabCli::Util.check_response_code(e.response)
  end
end