Programmatically create Stash GIT repos

Is there a way to programmatically create GIT repos in Stash (running Postgres). I need to create 40 odd blank starters in a central model, I have the list of names I want to use.

2 answers

1 accepted

9 votes
Seb Ruiz Atlassian Team Jun 28, 2012

Definitely.

You can use the Stash REST apis to do this. If you would like to see all the REST endpoints, please have a read through the REST apis or install the REST api browser plugin in Stash.

The REST endpoint you will want to use is as follows:

curl -u user:pass -X POST -H "Accept: application/json"  -H "Content-Type: application/json" "http://stash/projects/QA/repos/" -d '{"name": "Repository Name"}'

What does it mean if I get an error message stating the following:

"{"errors":[{"context":nul,l...]}

Seb Ruiz Atlassian Team Sep 04, 2012

It means that your request is malformed. Unfortunately it seems that Stash is not providing a good error message in this exact circumstance. If you provide the details of the request you are creating (headers, url, content) then I can look into it and fix your request and also make sure we do better error handling for this case.

The initial post by Seb worked for me as is.

Just don't forget to put /repos/ at the end of your stash project name. eg.

http://yourstashserverhost/projects/yourstashprojectname/repos/

Initially, I only had

http://yourstashserverhost/projects/yourstashprojectname/

so it didn't work.

Edit: It now seems to be working with wget but not curl. Clearly this is just a curl issue so I think I will just drop it and use wget. Thanks again for the help.

Thanks, Seb. I actually managed to create a repo by manually creating an http request through a proxy. Here is the request that worked:

POST /rest/api/1.0/projects/[Project Name]/repos HTTP/1.1
Host: [hostname]
Connection: keep-alive
Authorization: Basic [user:pass (Base64)]
Content-Length: 29
Cache-Control: max-age=0
Origin: https://[hostname]
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1
Content-Type: application/json
Accept: application/json
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

{"slug":"test","name":"test"}
curl -k -X POST -H "Host: [hostname]" -H "Connection: keep-alive" -H "Authorization: Basic [user:pass(Base64)]" -H  "Content-Length: 29" -H "Cache-Control: max-age=0" -H "Origin: https://[hostname]" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1" -H "Content-Type: application/json" -H "Accept: application/json" -H "Accept-Encoding: gzip,deflate,sdch" -H "Accept-Language: en-US,en;q=0.8" -H "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3" --data {"slug":"test", "name":"test"} https://[hostname]/rest/api/1.0/projects/[projectName]/repos/
wget --no-check-certificate --header='Host: [hostname]' --header 'Connection: keep-alive' --header 'Authorization: Basic [user:pass(Base64)]' --header 'Content-Type: application/json' --header 'Accept: application/json' --post-data='{"slug":"test", "name":"test"}' https://[hostname]/rest/api/1.0/projects/[projectName]/repos/

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Jodi LeBlanc
Posted Thursday in Off-topic

Friday Fun: How do you recharge your batteries after a busy week?

Happy Friday – Fun Day! I don’t know about you, but I have been giving everything I’ve got this week (and most weeks) and come Friday I look for unique ways to recharge my batteries - rest, relax, a...

916 views 29 4
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you