I would like to be able to set the 'Reject Force Push' hook enabled for every repository we currently have, and also to have it set as the default for all repositories created in the future.
Is this possible, or would I need to do it manually in each existing project?
Thanks, I came up with this Python 3 script which loops through the first 100 repositories and sets the hook to enabled:
import urllib.request, json request = urllib.request.Request('http://stash.example.com/rest/api/1.0/repos?limit=100') base64string = 'base64_encoded username:password' request.add_header("Authorization","Basic %s" % base64string) result = urllib.request.urlopen(request) jsonresult = json.loads(result.read().decode('utf-8')) for j in jsonresult['values']: print(j['slug']) enableHookUrl = "http://stash.example.com/rest/api/1.0/projects/%s/repos/%s/settings/hooks/com.atlassian.stash.stash-bundled-hooks:force-push-hook/enabled" % (j['project']['key'], j['slug']) enableRequest = urllib.request.Request(enableHookUrl,data=b"") enableRequest.add_header("Authorization","Basic %s" % base64string) enableRequest.add_header("Content-Type", "application/json") enableRequest.get_method = lambda:'PUT' enableResult = urllib.request.urlopen(enableRequest)
I think this script would enable the hook on user repos as well. not sure you would want to do this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There are REST API to enable hook:
Where:
From description:
Enable a repository hook for this repositories and optionally applying new configuration. The authenticated user must have REPO_ADMIN permission for the specified repository to call this resource.
You can also use REST to get list of all repos/projects in your Stash.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have used an open source python plugin written for Stash API:
https://github.com/RisingOak/stashy
import stashy stash = stashy.connect("http://localhost:7990/stash", "admin", "admin") stash.projects[PROJECT_KEY].repos[REPO_NAME].settings.hooks["com.atlassian.stash.stash-bundled-hooks:force-push-hook"].enable()
You can easily loop through the projects and the repos under those projects. After that, turning this script into a cron job seems to be the only viable solution.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is a bit late, but, I just saw this - it might help others:
You can use Stash Command Line Interface (CLI) action enableHook and combine that with runFromRepositoryList to hit everything.
stash --action runFromProjectList --common "--action runFromRepositoryList --project @project@ --common \"--action enableHook --hook com.atlassian.stash.stash-bundled-hooks:force-push-hook --project @project@ --repository @repository@\" "
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
As an alternative to the above, I have added a ruby version here with a few elements commented out. This one will only set the hook on the main repos, not users forks or other user repos as denoted by the '~' in the key. There is a line at the begining for retriving the repos details but I had trouble with this as it did not appear to be returning proper json to my ruby script (even though it is responding with 'application/json' content) but this is likely my bad coding rather than stash. enjoy
require 'net/http' require 'open-uri' require 'rubygems' require 'json' #content = open("http://stash.example.com/rest/api/1.0/repos?limit=10000").read #json = JSON.parse(content) jsonFile = File.read('repos.json') json = JSON.parse(jsonFile) json['values'].each do |value| puts value['id'] if value['project']['key'].include? '~' # puts "This is a private project" # elsif value['origin']['project']['isPersonal'] == false # puts "key is: #{value['origin']['project']['key']}" # puts "repo is: #{value['origin']['slug']}" else # puts "key is: #{value['project']['key']}" # puts "repo is: #{value['slug']}" hookURL = "http://stash.example.com/rest/api/1.0/projects/#{value['project']['key']}/repos/#{value['slug']}/settings/hooks/com.atlassian.stash.stash-bundled-hooks:force-push-hook/enabled" # puts hookURL uri = URI.parse(hookURL) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Put.new(uri.request_uri) request.basic_auth("user", "pass") response = http.request(request) puts response end end
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.