Class: Dependabot::Clients::Azure
- Inherits:
-
Object
- Object
- Dependabot::Clients::Azure
- Defined in:
- lib/dependabot/clients/azure.rb
Defined Under Namespace
Classes: NotFound
Class Method Summary collapse
-
.for_source(source:, credentials:) ⇒ Object
Constructor methods #.
Instance Method Summary collapse
- #branch(branch_name) ⇒ Object
- #commits(branch_name = nil) ⇒ Object
- #create_commit(branch_name, base_commit, commit_message, files) ⇒ Object
- #create_pull_request(pr_name, source_branch, target_branch, pr_description, labels) ⇒ Object
- #fetch_commit(_repo, branch) ⇒ Object
- #fetch_default_branch(_repo) ⇒ Object
- #fetch_file_contents(commit, path) ⇒ Object
- #fetch_repo_contents(commit = nil, path = nil) ⇒ Object
- #fetch_repo_contents_treeroot(commit = nil, path = nil) ⇒ Object
- #get(url) ⇒ Object
-
#initialize(source, credentials) ⇒ Azure
constructor
Client #.
- #post(url, json) ⇒ Object
- #pull_requests(source_branch, target_branch) ⇒ Object
Constructor Details
#initialize(source, credentials) ⇒ Azure
Client #
28 29 30 31 |
# File 'lib/dependabot/clients/azure.rb', line 28 def initialize(source, credentials) @source = source @credentials = credentials end |
Class Method Details
.for_source(source:, credentials:) ⇒ Object
Constructor methods #
15 16 17 18 19 20 21 22 |
# File 'lib/dependabot/clients/azure.rb', line 15 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
106 107 108 109 110 111 112 113 |
# File 'lib/dependabot/clients/azure.rb', line 106 def branch(branch_name) response = get(source.api_endpoint + source.organization + "/" + source.project + "/_apis/git/repositories/" + source.unscoped_repo + "/refs?filter=heads/" + branch_name) JSON.parse(response.body).fetch("value").first end |
#commits(branch_name = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/dependabot/clients/azure.rb', line 91 def commits(branch_name = nil) commits_url = source.api_endpoint + source.organization + "/" + source.project + "/_apis/git/repositories/" + source.unscoped_repo + "/commits" unless branch_name.to_s.empty? commits_url += "?searchCriteria.itemVersion.version=" + branch_name end response = get(commits_url) JSON.parse(response.body).fetch("value") end |
#create_commit(branch_name, base_commit, commit_message, files) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/dependabot/clients/azure.rb', line 126 def create_commit(branch_name, base_commit, , files) content = { refUpdates: [ { name: "refs/heads/" + branch_name, oldObjectId: base_commit } ], commits: [ { comment: , changes: files.map do |file| { changeType: "edit", item: { path: file.path }, newContent: { content: Base64.encode64(file.content), contentType: "base64encoded" } } end } ] } post(source.api_endpoint + source.organization + "/" + source.project + "/_apis/git/repositories/" + source.unscoped_repo + "/pushes?api-version=5.0", content.to_json) end |
#create_pull_request(pr_name, source_branch, target_branch, pr_description, labels) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/dependabot/clients/azure.rb', line 153 def create_pull_request(pr_name, source_branch, target_branch, pr_description, labels) # Azure DevOps only support descriptions up to 4000 characters (https://developercommunity.visualstudio.com/content/problem/608770/remove-4000-character-limit-on-pull-request-descri.html) azure_max_length = 3999 if pr_description.length > azure_max_length truncated_msg = "...\n\n_Description has been truncated_" truncate_length = azure_max_length - truncated_msg.length pr_description = pr_description[0..truncate_length] + truncated_msg end content = { sourceRefName: "refs/heads/" + source_branch, targetRefName: "refs/heads/" + target_branch, title: pr_name, description: pr_description, labels: labels.map { |label| { name: label } } } post(source.api_endpoint + source.organization + "/" + source.project + "/_apis/git/repositories/" + source.unscoped_repo + "/pullrequests?api-version=5.0", content.to_json) end |
#fetch_commit(_repo, branch) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/dependabot/clients/azure.rb', line 33 def fetch_commit(_repo, branch) response = get(source.api_endpoint + source.organization + "/" + source.project + "/_apis/git/repositories/" + source.unscoped_repo + "/stats/branches?name=" + branch) JSON.parse(response.body).fetch("commit").fetch("commitId") end |
#fetch_default_branch(_repo) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/dependabot/clients/azure.rb', line 42 def fetch_default_branch(_repo) response = get(source.api_endpoint + source.organization + "/" + source.project + "/_apis/git/repositories/" + source.unscoped_repo) JSON.parse(response.body).fetch("defaultBranch").gsub("refs/heads/", "") end |
#fetch_file_contents(commit, path) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dependabot/clients/azure.rb', line 80 def fetch_file_contents(commit, path) response = get(source.api_endpoint + source.organization + "/" + source.project + "/_apis/git/repositories/" + source.unscoped_repo + "/items?path=" + path + "&versionDescriptor.versionType=commit" \ "&versionDescriptor.version=" + commit) response.body end |
#fetch_repo_contents(commit = nil, path = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dependabot/clients/azure.rb', line 50 def fetch_repo_contents(commit = nil, path = nil) tree = fetch_repo_contents_treeroot(commit, path) response = get(source.api_endpoint + source.organization + "/" + source.project + "/_apis/git/repositories/" + source.unscoped_repo + "/trees/" + tree + "?recursive=false") JSON.parse(response.body).fetch("treeEntries") end |
#fetch_repo_contents_treeroot(commit = nil, path = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/dependabot/clients/azure.rb', line 61 def fetch_repo_contents_treeroot(commit = nil, path = nil) actual_path = path actual_path = "/" if path.to_s.empty? tree_url = source.api_endpoint + source.organization + "/" + source.project + "/_apis/git/repositories/" + source.unscoped_repo + "/items?path=" + actual_path unless commit.to_s.empty? tree_url += "&versionDescriptor.versionType=commit" \ "&versionDescriptor.version=" + commit end tree_response = get(tree_url) JSON.parse(tree_response.body).fetch("objectId") end |
#get(url) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/dependabot/clients/azure.rb', line 177 def get(url) response = Excon.get( url, user: credentials&.fetch("username"), password: credentials&.fetch("password"), idempotent: true, **SharedHelpers.excon_defaults ) raise NotFound if response.status == 404 response end |
#post(url, json) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/dependabot/clients/azure.rb', line 190 def post(url, json) response = Excon.post( url, headers: { "Content-Type" => "application/json" }, body: json, user: credentials&.fetch("username"), password: credentials&.fetch("password"), idempotent: true, **SharedHelpers.excon_defaults ) raise NotFound if response.status == 404 response end |
#pull_requests(source_branch, target_branch) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/dependabot/clients/azure.rb', line 115 def pull_requests(source_branch, target_branch) response = get(source.api_endpoint + source.organization + "/" + source.project + "/_apis/git/repositories/" + source.unscoped_repo + "/pullrequests?searchCriteria.status=all" \ "&searchCriteria.sourceRefName=refs/heads/" + source_branch + "&searchCriteria.targetRefName=refs/heads/" + target_branch) JSON.parse(response.body).fetch("value") end |