Skip to content

Instantly share code, notes, and snippets.

@mantri-govind
Last active September 23, 2019 18:05
Show Gist options
  • Select an option

  • Save mantri-govind/65bbb6a1785fd99950df1da189f187a3 to your computer and use it in GitHub Desktop.

Select an option

Save mantri-govind/65bbb6a1785fd99950df1da189f187a3 to your computer and use it in GitHub Desktop.
This script will extract tag from gitlab's pending job
#
# @author : mantri-govind
#
# This script will pull first tag by default. If you wish to extract all consider updating last time and remove the index.
#
# Details for parameters
# JobName : Name of gitlab job for which we want to extract tag
# projectId : Project Id can be found on your repo's main page
# commitId : pass the full commit ID which should be extracted by job API
#
def getPendingTag(jobName, projectId, commitId)
# Using gitlab repository_files prepare the url to pull the file
url = URI(URI.encode("https://gitlab.example.net/gitlab/api/v4/projects/#{projectId}/repository/files/.gitlab-ci.yml?ref=#{commitId}"))
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(url)
request["content-type"] = 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
request["private-token"] = ENV['private_token']
request["cache-control"] = 'no-cache'
request.body = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\xxxxxx\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ref\"\r\n\r\nmaster\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
response = http.request(request)
job = JSON.parse(job.read_body)
file = File.open("tmp/gitlab.yml", "w+") do |file|
file.write(Base64.decode64(job["content"]))
end
temp = YAML.load_file("#{Rails.root}/tmp/gitlab.yml")
# set the index according to your requirement
temp["#{jobName}"]["tags"][0]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment