Skip to content

Instantly share code, notes, and snippets.

@jessereynolds
Last active May 17, 2019 13:22
Show Gist options
  • Save jessereynolds/ced53ad1a085a3ff4e91d75ca5a8f7ae to your computer and use it in GitHub Desktop.
Save jessereynolds/ced53ad1a085a3ff4e91d75ca5a8f7ae to your computer and use it in GitHub Desktop.
TFS - create repo curl examples
#!/usr/bin/env ruby
require 'net/http'
require 'openssl'
require 'json'
# Create a new git repo on TFS (aka VSTS, aka Azure DevOps Server soon)
# This works with TFS 2017 (on prem). It differs from the official API documentation
# in that the string "repos/" is not present in the URLs.
# Relevant docs:
# - https://docs.microsoft.com/en-us/azure/devops/integrate/previous-apis/git/repositories
# - https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/pats?view=vsts
repo_name = "test-repo"
tfsserver = "va.com.au"
project_name = "Puppet%20Mirrors"
username = "someuser"
token = "tokentokentoken"
config = {
:tfsserver => tfsserver,
:project_name => project_name,
:username => username,
:token => token,
:project_repos_api => "http://#{tfsserver}/tfs/DefaultCollection/#{project_name}/_apis/git/repositories"
}
def repo_exists?(repo_name, config)
uri = URI("#{config[:project_repos_api]}/#{repo_name}?api-version=1.0")
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Get.new uri.request_uri
request.basic_auth config[:username], config[:token]
response = http.request request
# puts response
# puts response.body
case response
when Net::HTTPSuccess # 200 OK - repo exists
return true
when Net::HTTPNotFound # 404 - repo does not exist
return false
else
raise "Error when getting repository status for #{repo_name}: response: #{response}"
end
end
end
def create_repo(repo_name, config)
project_repos_api = "http://#{config[:tfsserver]}/tfs/DefaultCollection/#{config[:project_name]}/_apis/git/repositories"
uri = URI("#{project_repos_api}/#{repo_name}?api-version=1.0")
data = {
"name" => repo_name,
}
Net::HTTP.start(uri.hostname, uri.port) {|http|
request = Net::HTTP::Post.new uri
request.content_type = "application/json"
request.body = data.to_json
request.basic_auth config[:username], config[:token]
response = http.request request
unless response.is_a?(Net::HTTPSuccess)
raise "Error when creating repository #{repo_name}: response: #{response}"
end
}
end
if repo_exists?(repo_name, config)
puts "Repo #{repo_name} already exists."
else
print "Repo #{repo_name} does not exist, creating... "
create_repo(repo_name, config)
puts "Done."
end

This works with TFS 2017 (on prem). It differs from the official API documentation in that the string "repos/" is not present in the URLs.

Relevant docs:

$tfsserver="tfs.example"
$project="Puppet Mirrors"
$username="someuser"
$token="tokentokentoken"
$repo_name="test-repo"

Testing to see if the repo exists (200 means it exists, 404 means it does not exist)

curl -u $username:$token \
  "http://${tfsserver}/tfs/DefaultCollection/${project}/_apis/git/repositories/${repo_name}?api-version=1.0"

Creating the repo:

curl -u ${username}:${token} \
  --header "Content-Type: application/json" --request POST --data \
  '{ "name": "$repo_name" }' \
  "http://${tfs_server}/tfs/DefaultCollection/${project_name}/_apis/git/repositories/?api-version=1.0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment