Class: Dependabot::Clients::Gitea

Inherits:
Object
  • Object
show all
Defined in:
lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb

Defined Under Namespace

Classes: NotFound

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, credentials) ⇒ Gitea

Client #



29
30
31
32
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 29

def initialize(source, credentials)
  @source = source
  @credentials = credentials
end

Class Method Details

.for_source(source:, credentials:) ⇒ Object

Constructor methods #



16
17
18
19
20
21
22
23
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 16

def self.for_source(source:, credentials:)
  credential =
    credentials.
    select { |cred| cred["type"] == "git_source" }.
    find { |cred| cred["host"] == source.hostname }

  new(source, credential)
end

Instance Method Details

#branch(branch_name) ⇒ Object



77
78
79
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 77

def branch(branch_name)
  raise
end

#commits(branch_name = nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 70

def commits(branch_name = nil)
  response = get(source.api_endpoint + "repos/" +
                   "#{source.repo}/" + "commits")

  JSON.parse(response.body, object_class: OpenStruct)
end

#create_commit(branch_name, base_commit, commit_message, files, author_details) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 85

def create_commit(branch_name, base_commit, commit_message, files,
                  author_details)
  head_file = files.first
  tail_files = files.drop(1)

  res = fetch_repo_contents(base_commit, head_file.path)

  content = {
    new_branch: branch_name,
    content: Base64.encode64(head_file.content),
    message: commit_message,
    sha: res.fetch('sha'),
    branch: 'master'
  }

  response = put(source.api_endpoint + "repos/" +
                   "#{source.repo}/" + "contents" + head_file.path, content.to_json)
end

#create_pull_request(pr_name, source_branch, target_branch, pr_description, labels) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 104

def create_pull_request(pr_name, source_branch, target_branch,
                        pr_description, labels)
  content = {
    base: target_branch,
    head: source_branch,
    title: pr_name,
    body: pr_description,
  }

  response = post(source.api_endpoint + "repos/" +
                   "#{source.repo}/" + "pulls", content.to_json)
end

#fetch_commit(_repo, branch) ⇒ Object



34
35
36
37
38
39
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 34

def fetch_commit(_repo, branch)
  response = get(source.api_endpoint + "repos/" +
                   "#{source.repo}/" + "branches/" + branch)

  JSON.parse(response.body).fetch("commit").fetch("id")
end

#fetch_default_branch(_repo) ⇒ Object



48
49
50
51
52
53
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 48

def fetch_default_branch(_repo)
  response = get(source.api_endpoint + "repos/" +
                   source.repo)

  JSON.parse(response.body).fetch("default_branch")
end

#fetch_file_contents(commit, path) ⇒ Object



66
67
68
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 66

def fetch_file_contents(commit, path)
  fetch_repo_contents(commit, path)
end

#fetch_repo_contents(commit = nil, path = nil) ⇒ Object



55
56
57
58
59
60
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 55

def fetch_repo_contents(commit = nil, path = nil)
  response = get(source.api_endpoint + "repos/" +
                   "#{source.repo}/" + "contents/" + path, {ref: commit})

  JSON.parse(response.body)
end

#fetch_repo_contents_treeroot(commit = nil, path = nil) ⇒ Object



62
63
64
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 62

def fetch_repo_contents_treeroot(commit = nil, path = nil)
  raise # not-implemented
end

#get(url, extra_query = {}) ⇒ Object

Raises:



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 117

def get(url, extra_query = {})
  response = Excon.get(
    url,
    query: {access_token: credentials&.fetch("password")}.merge(extra_query),
    idempotent: true,
    **SharedHelpers.excon_defaults
  )
  raise NotFound if response.status == 404

  response
end

#labels(_repo) ⇒ Object



41
42
43
44
45
46
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 41

def labels(_repo)
  response = get(source.api_endpoint + "repos/" +
                   "#{source.repo}/" + "labels")

  JSON.parse(response.body, object_class: OpenStruct)
end

#post(url, json) ⇒ Object

Raises:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 129

def post(url, json)
  response = Excon.post(
    url,
    headers: {
      "Content-Type" => "application/json"
    },
    body: json,
    query: {access_token: credentials&.fetch("password")},
    idempotent: true,
    **SharedHelpers.excon_defaults
  )
  raise NotFound if response.status == 404

  response
end

#pull_requests(source_branch, target_branch) ⇒ Object



81
82
83
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 81

def pull_requests(source_branch, target_branch)
  raise
end

#put(url, json) ⇒ Object

Raises:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/hack/dependabot-core/common/lib/dependabot/clients/gitea.rb', line 145

def put(url, json)
  response = Excon.put(
    url,
    headers: {
      "Content-Type" => "application/json"
    },
    body: json,
    query: {access_token: credentials&.fetch("password")},
    idempotent: true,
    **SharedHelpers.excon_defaults
  )
  raise NotFound if response.status == 404

  response
end