File size: 105,307 Bytes
56d74b6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 | # coding=utf-8
import logging
from deprecated import deprecated
from requests import HTTPError
from .base import BitbucketBase
from atlassian.bitbucket.cloud import Cloud
log = logging.getLogger(__name__)
class Bitbucket(BitbucketBase):
def __init__(self, url, *args, **kwargs):
if "cloud" not in kwargs and ("bitbucket.org" in url):
kwargs["cloud"] = True
if "api_version" not in kwargs:
kwargs["api_version"] = "2.0" if "cloud" in kwargs and kwargs["cloud"] else "1.0"
if "cloud" in kwargs:
kwargs["api_root"] = "" if "api.bitbucket.org" in url else "rest/api"
super(Bitbucket, self).__init__(url, *args, **kwargs)
def markup_preview(self, data):
"""
Preview generated HTML for the given markdown content.
Only authenticated users may call this resource.
:param data:
:return:
"""
url = self.resource_url("markup/preview")
return self.post(url, data=data)
################################################################################################
# Administrative functions
################################################################################################
def _url_admin(self, api_version=None):
return self.resource_url("admin", api_version=api_version)
def group_members(self, group, start=0, limit=None):
"""
Get group of members
:param group: The group name to query
:param start:
:param limit:
:return: A list of group members
"""
url = "{}/groups/more-members".format(self._url_admin)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if group:
params["context"] = group
return self._get_paged(url, params=params)
def all_project_administrators(self):
"""
Get the list of project administrators
:return: A generator object containing a map with the project_key, project_name and project_administrators
"""
for project in self.project_list():
log.info("Processing project: {0} - {1}".format(project.get("key"), project.get("name")))
yield {
"project_key": project.get("key"),
"project_name": project.get("name"),
"project_administrators": [
{"email": x["emailAddress"], "name": x["displayName"]}
for x in self.project_users_with_administrator_permissions(project["key"])
],
}
def reindex(self):
"""
Rebuild the bundled Elasticsearch indexes for Bitbucket Server
:return:
"""
url = self.resource_url("sync", api_root="rest/indexing", api_version="latest")
return self.post(url)
def check_reindexing_status(self):
"""
Check reindexing status
:return:
"""
url = self.resource_url("status", api_root="rest/indexing", api_version="latest")
return self.get(url)
def get_users(self, user_filter=None):
"""
Get list of bitbucket users.
Use 'user_filter' for get specific users.
:user_filter: str
"""
url = self.resource_url("users", api_version="1.0")
params = {}
if user_filter:
params["filter"] = user_filter
return self.get(url, params=params)
def get_users_info(self, user_filter=None, start=0, limit=25):
"""
The authenticated user must have the LICENSED_USER permission to call this resource.
:param user_filter: if specified only users with usernames, display name or email addresses
containing the supplied string will be returned
:param limit:
:param start:
:return:
"""
url = "{}/users".format(self._url_admin(api_version="1.0"))
params = {}
if limit:
params["limit"] = limit
if start:
params["start"] = start
if user_filter:
params["filter"] = user_filter
return self._get_paged(url, params=params)
def get_current_license(self):
"""
Retrieves details about the current license, as well as the current status of the system with
regards to the installed license. The status includes the current number of users applied
toward the license limit, as well as any status messages about the license (warnings about expiry
or user counts exceeding license limits).
The authenticated user must have ADMIN permission. Unauthenticated users, and non-administrators,
are not permitted to access license details.
:return:
"""
url = "{}/license".format(self._url_admin())
return self.get(url)
def _url_mail_server(self):
return "{}/mail-server".format(self._url_admin())
def get_mail_configuration(self):
"""
Retrieves the current mail configuration.
The authenticated user must have the SYS_ADMIN permission to call this resource.
:return:
"""
url = self._url_mail_server()
return self.get(url)
def _url_mail_server_sender_address(self):
return "{}/sender-address".format(self._url_mail_server())
def get_mail_sender_address(self):
"""
Retrieves the server email address
:return:
"""
url = self._url_mail_server_sender_address()
return self.get(url)
def remove_mail_sender_address(self):
"""
Clears the server email address.
The authenticated user must have the ADMIN permission to call this resource.
:return:
"""
url = self._url_mail_server_sender_address()
return self.delete(url)
def get_ssh_settings(self):
"""
Retrieve ssh settings for user
:return:
"""
url = self.resource_url("settings", api_root="rest/ssh")
return self.get(url)
def health_check(self):
"""
Get health status
https://confluence.atlassian.com/jirakb/how-to-retrieve-health-check-results-using-rest-api-867195158.html
:return:
"""
# check as Troubleshooting & Support Tools Plugin
response = self.get("rest/troubleshooting/1.0/check/")
if not response:
# check as support tools
response = self.get("rest/supportHealthCheck/1.0/check/")
return response
def get_associated_build_statuses(self, commit):
"""
To get the build statuses associated with a commit.
:commit: str- commit id
:return:
"""
url = self.resource_url("commits/{commitId}".format(commitId=commit), api_root="rest/build-status")
return self.get(url)
def _url_announcement_banner(self):
return "{}/banner".format(self._url_admin())
def get_announcement_banner(self):
"""
Gets the announcement banner, if one exists and is available to the user
:return:
"""
url = self._url_announcement_banner()
return self.get(url)
def set_announcement_banner(self, body):
"""
Sets the announcement banner with the provided JSON.
Only users authenticated as Admins may call this resource
:param body
{
"id": "https://docs.atlassian.com/jira/REST/schema/rest-announcement-banner#",
"title": "Rest Announcement Banner",
"type": "object"
}
:return:
"""
url = self._url_announcement_banner()
return self.put(url, data=body)
def delete_announcement_banner(self):
"""
Gets the announcement banner, if one exists and is available to the user
:return:
"""
url = self._url_announcement_banner()
return self.delete(url)
def upload_plugin(self, plugin_path):
"""
Provide plugin path for upload into BitBucket e.g. useful for auto deploy
:param plugin_path:
:return:
"""
upm_token = self.request(
method="GET",
path="rest/plugins/1.0/",
headers=self.no_check_headers,
trailing=True,
).headers["upm-token"]
url = "rest/plugins/1.0/?token={}".format(upm_token)
files = {"plugin": open(plugin_path, "rb")}
return self.post(url, files=files, headers=self.no_check_headers)
################################################################################################
# Functions related to projects
################################################################################################
def _url_projects(self, api_root=None, api_version=None):
return self.resource_url("projects", api_root, api_version)
def project_list(self, start=0, limit=None):
"""
Provide the project list
:return: A list of projects
"""
url = self._url_projects()
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params=params)
def create_project(self, key, name, description=""):
"""
Create project
:param key: The project key
:param name: The project name
:param description: The project description
:return: The value of the post request.
"""
url = self._url_projects()
data = {"key": key, "name": name, "description": description}
return self.post(url, data=data)
################################################################################################
# Functions related to a specific project
################################################################################################
def _url_project(self, project_key, api_root=None, api_version=None):
return "{}/{}".format(self._url_projects(api_root, api_version), project_key)
def project(self, key):
"""
Provide project info
:param key: The project key
:return:
"""
url = self._url_project(key)
return self.get(url) or {}
def project_exists(self, project_key):
"""
Check if project with the provided project key exists and available.
:param project_key: Key of the project where to check for repository.
:return: False is requested repository doesn't exist in the project or not accessible to the requestor
"""
exists = False
try:
self.project(project_key)
exists = True
except HTTPError as e:
if e.response.status_code in (401, 404):
pass
return exists
def update_project(self, key, **params):
"""
Update project
:param key: The project key
:return: The value of the put request.
"""
url = self._url_project(key)
return self.put(url, data=params)
def _url_project_avatar(self, project_key):
return "{}/avatar.png".format(self._url_project(project_key))
def project_summary(self, key):
"""
Get a project summary
:param key: The project key
:return: Map with the project information
"""
return {
"key": key,
"data": self.project(key),
"users": self.project_users(key),
"groups": self.project_groups(key),
"avatar": self.project_avatar(key),
}
def project_avatar(self, key, content_type="image/png"):
"""
Get project avatar
:param key: The project key
:param content_type: The content type to get
:return: Value of get request
"""
url = self._url_project_avatar(key)
headers = dict(self.default_headers)
headers["Accept"] = content_type
headers["X-Atlassian-Token"] = "no-check"
return self.get(url, not_json_response=True, headers=headers)
def set_project_avatar(self, key, icon, content_type="image/png"):
"""
Set project avatar
:param key: The Project key
:param icon: The icon file
:param content_type: The content type of icon
:return: Value of post request
"""
url = self._url_project_avatar(key)
headers = {"X-Atlassian-Token": "no-check"}
files = {"avatar": ("avatar.png", icon, content_type)}
return self.post(url, files=files, headers=headers)
def project_keys(self, key, start=0, limit=None, filter_str=None):
"""
Get SSH access keys added to the project
:param start:
:param limit:
:param key: The project key
:param filter_str: OPTIONAL: users filter string
:return: The list of SSH access keys
"""
url = "{}/ssh".format(self._url_project(key, api_root="rest/keys"))
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def _url_project_users(self, project_key):
return "{}/permissions/users".format(self._url_project(project_key))
def project_users(self, key, start=0, limit=None, filter_str=None):
"""
Get users who has permission in project
:param key: The project key
:param filter_str: OPTIONAL: users filter string
:param start:
:param limit:
:return: The list of project users
"""
url = self._url_project_users(key)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def project_users_with_administrator_permissions(self, key):
"""
Get project administrators for project
:param key: The project key
:return: List of project administrators
"""
project_administrators = [
user["user"] for user in self.project_users(key) if user["permission"] == "PROJECT_ADMIN"
]
for group in self.project_groups_with_administrator_permissions(key):
for user in self.group_members(group):
project_administrators.append(user)
return project_administrators
def project_grant_user_permissions(self, project_key, username, permission):
"""
Grant the specified project permission to an specific user
:param project_key: The project key
:param username: user name to be granted
:param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
:return:
"""
url = self._url_project_users(project_key)
params = {"permission": permission, "name": username}
return self.put(url, params=params)
def project_remove_user_permissions(self, project_key, username):
"""
Revoke all permissions for the specified project for a user.
The authenticated user must have PROJECT_ADMIN permission for
the specified project or a higher global permission to call this resource.
In addition, a user may not revoke their own project permissions if they do not have a higher global permission.
:param project_key: The project key
:param username: user name to be granted
:return:
"""
url = self._url_project_users(project_key)
params = {"name": username}
return self.delete(url, params=params)
def _url_project_groups(self, project_key):
return "{}/permissions/groups".format(self._url_project(project_key))
def project_groups(self, key, start=0, limit=None, filter_str=None):
"""
Get Project Groups
:param limit:
:param limit:
:param start:
:param key: The project key
:param filter_str: OPTIONAL: group filter string
:return:
"""
url = self._url_project_groups(key)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def project_grant_group_permissions(self, project_key, group_name, permission):
"""
Grant the specified project permission to an specific group
:param project_key: The project key
:param group_name: group to be granted
:param permission: the project permissions available are 'PROJECT_ADMIN', 'PROJECT_WRITE' and 'PROJECT_READ'
:return:
"""
url = self._url_project_groups(project_key)
params = {"permission": permission, "name": group_name}
return self.put(url, params=params)
def project_remove_group_permissions(self, project_key, groupname):
"""
Revoke all permissions for the specified project for a group.
The authenticated user must have PROJECT_ADMIN permission for the specified project
or a higher global permission to call this resource.
In addition, a user may not revoke a group's permissions
if it will reduce their own permission level.
:param project_key: The project key
:param groupname: group to be granted
:return:
"""
url = self._url_project_groups(project_key)
params = {"name": groupname}
return self.delete(url, params=params)
def _url_project_conditions(self, project_key):
return "{}/conditions".format(
self._url_project(project_key, api_root="rest/default-reviewers", api_version="1.0")
)
def get_project_conditions(self, project_key):
"""
Request type: GET
Return a page of defaults conditions with reviewers list that have been configured for this project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264904368
:projectKey: str
:return:
"""
url = self._url_project_conditions(project_key)
return self.get(url) or {}
def _url_project_condition(self, project_key, id_condition=None):
url = "{}/condition".format(
self._url_project(project_key, api_root="rest/default-reviewers", api_version="1.0")
)
if id_condition is not None:
url += "/{}".format(id_condition)
return url
def get_project_condition(self, project_key, id_condition):
"""
Request type: GET
Return a specific condition with reviewers list that has been configured for this project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264901504
:projectKey: str - project key involved
:idCondition: int - condition id involved
:return:
"""
url = self._url_project_condition(project_key, id_condition)
return self.get(url) or {}
def create_project_condition(self, project_key, condition):
"""
Request type: POST
Create a new condition for this project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264893584
:projectKey: str- project key involved
:data: condition: dictionary object
:example condition: '{"sourceMatcher":
{"id":"any",
"type":{"id":"ANY_REF"}},
"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},
"reviewers":[{"id": 12}],"requiredApprovals":"0"
}'
:return:
"""
url = self._url_project_condition(project_key)
return self.post(url, data=condition) or {}
def update_project_condition(self, project_key, condition, id_condition):
"""
Request type: PUT
Update a new condition for this project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632
:projectKey: str- project key involved
:idCondition: int - condition id involved
:data: condition: dictionary object
:example condition: '{"sourceMatcher":
{"id":"any",
"type":{"id":"ANY_REF"}},
"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},
"reviewers":[{"id": 12}],"requiredApprovals":"0"
}'
:return:
"""
url = self._url_project_condition(project_key, id_condition)
return self.put(url, data=condition) or {}
def delete_project_condition(self, project_key, id_condition):
"""
Request type: DELETE
Delete a specific condition for this repository slug inside project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264896304
:projectKey: str- project key involved
:idCondition: int - condition id involved
:return:
"""
url = self._url_project_condition(project_key, id_condition)
return self.delete(url) or {}
def _url_repos(self, project_key, api_root=None, api_version=None):
return "{}/repos".format(self._url_project(project_key, api_root, api_version))
def repo_list(self, project_key, start=0, limit=25):
"""
Get repositories list from project
:param project_key: The project key
:param start:
:param limit:
:return:
"""
url = self._url_repos(project_key)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params=params)
def repo_all_list(self, project_key):
"""
Get all repositories list from project
:param project_key:
:return:
"""
return self.repo_list(project_key, limit=None)
def create_repo(self, project_key, repository_slug, forkable=False, is_private=True):
"""Create a new repository.
Requires an existing project in which this repository will be created. The only parameters which will be used
are name and scmId.
The authenticated user must have PROJECT_ADMIN permission for the context project to call this resource.
:param project_key: The project matching the projectKey supplied in the resource path as shown in URL.
:type project_key: str
:param repository_slug: Name of repository to create (i.e. "My repo").
:param forkable: Set the repository to be forkable or not.
:type forkable: bool
:param is_private: Set the repository to be private or not.
:type is_private: bool
:return:
201 - application/json (repository)
400 - application/json (errors)
401 - application/json (errors)
409 - application/json (errors)
:rtype: requests.Response
"""
url = self._url_repos(project_key)
data = {
"name": repository_slug,
"scmId": "git",
"forkable": forkable,
"is_private": is_private,
}
return self.post(url, data=data)
################################################################################################
# Functions related to a specific repository
################################################################################################
def _url_repo(self, project_key, repo, api_root=None, api_version=None):
return "{}/{}".format(self._url_repos(project_key, api_root, api_version), repo)
def reindex_repo(self, project_key, repository_slug):
"""
Reindex repo
:param project_key:
:param repository_slug:
:return:
"""
url = "{urlRepo}/sync".format(
urlRepo=self._url_repo(
project_key,
repository_slug,
api_root="rest/indexing",
api_version="1.0",
)
)
return self.post(url)
def reindex_repo_dev_panel(self, project_key, repository_slug):
"""
Reindex all of the Jira issues related to this repository_slug, including branches and pull requests.
This automatically happens as part of an upgrade, and calling this manually should only be required
if something unforeseen happens and the index becomes out of sync.
The authenticated user must have REPO_ADMIN permission for the specified repository to call this resource.
:param project_key:
:param repository_slug:
:return:
"""
url = "{}/reindex".format(self._url_repo(project_key, repository_slug, api_root="rest/jira-dev"))
return self.post(url)
def get_repo(self, project_key, repository_slug):
"""
Get a specific repository from a project. This operates based on slug not name which may
be confusing to some users.
:param project_key: Key of the project you wish to look in.
:param repository_slug: url-compatible repository identifier
:return: Dictionary of request response
"""
url = self._url_repo(project_key, repository_slug)
return self.get(url)
def repo_exists(self, project_key, repository_slug):
"""
Check if given combination of project and repository exists and available.
:param project_key: Key of the project where to check for repository.
:param repository_slug: url-compatible repository identifier to look for.
:return: False is requested repository doesn't exist in the project or not accessible to the requestor
"""
exists = False
try:
self.get_repo(project_key, repository_slug)
exists = True
except HTTPError as e:
if e.response.status_code in (401, 404):
pass
return exists
def update_repo(self, project_key, repository_slug, **params):
"""
Update a repository in a project. This operates based on slug not name which may
be confusing to some users.
:param project_key: Key of the project you wish to look in.
:param repository_slug: url-compatible repository identifier
:return: The value of the put request.
"""
url = self._url_repo(project_key, repository_slug)
return self.put(url, data=params)
def delete_repo(self, project_key, repository_slug):
"""
Delete a specific repository from a project. This operates based on slug not name which may
be confusing to some users.
:param project_key: Key of the project you wish to look in.
:param repository_slug: url-compatible repository identifier
:return: Dictionary of request response
"""
url = self._url_repo(project_key, repository_slug)
return self.delete(url)
def fork_repository(self, project_key, repository_slug, new_repository_slug):
"""
Forks a repository within the same project.
:param project_key:
:param repository_slug:
:param new_repository_slug:
:return:
"""
url = self._url_repo(project_key, repository_slug)
body = {}
if new_repository_slug is not None:
body["name"] = new_repository_slug
if new_repository_slug is not None:
body["project"] = {"key": project_key}
return self.post(url, data=body)
def repo_keys(self, project_key, repo_key, start=0, limit=None, filter_str=None):
"""
Get SSH access keys added to the repository
:param start:
:param limit:
:param project_key: The project key
:param repo_key: The repository key
:param filter_str: OPTIONAL: users filter string
:return:
"""
url = "{}/ssh".format(self._url_repo(project_key, repo_key, api_root="rest/keys"))
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def _url_repo_users(self, project_key, repo):
return "{}/permissions/users".format(self._url_repo(project_key, repo))
def repo_users(self, project_key, repo_key, start=0, limit=None, filter_str=None):
"""
Get users who has permission in repository
:param start:
:param limit:
:param project_key: The project key
:param repo_key: The repository key
:param filter_str: OPTIONAL: Users filter string
:return:
"""
url = self._url_repo_users(project_key, repo_key)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def repo_grant_user_permissions(self, project_key, repo_key, username, permission):
"""
Grant the specified repository permission to an specific user
:param project_key: The project key
:param repo_key: The repository key (slug)
:param username: user name to be granted
:param permission: the repository permissions available are 'REPO_ADMIN', 'REPO_WRITE' and 'REPO_READ'
:return:
"""
url = self._url_repo_users(project_key, repo_key)
params = {"permission": permission, "name": username}
return self.put(url, params=params)
def repo_remove_user_permissions(self, project_key, repo_key, username):
"""
Revoke all permissions for the specified repository for a user.
The authenticated user must have REPO_ADMIN permission for the specified repository
or a higher project or global permission to call this resource.
In addition, a user may not revoke their own repository permissions
if they do not have a higher project or global permission.
:param project_key: The project key
:param repo_key: The repository key (slug)
:param username: user name to be granted
:return:
"""
url = self._url_repo_users(project_key, repo_key)
params = {"name": username}
return self.delete(url, params=params)
def _url_repo_groups(self, project_key, repo):
return "{}/permissions/groups".format(self._url_repo(project_key, repo))
def repo_groups(self, project_key, repo_key, start=0, limit=None, filter_str=None):
"""
Get repository Groups
:param start:
:param limit:
:param project_key: The project key
:param repo_key: The repository key
:param filter_str: OPTIONAL: group filter string
:return:
"""
url = self._url_repo_groups(project_key, repo_key)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter_str:
params["filter"] = filter_str
return self._get_paged(url, params=params)
def project_groups_with_administrator_permissions(self, key):
"""
Get groups with admin permissions
:param key:
:return:
"""
return [group["group"]["name"] for group in self.project_groups(key) if group["permission"] == "PROJECT_ADMIN"]
def repo_grant_group_permissions(self, project_key, repo_key, groupname, permission):
"""
Grant the specified repository permission to an specific group
Promote or demote a group's permission level for the specified repository. Available repository permissions are:
REPO_READ
REPO_WRITE
REPO_ADMIN
See the Bitbucket Server documentation for a detailed explanation of what each permission entails.
The authenticated user must have REPO_ADMIN permission for the specified repository or a higher project
or global permission to call this resource.
In addition, a user may not demote a group's permission level
if their own permission level would be reduced as a result.
:param project_key: The project key
:param repo_key: The repository key (slug)
:param groupname: group to be granted
:param permission: the repository permissions available are 'REPO_ADMIN', 'REPO_WRITE' and 'REPO_READ'
:return:
"""
url = self._url_repo_groups(project_key, repo_key)
params = {"permission": permission, "name": groupname}
return self.put(url, params=params)
def repo_remove_group_permissions(self, project_key, repo_key, groupname, permission):
"""
Revoke all permissions for the specified repository for a group.
The authenticated user must have REPO_ADMIN permission for the specified repository
or a higher project or global permission to call this resource.
In addition, a user may not revoke a group's permissions if it will reduce their own permission level.
:param project_key: The project key
:param repo_key: The repository key (slug)
:param groupname: group to be granted
:param permission: the repository permissions available are 'REPO_ADMIN', 'REPO_WRITE' and 'REPO_READ'
:return:
"""
url = self._url_repo_groups(project_key, repo_key)
params = {"name": groupname}
if permission:
params["permission"] = permission
return self.delete(url, params=params)
def _url_repo_labels(self, project_key, repository_slug):
if self.cloud:
raise Exception("Not supported in Bitbucket Cloud")
return "{}/labels".format(self._url_repo(project_key, repository_slug))
def get_repo_labels(self, project_key, repository_slug):
"""
Get labels for a specific repository from a project. This operates based on slug not name which may
be confusing to some users. (BitBucket Server only)
:param project_key: Key of the project you wish to look in.
:param repository_slug: url-compatible repository identifier
:return: Dictionary of request response
"""
url = self._url_repo_labels(project_key, repository_slug)
return self.get(url)
def set_repo_label(self, project_key, repository_slug, label_name):
"""
Sets a label on a repository. (BitBucket Server only)
The authenticated user must have REPO_ADMIN permission for the specified repository to call this resource.
:param project_key: Key of the project you wish to look in.
:param repository_slug: url-compatible repository identifier
:param label_name: label name to apply
:return:
"""
url = self._url_repo_labels(project_key, repository_slug)
data = {"name": label_name}
return self.post(url, data=data)
def _url_repo_branches(self, project_key, repository_slug, api_root=None):
return "{}/branches".format(self._url_repo(project_key, repository_slug, api_root=api_root))
def get_branches(
self,
project_key,
repository_slug,
base=None,
filter=None,
start=0,
limit=None,
details=True,
order_by="MODIFICATION",
):
"""
Retrieve the branches matching the supplied filterText param.
The authenticated user must have REPO_READ permission for the specified repository to call this resource.
:param start:
:param project_key:
:param repository_slug:
:param base: base branch/tag to compare each branch to (for the metadata providers that uses that information)
:param filter:
:param limit: OPTIONAL: The limit of the number of branches to return, this may be restricted by
fixed system limits. Default by built-in method: None
:param details: whether to retrieve plugin-provided metadata about each branch
:param order_by: OPTIONAL: ordering of refs either ALPHABETICAL (by name) or MODIFICATION (last updated)
:return:
"""
url = self._url_repo_branches(project_key, repository_slug)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter:
params["filterText"] = filter
if base:
params["base"] = base
if order_by:
params["orderBy"] = order_by
params["details"] = details
return self._get_paged(url, params=params)
def _url_repo_default_branche(self, project_key, repository_slug):
return "{}/default".format(self._url_repo_branches(project_key, repository_slug))
def get_default_branch(self, project_key, repository_slug):
"""
Get the default branch of the repository.
The authenticated user must have REPO_READ permission for the specified repository to call this resource.
:param project_key: The project key
:param repository_slug: The repository key
:return:
"""
url = self._url_repo_default_branche(project_key, repository_slug)
return self.get(url)
def set_default_branch(self, project_key, repository_slug, ref_branch_name):
"""
Update the default branch of a repository.
The authenticated user must have REPO_ADMIN permission for the specified repository to call this resource.
:param project_key: The project key
:param repository_slug: The repository key (slug)
:param ref_branch_name: ref name like refs/heads/master
:return:
"""
url = self._url_repo_default_branche(project_key, repository_slug)
data = {"id": ref_branch_name}
return self.put(url, data=data)
def create_branch(self, project_key, repository_slug, name, start_point, message=""):
"""Creates a branch using the information provided in the request.
The authenticated user must have REPO_WRITE permission for the context repository to call this resource.
:param project_key: The project matching the projectKey supplied in the resource path as shown in URL.
:type project_key: str
:param repository_slug: Name of repository where branch is created (i.e. "my_repo").
:param name: Name of branch to create (i.e. "my_branch").
:type name: str
:param start_point: Name of branch to branch from.
:type start_point: str
:param message: Branch message.
:type message: str
:return:
200 - application/json (repository)
401 - application/json (errors)
404 - application/json (errors)
:rtype: requests.Response
"""
url = self._url_repo_branches(project_key, repository_slug)
data = {"name": name, "startPoint": start_point, "message": message}
return self.post(url, data=data)
def delete_branch(self, project_key, repository_slug, name, end_point=None):
"""
Delete branch from related repo
:param self:
:param project_key:
:param repository_slug:
:param name:
:param end_point:
:return:
"""
url = self._url_repo_branches(project_key, repository_slug, api_root="rest/branch-utils")
data = {"name": str(name)}
if end_point:
data["endPoint"] = end_point
return self.delete(url, data=data)
def _url_repo_tags(self, project_key, repository_slug, api_root=None):
if self.cloud:
return "{}/refs/tags".format(self._url_repo(project_key, repository_slug, api_root=api_root))
else:
return "{}/tags".format(self._url_repo(project_key, repository_slug, api_root=api_root))
def get_tags(
self,
project_key,
repository_slug,
filter="",
limit=1000,
order_by=None,
start=0,
):
"""
Retrieve the tags matching the supplied filterText param.
The authenticated user must have REPO_READ permission for the context repository to call this resource.
:param project_key:
:param repository_slug:
:param filter:
:param start:
:param limit: OPTIONAL: The limit of the number of tags to return, this may be restricted by
fixed system limits. Default by built-in method: 1000
:param order_by: OPTIONAL: ordering of refs either ALPHABETICAL (by name) or MODIFICATION (last updated)
:return:
"""
url = self._url_repo_tags(project_key, repository_slug)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
if filter:
params["filterText"] = filter
if order_by:
params["orderBy"] = order_by
return self._get_paged(url, params=params)
def get_project_tags(self, project_key, repository_slug, tag_name):
"""
Retrieve a tag in the specified repository.
The authenticated user must have REPO_READ permission for the context repository to call this resource.
Search uri is api/1.0/projects/{projectKey}/repos/{repositorySlug}/tags/{name:.*}
:param project_key:
:param repository_slug:
:param tag_name: OPTIONAL:
:return:
"""
url = self._url_repo_tags(project_key, repository_slug)
return self.get(url)
def set_tag(self, project_key, repository_slug, tag_name, commit_revision, description=None):
"""
Creates a tag using the information provided in the {@link RestCreateTagRequest request}
The authenticated user must have REPO_WRITE permission for the context repository to call this resource.
:param project_key:
:param repository_slug:
:param tag_name:
:param commit_revision: commit hash
:param description: OPTIONAL:
:return:
"""
url = self._url_repo_tags(project_key, repository_slug)
body = {}
if tag_name is not None:
body["name"] = tag_name
if tag_name is not None:
body["startPoint"] = commit_revision
if tag_name is not None:
body["message"] = description
return self.post(url, data=body)
def delete_tag(self, project_key, repository_slug, tag_name):
"""
Creates a tag using the information provided in the {@link RestCreateTagRequest request}
The authenticated user must have REPO_WRITE permission for the context repository to call this resource.
:param project_key:
:param repository_slug:
:param tag_name:
:return:
"""
url = "{}/{}".format(
self._url_repo_tags(project_key, repository_slug, api_root="rest/git"),
tag_name,
)
(project_key, repository_slug, tag_name)
return self.delete(url)
def _url_pull_request_settings(self, project_key, repository_slug):
return "{}/settings/pull-requests".format(self._url_repo(project_key, repository_slug))
def get_pull_request_settings(self, project_key, repository_slug):
"""
Get pull request settings.
:param project_key:
:param repository_slug:
:return:
"""
url = self._url_pull_request_settings(project_key, repository_slug)
return self.get(url)
def set_pull_request_settings(self, project_key, repository_slug, data):
"""
Set pull request settings.
:param project_key:
:param repository_slug:
:param data: json body
:return:
"""
url = self._url_pull_request_settings(project_key, repository_slug)
return self.post(url, data=data)
def _url_pull_requests(self, project_key, repository_slug):
if self.cloud:
return self.resource_url("repositories/{}/{}/pullrequests".format(project_key, repository_slug))
else:
return "{}/pull-requests".format(self._url_repo(project_key, repository_slug))
def get_pull_requests(
self,
project_key,
repository_slug,
state="OPEN",
order="newest",
limit=100,
start=0,
at=None,
):
"""
Get pull requests
:param project_key:
:param repository_slug:
:param state:
:param order: OPTIONAL: defaults to NEWEST) the order to return pull requests in, either OLDEST
(as in: "oldest first") or NEWEST.
:param limit:
:param start:
:param at:
:return:
"""
url = self._url_pull_requests(project_key, repository_slug)
params = {}
if state:
params["state"] = state
if limit:
params["limit"] = limit
if start:
params["start"] = start
if order:
params["order"] = order
if at:
params["at"] = at
return self._get_paged(url, params=params)
def open_pull_request(
self,
source_project,
source_repo,
dest_project,
dest_repo,
source_branch,
destination_branch,
title,
description,
reviewers=None,
):
"""
Create a new pull request between two branches.
The branches may be in the same repository_slug, or different ones.
When using different repositories, they must still be in the same {@link Repository#getHierarchyId() hierarchy}.
The authenticated user must have REPO_READ permission for the "from" and "to"repositories to call this resource.
:param source_project: the project that the PR source is from
:param source_repo: the repository that the PR source is from
:param source_branch: the branch name of the PR
:param dest_project: the project that the PR destination is from
:param dest_repo: the repository that the PR destination is from
:param destination_branch: where the PR is being merged into
:param title: the title of the PR
:param description: the description of what the PR does
:param reviewers: the list of reviewers or a single reviewer of the PR
:return:
"""
body = {
"title": title,
"description": description,
"fromRef": {
"id": source_branch,
"repository": {
"slug": source_repo,
"name": source_repo,
"project": {"key": source_project},
},
},
"toRef": {
"id": destination_branch,
"repository": {
"slug": dest_repo,
"name": dest_repo,
"project": {"key": dest_project},
},
},
"reviewers": [],
}
def add_reviewer(reviewer_name):
entry = {"user": {"name": reviewer_name}}
body["reviewers"].append(entry)
if reviewers is not None:
if isinstance(reviewers, str):
add_reviewer(reviewers)
elif isinstance(reviewers, list):
for reviewer in reviewers:
add_reviewer(reviewer)
return self.create_pull_request(dest_project, dest_repo, body)
def create_pull_request(self, project_key, repository_slug, data):
"""
:param project_key:
:param repository_slug:
:param data: json body
:return:
"""
url = self._url_pull_requests(project_key, repository_slug)
return self.post(url, data=data)
def _url_pull_request(self, project_key, repository_slug, pull_request_id):
return "{}/{}".format(self._url_pull_requests(project_key, repository_slug), pull_request_id)
def get_pull_request(self, project_key, repository_slug, pull_request_id):
"""
Retrieve a pull request.
The authenticated user must have REPO_READ permission
for the repository that this pull request targets to call this resource.
:param project_key:
:param repository_slug:
:param pull_request_id: the ID of the pull request within the repository
:return:
"""
url = self._url_pull_request(project_key, repository_slug, pull_request_id)
return self.get(url)
@deprecated(version="1.15.1", reason="Use get_pull_request()")
def get_pullrequest(self, *args, **kwargs):
"""
Deprecated name since 1.15.1. Let's use the get_pull_request()
"""
return self.get_pull_request(*args, **kwargs)
def delete_pull_request(self, project_key, repository_slug, pull_request_id, pull_request_version):
"""
Delete a pull request.
:param project_key: the project key
:param repository_slug: the repository slug
:param pull_request_id: the ID of the pull request within the repository
:param pull_request_version: the version of the pull request
:return:
"""
url = self._url_pull_request(project_key, repository_slug, pull_request_id)
data = {"version": pull_request_version}
return self.delete(url, data=data)
def get_pull_requests_activities(self, project_key, repository_slug, pull_request_id, start=0, limit=None):
"""
Get pull requests activities
:param limit:
:param project_key:
:param repository_slug:
:param pull_request_id: the ID of the pull request within the repository
:param start:
:return:
"""
url = "{}/activities".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params)
def get_pull_requests_changes(self, project_key, repository_slug, pull_request_id, start=0, limit=None):
"""
Get pull requests changes
:param start:
:param limit:
:param project_key:
:param repository_slug:
:param pull_request_id: the ID of the pull request within the repository
:return:
"""
url = "{}/changes".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params)
def get_pull_requests_commits(self, project_key, repository_slug, pull_request_id, start=0, limit=None):
"""
Get pull requests commits
:param start:
:param limit:
:param project_key:
:param repository_slug:
:param pull_request_id: the ID of the pull request within the repository
:start
:limit
:return:
"""
url = "{}/commits".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params)
def _url_pull_request_participants(self, project_key, repository_slug, pull_request_id):
return "{}/{}/participants".format(self._url_pull_requests(project_key, repository_slug), pull_request_id)
def get_pull_requests_participants(self, project_key, repository_slug, pull_request_id, start=0, limit=None):
"""
Get all participants of a pull request
:param start:
:param limit:
:param project_key:
:param repository_slug:
:param pull_request_id:
:return:
"""
url = self._url_pull_request_participants(project_key, repository_slug, pull_request_id)
params = {}
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params)
def change_reviewed_status(self, project_key, repository_slug, pull_request_id, status, user_slug):
"""
Change the current user's status for a pull request.
Implicitly adds the user as a participant if they are not already.
If the current user is the author, this method will fail.
:param project_key
:param repository_slug:
:param pull_request_id:
:param status:
:param user_slug:
:return:
"""
url = "{}/{}".format(
self._url_pull_request_participants(project_key, repository_slug, pull_request_id),
user_slug,
)
approved = True if status == "APPROVED" else False
data = {"user": {"name": user_slug}, "approved": approved, "status": status}
return self.put(url, data)
def _url_pull_request_comments(self, project_key, repository_slug, pull_request_id):
url = "{}/comments".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
return url
def add_pull_request_comment(self, project_key, repository_slug, pull_request_id, text, parent_id=None):
"""
Add comment into pull request
:param project_key:
:param repository_slug:
:param pull_request_id: the ID of the pull request within the repository
:param text comment text
:param parent_id parent comment id
:return:
"""
url = self._url_pull_request_comments(project_key, repository_slug, pull_request_id)
body = {"text": text}
if parent_id:
body["parent"] = {"id": parent_id}
return self.post(url, data=body)
def _url_pull_request_comment(self, project_key, repository_slug, pull_request_id, comment_id):
url = "{}/{}".format(
self._url_pull_request_comments(project_key, repository_slug, pull_request_id),
comment_id,
)
return url
def get_pull_request_comment(self, project_key, repository_slug, pull_request_id, comment_id):
"""
Retrieves a pull request comment.
The authenticated user must have REPO_READ permission
for the repository that this pull request targets to call this resource.
:param project_key:
:param repository_slug:
:param pull_request_id: the ID of the pull request within the repository
:param comment_id: the ID of the comment to retrieve
:return:
"""
url = self._url_pull_request_comment(project_key, repository_slug, pull_request_id, comment_id)
return self.get(url)
def update_pull_request_comment(
self,
project_key,
repository_slug,
pull_request_id,
comment_id,
comment,
comment_version,
):
"""
Update the text of a comment.
Only the user who created a comment may update it.
Note: the supplied supplied JSON object must contain a version
that must match the server's version of the comment
or the update will fail.
"""
url = self._url_pull_request_comment(project_key, repository_slug, pull_request_id, comment_id)
data = {"version": comment_version, "text": comment}
return self.put(url, data=data)
@deprecated(version="2.4.2", reason="Use delete_pull_request_comment()")
def delete_pull_reques_comment(self, project_key, repository_slug, pull_request_id, comment_id, comment_version):
"""
Deprecated name since 2.4.2. Let's use the get_pull_request()
"""
return self.delete_pull_request_comment(
project_key, repository_slug, pull_request_id, comment_id, comment_version
)
def delete_pull_request_comment(self, project_key, repository_slug, pull_request_id, comment_id, comment_version):
"""
Delete a comment.
Only the repository admin or user who created a comment may update it.
Note: the supplied supplied JSON object must contain a version
that must match the server's version of the comment
or the delete will fail.
"""
url = self._url_pull_request_comment(project_key, repository_slug, pull_request_id, comment_id)
data = {"version": comment_version}
return self.delete(url, params=data)
def decline_pull_request(self, project_key, repository_slug, pr_id, pr_version):
"""
Decline a pull request.
The authenticated user must have REPO_READ permission for the repository
that this pull request targets to call this resource.
:param project_key: PROJECT
:param repository_slug: my_shiny_repo
:param pr_id: 2341
:param pr_version: 12
:return:
"""
url = "{}/decline".format(self._url_pull_request(project_key, repository_slug, pr_id))
params = {}
if not self.cloud:
params["version"] = pr_version
return self.post(url, params=params)
def get_tasks(self, project_key, repository_slug, pull_request_id):
"""
Get all tasks for the pull request
:param project_key:
:param repository_slug:
:param pull_request_id: the ID of the pull request within the repository
:return:
"""
if self.cloud:
raise Exception("Not supported in Bitbucket Cloud")
url = "{}/tasks".format(self._url_pull_request(project_key, repository_slug, pull_request_id))
return self.get(url)
def _url_tasks(self):
if self.cloud:
raise Exception("Not supported in Bitbucket Cloud")
return self.resource_url("tasks")
def add_task(self, anchor, text):
"""
Add task to the comment
:param anchor: ID of the comment,
:param text: task text
:return:
"""
url = self._url_tasks()
data = {"anchor": {"id": anchor, "type": "COMMENT"}, "text": text}
return self.post(url, data=data)
def _url_task(self, task_id):
return "{}/{}".format(self._url_tasks(), task_id)
def get_task(self, task_id):
"""
Get task information by ID
:param task_id:
:return:
"""
url = self._url_task(task_id)
return self.get(url)
def delete_task(self, task_id):
"""
Delete task by ID
:param task_id:
:return:
"""
url = self._url_task(task_id)
return self.delete(url)
def update_task(self, task_id, text=None, state=None):
"""
Update task by ID. It is possible to update state and/or text of the task
:param task_id:
:param text:
:param state: OPEN, RESOLVED
:return:
"""
url = self._url_task(task_id)
data = {"id": task_id}
if text:
data["text"] = text
if state:
data["state"] = state
return self.put(url, data=data)
def is_pull_request_can_be_merged(self, project_key, repository_slug, pr_id):
"""
Test whether a pull request can be merged.
A pull request may not be merged if:
- there are conflicts that need to be manually resolved before merging; and/or
- one or more merge checks have vetoed the merge.
The authenticated user must have REPO_READ permission for the repository
that this pull request targets to call this resource.
:param project_key: PROJECT
:param repository_slug: my_shiny_repo
:param pr_id: 2341
:return:
"""
url = "{}/merge".format(self._url_pull_request(project_key, repository_slug, pr_id))
return self.get(url)
def merge_pull_request(self, project_key, repository_slug, pr_id, pr_version):
"""
Merge pull request
The authenticated user must have REPO_READ permission for the repository
that this pull request targets to call this resource.
:param project_key: PROJECT
:param repository_slug: my_shiny_repo
:param pr_id: 2341
:param pr_version:
:return:
"""
url = "{}/merge".format(self._url_pull_request(project_key, repository_slug, pr_id))
params = {}
if not self.cloud:
params["version"] = pr_version
return self.post(url, params=params)
def reopen_pull_request(self, project_key, repository_slug, pr_id, pr_version):
"""
Re-open a declined pull request.
The authenticated user must have REPO_READ permission for the repository
that this pull request targets to call this resource.
:param project_key: PROJECT
:param repository_slug: my_shiny_repo
:param pr_id: 2341
:param pr_version: 12
:return:
"""
url = "{}/reopen".format(self._url_pull_request(project_key, repository_slug, pr_id))
params = {"version": pr_version}
return self.post(url, params=params)
def _url_inbox_pull_requests(self):
return "inbox/pull-requests"
def check_inbox_pull_requests_count(self):
url = "{}/count".format(self._url_inbox_pull_requests())
return self.get(url)
def check_inbox_pull_requests(self, start=0, limit=None, role=None):
"""
Get pull request in your inbox
:param start:
:param limit:
:param role:
:return:
"""
url = self._url_inbox_pull_requests()
params = {"start": start}
if limit:
params["limit"] = limit
if role:
params["role"] = role
return self.get(url, params=params)
def _url_repo_compare(self, project_key, repository_slug):
url = "{}/compare".format(self._url_repo(project_key, repository_slug))
return url
def get_diff(self, project_key, repository_slug, path, hash_oldest, hash_newest):
"""
Gets a diff of the changes available in the {@code from} commit but not in the {@code to} commit.
If either the {@code from} or {@code to} commit are not specified,
they will be replaced by the default branch of their containing repository.
:param project_key:
:param repository_slug:
:param path:
:param hash_oldest: the source commit (can be a partial/full commit ID or qualified/unqualified ref name)
:param hash_newest: the target commit (can be a partial/full commit ID or qualified/unqualified ref name)
:return:
"""
url = "{}/diff/{}".format(self._url_repo_compare(project_key, repository_slug), path)
params = {}
if hash_oldest:
params["from"] = hash_oldest
if hash_newest:
params["to"] = hash_newest
return (self.get(url, params=params) or {}).get("diffs")
def _url_commits(self, project_key, repository_slug, api_root=None, api_version=None):
return "{}/commits".format(
self._url_repo(project_key, repository_slug, api_root=api_root, api_version=api_version)
)
def get_commits(
self,
project_key,
repository_slug,
hash_oldest=None,
hash_newest=None,
follow_renames=False,
ignore_missing=False,
merges="include",
with_counts=False,
avatar_size=None,
avatar_scheme=None,
limit=None,
):
"""
Get commit list from repo
:param project_key:
:param repository_slug:
:param hash_oldest:
:param hash_newest:
:param merges: OPTIONAL: include|exclude|only if present, controls how merge commits should be filtered.
:param follow_renames: OPTIONAL: if true, the commit history of the specified file will be followed past renames
:param ignore_missing: OPTIONAL: true to ignore missing commits, false otherwise
:param with_counts: OPTIONAL: optionally include the total number of commits and total number of unique authors
:param avatar_size: OPTIONAL: if present the service adds avatar URLs for commit authors.
:param avatar_scheme: OPTIONAL: the desired scheme for the avatar URL
:param limit: OPTIONAL: The limit of the number of commits to return, this may be restricted by
fixed system limits. Default by built-in method: None
:return:
"""
url = self._url_commits(project_key, repository_slug)
params = {"merges": merges}
if hash_oldest:
params["since"] = hash_oldest
if hash_newest:
params["until"] = hash_newest
if follow_renames:
params["followRenames"] = follow_renames
if ignore_missing:
params["ignoreMissing"] = ignore_missing
if with_counts:
params["withCounts"] = with_counts
if avatar_size:
params["avatarSize"] = avatar_size
if avatar_scheme:
params["avatarScheme"] = avatar_scheme
if limit:
params["limit"] = limit
return (self.get(url, params=params) or {}).get("values")
def get_changelog(self, project_key, repository_slug, ref_from, ref_to, start=0, limit=None):
"""
Get change log between 2 refs
:param start:
:param project_key:
:param repository_slug:
:param ref_from:
:param ref_to:
:param limit: OPTIONAL: The limit of the number of changes to return, this may be restricted by
fixed system limits. Default by built-in method: None
:return:
"""
url = self._url_commits(project_key, repository_slug)
params = {}
if ref_from:
params["from"] = ref_from
if ref_to:
params["to"] = ref_to
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params=params)
def _url_commit(self, project_key, repository_slug, commit_id, api_root=None, api_version=None):
return "{}/{}".format(
self._url_commits(project_key, repository_slug, api_root=api_root, api_version=api_version),
commit_id,
)
def get_commit_info(self, project_key, repository_slug, commit, path=None):
"""
Retrieve a single commit identified by its ID>. In general, that ID is a SHA1.
From 2.11, ref names like "refs/heads/master" are no longer accepted by this resource.
The authenticated user must have REPO_READ permission for the specified repository to call this resource.
:param project_key:
:param repository_slug:
:param commit: the commit ID to retrieve
:param path :OPTIONAL an optional path to filter the commit by.
If supplied the details returned may not be for the specified commit.
Instead, starting from the specified commit, they will be the details for the first commit
affecting the specified path.
:return:
"""
url = self._url_commit(project_key, repository_slug, commit)
params = {}
if path:
params["path"] = path
return self.get(url, params=params)
def get_pull_requests_contain_commit(self, project_key, repository_slug, commit):
url = self._url_commit(project_key, repository_slug, commit)
return (self.get(url) or {}).get("values")
def _url_code_insights_report(self, project_key, repository_slug, commit_id, report_key):
return "{}/reports/{}".format(
self._url_commit(
project_key,
repository_slug,
commit_id,
api_root="rest/insights",
api_version="1.0",
),
report_key,
)
def get_code_insights_report(self, project_key, repository_slug, commit_id, report_key):
"""
Retrieve the specified code-insights report.
:projectKey: str
:repositorySlug: str
:commitId: str
:report_key: str
"""
url = self._url_code_insights_report(project_key, repository_slug, commit_id, report_key)
return self.get(url)
def delete_code_insights_report(self, project_key, repository_slug, commit_id, report_key):
"""
Delete a report for the given commit. Also deletes any annotations associated with this report.
:projectKey: str
:repositorySlug: str
:commitId: str
:report_key: str
"""
url = self._url_code_insights_report(project_key, repository_slug, commit_id, report_key)
return self.delete(url)
def create_code_insights_report(
self, project_key, repository_slug, commit_id, report_key, report_title, **report_params
):
"""
Create a new insight report, or replace the existing one
if a report already exists for the given repository_slug, commit, and report key.
A request to replace an existing report will be rejected
if the authenticated user was not the creator of the specified report.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/6.6.1/bitbucket-code-insights-rest.html
:projectKey: str
:repositorySlug: str
:commitId: str
:report_key: str
:report_title: str
:report_params:
"""
url = self._url_code_insights_report(project_key, repository_slug, commit_id, report_key)
data = {"title": report_title}
data.update(report_params)
return self.put(url, data=data)
def get_file_list(self, project_key, repository_slug, query=None, start=0, limit=None):
"""
Retrieve a page of files from particular directory of a repository.
The search is done recursively, so all files from any sub-directory of the specified directory will be returned.
The authenticated user must have REPO_READ permission for the specified repository to call this resource.
:param start:
:param project_key:
:param repository_slug:
:param query: the commit ID or ref (e.g. a branch or tag) to list the files at.
If not specified the default branch will be used instead.
:param limit: OPTIONAL
:return:
"""
url = "{}/files".format(self._url_repo(project_key, repository_slug))
params = {}
if query:
params["at"] = query
if start:
params["start"] = start
if limit:
params["limit"] = limit
return self._get_paged(url, params=params)
def get_content_of_file(self, project_key, repository_slug, filename, at=None, markup=None):
"""
Retrieve the raw content for a file path at a specified revision.
The authenticated user must have REPO_READ permission for the specified repository to call this resource.
:param project_key:
:param repository_slug:
:param filename:
:param at: OPTIONAL ref string
:param markup: if present or "true", triggers the raw content to be markup-rendered and returned as HTML;
otherwise, if not specified, or any value other than "true" the content is streamed without markup.
:return:
"""
url = "{}/raw/{}".format(self._url_repo(project_key, repository_slug), filename)
params = {}
if at is not None:
params["at"] = at
if markup is not None:
params["markup"] = markup
headers = self.form_token_headers
return self.get(url, params=params, not_json_response=True, headers=headers)
def _url_branches_permissions(self, project_key, permission_id=None, repository_slug=None):
if repository_slug is None:
base = self._url_project(project_key, api_root="rest/branch-permissions", api_version="2.0")
else:
base = self._url_repo(
project_key,
repository_slug,
api_root="rest/branch-permissions",
api_version="2.0",
)
return "{}/restrictions/{}".format(base, "" if permission_id is None else str(permission_id))
def get_branches_permissions(self, project_key, permission_id, repository_slug=None, start=0, limit=25):
"""
Get branches permissions from a given repo
:param project_key:
:param permission_id:
:param repository_slug:
:param start:
:param limit:
:return:
"""
url = self._url_branches_permissions(project_key, permission_id, repository_slug)
params = {}
if limit:
params["limit"] = limit
if start:
params["start"] = start
return self.get(url, params=params)
def set_branches_permissions(
self,
project_key,
multiple_permissions=False,
matcher_type=None,
matcher_value=None,
permission_type=None,
repository_slug=None,
except_users=None,
except_groups=None,
except_access_keys=None,
start=0,
limit=25,
):
"""
Create a restriction for the supplied branch or set of branches to be applied to the given repository.
Allows creating multiple restrictions at once.
To use multiple restrictions you should format payload manually -
see the bitbucket-branch-restrictions.py example.
Reference: https://docs.atlassian.com/bitbucket-server/rest/6.8.0/bitbucket-ref-restriction-rest.html
:param project_key:
:param multiple_permissions:
:param matcher_type:
:param matcher_value:
:param permission_type:
:param repository_slug:
:param except_users:
:param except_groups:
:param except_access_keys:
:param start:
:param limit:
:return:
"""
url = self._url_branches_permissions(project_key=project_key, repository_slug=repository_slug)
if except_users is None:
except_users = []
if except_groups is None:
except_groups = []
if except_access_keys is None:
except_access_keys = []
headers = self.default_headers
if multiple_permissions:
headers = self.bulk_headers
restriction = multiple_permissions
else:
restriction = {
"type": permission_type,
"matcher": {
"id": matcher_value,
"displayId": matcher_value,
"type": {
"id": matcher_type.upper(),
"name": matcher_type.capitalize(),
},
"active": True,
},
"users": except_users,
"groups": except_groups,
"accessKeys": except_access_keys,
}
params = {"start": start, "limit": limit}
return self.post(url, data=restriction, params=params, headers=headers)
def delete_branch_permission(self, project_key, permission_id, repository_slug=None):
"""
Deletes a restriction as specified by a restriction id.
The authenticated user must have REPO_ADMIN permission or higher to call this resource.
:param project_key:
:param repository_slug:
:param permission_id:
:return:
"""
url = self._url_branches_permissions(project_key, permission_id, repository_slug)
return self.delete(url)
def get_branch_permission(self, project_key, permission_id, repository_slug=None):
"""
Returns a restriction as specified by a restriction id.
The authenticated user must have REPO_ADMIN permission or higher to call this resource.
:param project_key:
:param repository_slug:
:param permission_id:
:return:
"""
url = self._url_branches_permissions(project_key, permission_id, repository_slug)
return self.get(url)
def all_branches_permissions(self, project_key, permission_id, repository_slug=None):
"""
Get branches permissions from a given repo
:param project_key:
:param permission_id
:param repository_slug:
:return:
"""
url = self._url_branches_permissions(project_key, permission_id, repository_slug)
return self._get_paged(url)
def _url_branching_model(self, project_key, repository_slug):
return "{}/branchmodel/configuration".format(
self._url_repo(
project_key,
repository_slug,
api_root="rest/branch-utils",
api_version="1.0",
)
)
def get_branching_model(self, project_key, repository_slug):
"""
Get branching model
:param project_key:
:param repository_slug:
:return:
"""
url = self._url_branching_model(project_key, repository_slug)
return self.get(url)
def set_branching_model(self, project_key, repository_slug, data):
"""
Set branching model
:param project_key:
:param repository_slug:
:param data:
:return:
"""
url = self._url_branching_model(project_key, repository_slug)
return self.put(url, data=data)
def enable_branching_model(self, project_key, repository_slug):
"""
Enable branching model by setting it with default configuration
:param project_key:
:param repository_slug:
:return:
"""
default_model_data = {
"development": {"refId": None, "useDefault": True},
"types": [
{
"displayName": "Bugfix",
"enabled": True,
"id": "BUGFIX",
"prefix": "bugfix/",
},
{
"displayName": "Feature",
"enabled": True,
"id": "FEATURE",
"prefix": "feature/",
},
{
"displayName": "Hotfix",
"enabled": True,
"id": "HOTFIX",
"prefix": "hotfix/",
},
{
"displayName": "Release",
"enabled": True,
"id": "RELEASE",
"prefix": "release/",
},
],
}
return self.set_branching_model(project_key, repository_slug, default_model_data)
def disable_branching_model(self, project_key, repository_slug):
"""
Disable branching model
:param project_key:
:param repository_slug:
:return:
"""
return self.delete(self._url_branching_model(project_key, repository_slug))
def _url_file(self, project_key, repository_slug, filename):
return "{}/browse/{}".format(self._url_repo(project_key, repository_slug), filename)
def upload_file(self, project_key, repository_slug, content, message, branch, filename):
"""
Upload new file for given branch.
:param project_key:
:param repository_slug:
:param content:
:param message:
:param branch:
:param filename
:return:
"""
url = self._url_file(project_key, repository_slug, filename)
data = {"content": content, "message": message, "branch": branch}
return self.put(url, files=data)
def update_file(
self,
project_key,
repository_slug,
content,
message,
branch,
filename,
source_commit_id,
):
"""
Update existing file for given branch.
:param project_key:
:param repository_slug:
:param content:
:param message:
:param branch:
:param filename:
:param source_commit_id:
:return:
"""
url = self._url_file(project_key, repository_slug, filename)
data = {
"content": content,
"message": message,
"branch": branch,
"sourceCommitId": source_commit_id,
}
return self.put(url, files=data)
def search_code(self, team, search_query, page=1, limit=10):
"""
Search repositories for matching code
:team: str
:search_query: str
"""
url = self.resource_url("teams/{team}/search/code".format(team=team))
return self.get(url, params={"search_query": search_query, "page": page, "pagelen": limit})
def get_lfs_repo_status(self, project_key, repo):
url = "rest/git-lfs/git-lfs/admin/projects/{projectKey}/repos/{repositorySlug}/enabled".format(
projectKey=project_key, repositorySlug=repo
)
return self.get(url)
def _url_repo_conditions(self, project_key, repo_key):
return "{}/conditions".format(
self._url_repo(
project_key,
repo_key,
api_root="rest/default-reviewers",
api_version="1.0",
)
)
def get_repo_conditions(self, project_key, repo_key):
"""
Request type: GET
Return a page of defaults conditions with reviewers list (type REPOSITORY or PROJECT)
that have been configured for this repository slug inside project specified.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264928992
:projectKey: str- project key involved
:repoKey: str - repo key involved
:return:
"""
url = self._url_repo_conditions(project_key, repo_key)
return self.get(url) or {}
def get_repo_project_conditions(self, project_key, repo_key):
"""
Request type: GET
Return a page of repository conditions (only type PROJECT) with reviewers list associated
that have been configured for this repository slug inside project specified.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264928992
:projectKey: str- project key involved
:repoKey: str - repo key involved
:return:
"""
response = self.get_repo_conditions(project_key, repo_key)
count = 0
for condition in response:
if condition["scope"]["type"] == "REPOSITORY":
del response[count]
count += 1
return response
def get_repo_repo_conditions(self, project_key, repo_key):
"""
Request type: GET
Return a page of repository conditions (only type REPOSITORY) with reviewers list associated
that have been configured for this repository slug inside project specified.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264928992
:projectKey: str- project key involved
:repoKey: str - repo key involved
:return:
"""
response = self.get_repo_conditions(project_key, repo_key)
count = 0
for condition in response:
if condition["scope"]["type"] == "PROJECT":
del response[count]
count += 1
return response
def _url_repo_condition(self, project_key, repo_key, id_condition=None):
return "{}/condition/{}".format(
self._url_repo(
project_key,
repo_key,
api_root="rest/default-reviewers",
api_version="1.0",
),
"" if id_condition is None else str(id_condition),
)
def get_repo_condition(self, project_key, repo_key, id_condition):
"""
Request type: GET
Return a specific condition with reviewers list
that have been configured for this repository slug inside project specified.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632
:projectKey: str- project key involved
:repoKey: str - repo key involved
:idCondition: int - condition id involved
:return:
"""
url = self._url_repo_condition(project_key, repo_key, id_condition)
return self.get(url) or {}
def create_repo_condition(self, project_key, repo_key, condition):
"""
Request type: POST
Create a new condition for this repository slug inside project specified.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264908128
:projectKey: str- project key involved
:repoKey: str - repo key involved
:data: condition: dictionary object
:example condition: '{"sourceMatcher":
{"id":"any",
"type":{"id":"ANY_REF"}},
"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},
"reviewers":[{"id": 12}],"requiredApprovals":"0"
}'
:return:
"""
url = self._url_repo_condition(project_key, repo_key)
return self.post(url, data=condition) or {}
def update_repo_condition(self, project_key, repo_key, condition, id_condition):
"""
Request type: PUT
Update a specific condition for this repository slug inside project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm52264927632
:projectKey: str- project key involved
:repoKey: str - repo key involved
:idCondition: int - condition id involved
:data: condition: dictionary object
:example condition: '{"sourceMatcher":
{"id":"any",
"type":{"id":"ANY_REF"}},
"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},
"reviewers":[{"id": 12}],"requiredApprovals":"0"
}'
:return:
"""
url = self._url_repo_condition(project_key, repo_key, id_condition)
return self.put(url, data=condition) or {}
def delete_repo_condition(self, project_key, repo_key, id_condition):
"""
Request type: DELETE
Delete a specific condition for this repository slug inside project.
For further information visit:
https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-default-reviewers-rest.html#idm8287339888
:projectKey: str- project key involved
:repoKey: str - repo key involved
:idCondition: int - condition id involved
:return:
"""
url = self._url_repo_condition(project_key, repo_key, id_condition)
return self.delete(url) or {}
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def get_repositories(self, workspace, role=None, query=None, sort=None):
"""
Get all repositories in a workspace.
:param workspace:
:param role: Filters the result based on the authenticated user's role on each repository.
One of: member, contributor, admin, owner
:param query: Query string to narrow down the response.
:param sort: Field by which the results should be sorted.
"""
return [
r.data
for r in Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.each(role=role, q=query, sort=sort)
]
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def get_pipelines(self, workspace, repository_slug, number=10, sort_by="-created_on"):
"""
Get information about latest pipelines runs.
:param workspace:
:param repository_slug:
:param sort_by:
:param number: number of pipelines to fetch
:param :sort_by: optional key to sort available pipelines for
:return: List of pipeline data
"""
values = []
for p in (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.pipelines.each(sort=sort_by)
):
values.append(p.data)
if len(values) == number:
break
return values
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def trigger_pipeline(self, workspace, repository_slug, branch="master", revision=None, name=None):
"""
Trigger a new pipeline. The following options are possible (1 and 2
trigger the pipeline that the branch is associated with in the Pipelines
configuration):
1. Latest revision of a branch (specify ``branch``)
2. Specific revision on a branch (additionally specify ``revision``)
3. Specific pipeline (additionally specify ``name``)
:return: the initiated pipeline; or error information
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.pipelines.trigger(branch=branch, commit=revision, pattern=name)
.data
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def get_pipeline(self, workspace, repository_slug, uuid):
"""
Get information about the pipeline specified by ``uuid``.
:param workspace:
:param repository_slug:
:param uuid: Pipeline identifier (with surrounding {}; NOT the build number)
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.pipelines.get(uuid)
.data
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def stop_pipeline(self, workspace, repository_slug, uuid):
"""
Stop the pipeline specified by ``uuid``.
:param workspace:
:param repository_slug:
:param uuid: Pipeline identifier (with surrounding {}; NOT the build number)
See the documentation for the meaning of response status codes.
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.pipelines.get(uuid)
.stop()
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def get_pipeline_steps(self, workspace, repository_slug, uuid):
"""
Get information about the steps of the pipeline specified by ``uuid``.
:param workspace:
:param repository_slug:
:param uuid: Pipeline identifier (with surrounding {}; NOT the build number)
"""
values = []
for s in (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.pipelines.get(uuid)
.steps()
):
values.append(s.data)
return values
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def get_pipeline_step(self, workspace, repository_slug, pipeline_uuid, step_uuid):
"""
Get information about a step of a pipeline, specified by respective UUIDs.
:param workspace:
:param repository_slug:
:param pipeline_uuid: Pipeline identifier (with surrounding {}; NOT the build number)
:param step_uuid: Step identifier (with surrounding {})
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.pipelines.get(pipeline_uuid)
.step(step_uuid)
.data
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def get_pipeline_step_log(self, workspace, repository_slug, pipeline_uuid, step_uuid):
"""
Get log of a step of a pipeline, specified by respective UUIDs.
:param workspace:
:param repository_slug:
:param pipeline_uuid: Pipeline identifier (with surrounding {}; NOT the build number)
:param step_uuid: Step identifier (with surrounding {})
:return: byte string log
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.pipelines.get(pipeline_uuid)
.step(step_uuid)
.log()
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def create_issue(
self,
workspace,
repository_slug,
title,
description="",
kind="bug",
priority="major",
):
"""
Create a new issue in the issue tracker of the given repository.
:param workspace:
:param repository_slug:
:param title:
:param description:
:param kind: one of: bug, enhancement, proposal, task
:param priority: one of: trivial, minor, major, critical, blocker
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.issues.create(title=title, description=description, kind=kind, priority=priority)
.data
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def get_issues(self, workspace, repository_slug, sort_by=None, query=None):
"""
Get information about the issues tracked in the given repository. By
default, the issues are sorted by ID in descending order.
:param workspace:
:param repository_slug:
:param sort_by: optional key to sort available issues for
:param query: optional query to filter available issues for. See
https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering
for an overview
:return: List of issues (direct, i.e. without the 'values' key)
"""
values = []
for p in (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.issues.each(q=query, sort=sort_by)
):
values.append(p.data)
return values
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def get_issue(self, workspace, repository_slug, id):
"""
Get the issue specified by ``id``.
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.issues.get(id)
.data
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def update_issue(self, workspace, repository_slug, id, **fields):
"""
Update the ``fields`` of the issue specified by ``id``.
Consult the official API documentation for valid fields.
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.issues.get(id)
.update(**fields)
.data
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def delete_issue(self, workspace, repository_slug, id):
"""
Delete the issue specified by ``id``.
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.issues.get(id)
.delete()
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def add_branch_restriction(
self,
workspace,
repository_slug,
kind,
branch_match_kind="glob",
branch_pattern="*",
branch_type=None,
users=None,
groups=None,
value=None,
):
"""
Add a new branch restriction.
:param workspace:
:param repository_slug:
:param value:
:param kind: One of require_tasks_to_be_completed, force, restrict_merges,
enforce_merge_checks, require_approvals_to_merge, delete,
require_all_dependencies_merged, push, require_passing_builds_to_merge,
reset_pullrequest_approvals_on_change, require_default_reviewer_approvals_to_merge
:param branch_match_kind: branching_model or glob, if branching_model use
param branch_type otherwise branch_pattern.
:param branch_pattern: A glob specifying the branch this restriction should
apply to (supports * as wildcard).
:param branch_type: The branch type specifies the branches this restriction
should apply to. One of: feature, bugfix, release, hotfix, development, production.
:param users: List of user objects that are excluded from the restriction.
Minimal: {"username": "<username>"}
:param groups: List of group objects that are excluded from the restriction.
Minimal: {"owner": {"username": "<teamname>"}, "slug": "<groupslug>"}
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.branch_restrictions.create(
kind,
branch_match_kind=branch_match_kind,
branch_pattern=branch_pattern,
branch_type=branch_type,
users=users,
groups=groups,
value=value,
)
.data
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def get_branch_restrictions(self, workspace, repository_slug, kind=None, pattern=None, number=10):
"""
Get all branch permissions.
"""
values = []
for p in (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.branch_restrictions.each(kind=kind, pattern=pattern)
):
values.append(p.data)
if len(values) == number:
break
return values
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def update_branch_restriction(self, workspace, repository_slug, id, **fields):
"""
Update an existing branch restriction identified by ``id``.
Consult the official API documentation for valid fields.
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.branch_restrictions.get(id)
.update(**fields)
.data
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def delete_branch_restriction(self, workspace, repository_slug, id):
"""
Delete an existing branch restriction identified by ``id``.
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.branch_restrictions.get(id)
.delete()
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def add_default_reviewer(self, workspace, repository_slug, user):
"""
Add user as default reviewer to the repository.
Can safely be called multiple times with the same user, only adds once.
:param workspace:
:param repository_slug:
:param user: The username or account UUID to add as default_reviewer.
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.default_reviewers.add(user)
.data
)
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def get_default_reviewers(self, workspace, repository_slug, number=10):
"""
Get all default reviewers for the repository.
"""
values = []
for p in (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.default_reviewers.each()
):
values.append(p.data)
if len(values) == number:
break
return values
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def is_default_reviewer(self, workspace, repository_slug, user):
"""
Check if the user is a default reviewer of the repository.
:param workspace:
:param repository_slug:
:param user: The username or account UUID to check.
:return: True if present, False if not.
"""
if (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.default_reviewers.get(user)
is None
):
return False
return True
@deprecated(
version="2.0.2",
reason="Use atlassian.bitbucket.cloud instead of atlassian.bitbucket",
)
def delete_default_reviewer(self, workspace, repository_slug, user):
"""
Remove user as default reviewer from the repository.
:param repository_slug:
:param workspace:
:param user: The username or account UUID to delete as default reviewer.
"""
return (
Cloud(self.url, **self._new_session_args)
.workspaces.get(workspace)
.repositories.get(repository_slug)
.default_reviewers.get(user)
.delete()
)
|