##// END OF EJS Templates
api: Add api methods to get/set repository settings, implements #4021
Martin Bornhold -
r387:cac3b481 default
parent child Browse files
Show More
@@ -43,7 +43,7 b' from rhodecode.model.db import ('
43 43 from rhodecode.model.repo import RepoModel
44 44 from rhodecode.model.repo_group import RepoGroupModel
45 45 from rhodecode.model.scm import ScmModel, RepoList
46 from rhodecode.model.settings import SettingsModel
46 from rhodecode.model.settings import SettingsModel, VcsSettingsModel
47 47 from rhodecode.model.validation_schema import RepoSchema
48 48
49 49 log = logging.getLogger(__name__)
@@ -1773,3 +1773,110 b' def strip(request, apiuser, repoid, revi'
1773 1773 'Unable to strip commit %s from repo `%s`' % (
1774 1774 revision, repo.repo_name)
1775 1775 )
1776
1777
1778 @jsonrpc_method()
1779 def get_repo_settings(request, apiuser, repoid, key=Optional(None)):
1780 """
1781 Returns all settings for a repository. If key is given it only returns the
1782 setting identified by the key or null.
1783
1784 :param apiuser: This is filled automatically from the |authtoken|.
1785 :type apiuser: AuthUser
1786 :param repoid: The repository name or repository id.
1787 :type repoid: str or int
1788 :param key: Key of the setting to return.
1789 :type: key: Optional(str)
1790
1791 Example output:
1792
1793 .. code-block:: bash
1794
1795 {
1796 "error": null,
1797 "id": 237,
1798 "result": {
1799 "extensions_largefiles": true,
1800 "hooks_changegroup_push_logger": true,
1801 "hooks_changegroup_repo_size": false,
1802 "hooks_outgoing_pull_logger": true,
1803 "phases_publish": "True",
1804 "rhodecode_hg_use_rebase_for_merging": true,
1805 "rhodecode_pr_merge_enabled": true,
1806 "rhodecode_use_outdated_comments": true
1807 }
1808 }
1809 """
1810
1811 # Restrict access to this api method to admins only.
1812 if not has_superadmin_permission(apiuser):
1813 raise JSONRPCForbidden()
1814
1815 try:
1816 repo = get_repo_or_error(repoid)
1817 settings_model = VcsSettingsModel(repo=repo)
1818 settings = settings_model.get_global_settings()
1819 settings.update(settings_model.get_repo_settings())
1820
1821 # If only a single setting is requested fetch it from all settings.
1822 key = Optional.extract(key)
1823 if key is not None:
1824 settings = settings.get(key, None)
1825 except Exception:
1826 msg = 'Failed to fetch settings for repository `{}`'.format(repoid)
1827 log.exception(msg)
1828 raise JSONRPCError(msg)
1829
1830 return settings
1831
1832
1833 @jsonrpc_method()
1834 def set_repo_settings(request, apiuser, repoid, settings):
1835 """
1836 Update repository settings. Returns true on success.
1837
1838 :param apiuser: This is filled automatically from the |authtoken|.
1839 :type apiuser: AuthUser
1840 :param repoid: The repository name or repository id.
1841 :type repoid: str or int
1842 :param settings: The new settings for the repository.
1843 :type: settings: dict
1844
1845 Example output:
1846
1847 .. code-block:: bash
1848
1849 {
1850 "error": null,
1851 "id": 237,
1852 "result": true
1853 }
1854 """
1855 # Restrict access to this api method to admins only.
1856 if not has_superadmin_permission(apiuser):
1857 raise JSONRPCForbidden()
1858
1859 if type(settings) is not dict:
1860 raise JSONRPCError('Settings have to be a JSON Object.')
1861
1862 try:
1863 settings_model = VcsSettingsModel(repo=repoid)
1864
1865 # Merge global, repo and incoming settings.
1866 new_settings = settings_model.get_global_settings()
1867 new_settings.update(settings_model.get_repo_settings())
1868 new_settings.update(settings)
1869
1870 # Update the settings.
1871 inherit_global_settings = new_settings.get(
1872 'inherit_global_settings', False)
1873 settings_model.create_or_update_repo_settings(
1874 new_settings, inherit_global_settings=inherit_global_settings)
1875 Session().commit()
1876 except Exception:
1877 msg = 'Failed to update settings for repository `{}`'.format(repoid)
1878 log.exception(msg)
1879 raise JSONRPCError(msg)
1880
1881 # Indicate success.
1882 return True
General Comments 0
You need to be logged in to leave comments. Login now