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
|
//
// Copyright 2021, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package gitlab
import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"time"
"github.com/hashicorp/go-retryablehttp"
)
// List a couple of standard errors.
var (
ErrUserActivatePrevented = errors.New("Cannot activate a user that is blocked by admin or by LDAP synchronization")
ErrUserApprovePrevented = errors.New("Cannot approve a user that is blocked by admin or by LDAP synchronization")
ErrUserBlockPrevented = errors.New("Cannot block a user that is already blocked by LDAP synchronization")
ErrUserConflict = errors.New("User does not have a pending request")
ErrUserDeactivatePrevented = errors.New("Cannot deactivate a user that is blocked by admin or by LDAP synchronization")
ErrUserDisableTwoFactorPrevented = errors.New("Cannot disable two factor authentication if not authenticated as administrator")
ErrUserNotFound = errors.New("User does not exist")
ErrUserRejectPrevented = errors.New("Cannot reject a user if not authenticated as administrator")
ErrUserTwoFactorNotEnabled = errors.New("Cannot disable two factor authentication if not enabled")
ErrUserUnblockPrevented = errors.New("Cannot unblock a user that is blocked by LDAP synchronization")
)
// UsersService handles communication with the user related methods of
// the GitLab API.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html
type UsersService struct {
client *Client
}
// BasicUser included in other service responses (such as merge requests, pipelines, etc).
type BasicUser struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
State string `json:"state"`
Locked bool `json:"locked"`
CreatedAt *time.Time `json:"created_at"`
AvatarURL string `json:"avatar_url"`
WebURL string `json:"web_url"`
}
// ServiceAccount represents a GitLab service account.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/user_service_accounts.html
type ServiceAccount struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
}
// User represents a GitLab user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html
type User struct {
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Name string `json:"name"`
State string `json:"state"`
WebURL string `json:"web_url"`
CreatedAt *time.Time `json:"created_at"`
Bio string `json:"bio"`
Bot bool `json:"bot"`
Location string `json:"location"`
PublicEmail string `json:"public_email"`
Skype string `json:"skype"`
Linkedin string `json:"linkedin"`
Twitter string `json:"twitter"`
WebsiteURL string `json:"website_url"`
Organization string `json:"organization"`
JobTitle string `json:"job_title"`
ExternUID string `json:"extern_uid"`
Provider string `json:"provider"`
ThemeID int `json:"theme_id"`
LastActivityOn *ISOTime `json:"last_activity_on"`
ColorSchemeID int `json:"color_scheme_id"`
IsAdmin bool `json:"is_admin"`
IsAuditor bool `json:"is_auditor"`
AvatarURL string `json:"avatar_url"`
CanCreateGroup bool `json:"can_create_group"`
CanCreateProject bool `json:"can_create_project"`
ProjectsLimit int `json:"projects_limit"`
CurrentSignInAt *time.Time `json:"current_sign_in_at"`
CurrentSignInIP *net.IP `json:"current_sign_in_ip"`
LastSignInAt *time.Time `json:"last_sign_in_at"`
LastSignInIP *net.IP `json:"last_sign_in_ip"`
ConfirmedAt *time.Time `json:"confirmed_at"`
TwoFactorEnabled bool `json:"two_factor_enabled"`
Note string `json:"note"`
Identities []*UserIdentity `json:"identities"`
External bool `json:"external"`
PrivateProfile bool `json:"private_profile"`
SharedRunnersMinutesLimit int `json:"shared_runners_minutes_limit"`
ExtraSharedRunnersMinutesLimit int `json:"extra_shared_runners_minutes_limit"`
UsingLicenseSeat bool `json:"using_license_seat"`
CustomAttributes []*CustomAttribute `json:"custom_attributes"`
NamespaceID int `json:"namespace_id"`
Locked bool `json:"locked"`
CreatedBy *BasicUser `json:"created_by"`
}
// UserIdentity represents a user identity.
type UserIdentity struct {
Provider string `json:"provider"`
ExternUID string `json:"extern_uid"`
}
// UserAvatar represents a GitLab user avatar.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html
type UserAvatar struct {
Filename string
Image io.Reader
}
// MarshalJSON implements the json.Marshaler interface.
func (a *UserAvatar) MarshalJSON() ([]byte, error) {
if a.Filename == "" && a.Image == nil {
return []byte(`""`), nil
}
type alias UserAvatar
return json.Marshal((*alias)(a))
}
// ListUsersOptions represents the available ListUsers() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-users
type ListUsersOptions struct {
ListOptions
Active *bool `url:"active,omitempty" json:"active,omitempty"`
Blocked *bool `url:"blocked,omitempty" json:"blocked,omitempty"`
ExcludeInternal *bool `url:"exclude_internal,omitempty" json:"exclude_internal,omitempty"`
ExcludeExternal *bool `url:"exclude_external,omitempty" json:"exclude_external,omitempty"`
// The options below are only available for admins.
Search *string `url:"search,omitempty" json:"search,omitempty"`
Username *string `url:"username,omitempty" json:"username,omitempty"`
ExternalUID *string `url:"extern_uid,omitempty" json:"extern_uid,omitempty"`
Provider *string `url:"provider,omitempty" json:"provider,omitempty"`
CreatedBefore *time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
CreatedAfter *time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
TwoFactor *string `url:"two_factor,omitempty" json:"two_factor,omitempty"`
Admins *bool `url:"admins,omitempty" json:"admins,omitempty"`
External *bool `url:"external,omitempty" json:"external,omitempty"`
WithoutProjects *bool `url:"without_projects,omitempty" json:"without_projects,omitempty"`
WithCustomAttributes *bool `url:"with_custom_attributes,omitempty" json:"with_custom_attributes,omitempty"`
WithoutProjectBots *bool `url:"without_project_bots,omitempty" json:"without_project_bots,omitempty"`
}
// ListUsers gets a list of users.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-users
func (s *UsersService) ListUsers(opt *ListUsersOptions, options ...RequestOptionFunc) ([]*User, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "users", opt, options)
if err != nil {
return nil, nil, err
}
var usr []*User
resp, err := s.client.Do(req, &usr)
if err != nil {
return nil, resp, err
}
return usr, resp, nil
}
// GetUsersOptions represents the available GetUser() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#single-user
type GetUsersOptions struct {
WithCustomAttributes *bool `url:"with_custom_attributes,omitempty" json:"with_custom_attributes,omitempty"`
}
// GetUser gets a single user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#single-user
func (s *UsersService) GetUser(user int, opt GetUsersOptions, options ...RequestOptionFunc) (*User, *Response, error) {
u := fmt.Sprintf("users/%d", user)
req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
if err != nil {
return nil, nil, err
}
usr := new(User)
resp, err := s.client.Do(req, usr)
if err != nil {
return nil, resp, err
}
return usr, resp, nil
}
// CreateUserOptions represents the available CreateUser() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#user-creation
type CreateUserOptions struct {
Admin *bool `url:"admin,omitempty" json:"admin,omitempty"`
Avatar *UserAvatar `url:"-" json:"-"`
Bio *string `url:"bio,omitempty" json:"bio,omitempty"`
CanCreateGroup *bool `url:"can_create_group,omitempty" json:"can_create_group,omitempty"`
Email *string `url:"email,omitempty" json:"email,omitempty"`
External *bool `url:"external,omitempty" json:"external,omitempty"`
ExternUID *string `url:"extern_uid,omitempty" json:"extern_uid,omitempty"`
ForceRandomPassword *bool `url:"force_random_password,omitempty" json:"force_random_password,omitempty"`
JobTitle *string `url:"job_title,omitempty" json:"job_title,omitempty"`
Linkedin *string `url:"linkedin,omitempty" json:"linkedin,omitempty"`
Location *string `url:"location,omitempty" json:"location,omitempty"`
Name *string `url:"name,omitempty" json:"name,omitempty"`
Note *string `url:"note,omitempty" json:"note,omitempty"`
Organization *string `url:"organization,omitempty" json:"organization,omitempty"`
Password *string `url:"password,omitempty" json:"password,omitempty"`
PrivateProfile *bool `url:"private_profile,omitempty" json:"private_profile,omitempty"`
ProjectsLimit *int `url:"projects_limit,omitempty" json:"projects_limit,omitempty"`
Provider *string `url:"provider,omitempty" json:"provider,omitempty"`
ResetPassword *bool `url:"reset_password,omitempty" json:"reset_password,omitempty"`
SkipConfirmation *bool `url:"skip_confirmation,omitempty" json:"skip_confirmation,omitempty"`
Skype *string `url:"skype,omitempty" json:"skype,omitempty"`
ThemeID *int `url:"theme_id,omitempty" json:"theme_id,omitempty"`
Twitter *string `url:"twitter,omitempty" json:"twitter,omitempty"`
Username *string `url:"username,omitempty" json:"username,omitempty"`
WebsiteURL *string `url:"website_url,omitempty" json:"website_url,omitempty"`
}
// CreateUser creates a new user. Note only administrators can create new users.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#user-creation
func (s *UsersService) CreateUser(opt *CreateUserOptions, options ...RequestOptionFunc) (*User, *Response, error) {
var err error
var req *retryablehttp.Request
if opt.Avatar == nil {
req, err = s.client.NewRequest(http.MethodPost, "users", opt, options)
} else {
req, err = s.client.UploadRequest(
http.MethodPost,
"users",
opt.Avatar.Image,
opt.Avatar.Filename,
UploadAvatar,
opt,
options,
)
}
if err != nil {
return nil, nil, err
}
usr := new(User)
resp, err := s.client.Do(req, usr)
if err != nil {
return nil, resp, err
}
return usr, resp, nil
}
// ModifyUserOptions represents the available ModifyUser() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#user-modification
type ModifyUserOptions struct {
Admin *bool `url:"admin,omitempty" json:"admin,omitempty"`
Avatar *UserAvatar `url:"-" json:"avatar,omitempty"`
Bio *string `url:"bio,omitempty" json:"bio,omitempty"`
CanCreateGroup *bool `url:"can_create_group,omitempty" json:"can_create_group,omitempty"`
CommitEmail *string `url:"commit_email,omitempty" json:"commit_email,omitempty"`
Email *string `url:"email,omitempty" json:"email,omitempty"`
External *bool `url:"external,omitempty" json:"external,omitempty"`
ExternUID *string `url:"extern_uid,omitempty" json:"extern_uid,omitempty"`
JobTitle *string `url:"job_title,omitempty" json:"job_title,omitempty"`
Linkedin *string `url:"linkedin,omitempty" json:"linkedin,omitempty"`
Location *string `url:"location,omitempty" json:"location,omitempty"`
Name *string `url:"name,omitempty" json:"name,omitempty"`
Note *string `url:"note,omitempty" json:"note,omitempty"`
Organization *string `url:"organization,omitempty" json:"organization,omitempty"`
Password *string `url:"password,omitempty" json:"password,omitempty"`
PrivateProfile *bool `url:"private_profile,omitempty" json:"private_profile,omitempty"`
ProjectsLimit *int `url:"projects_limit,omitempty" json:"projects_limit,omitempty"`
Provider *string `url:"provider,omitempty" json:"provider,omitempty"`
PublicEmail *string `url:"public_email,omitempty" json:"public_email,omitempty"`
SkipReconfirmation *bool `url:"skip_reconfirmation,omitempty" json:"skip_reconfirmation,omitempty"`
Skype *string `url:"skype,omitempty" json:"skype,omitempty"`
ThemeID *int `url:"theme_id,omitempty" json:"theme_id,omitempty"`
Twitter *string `url:"twitter,omitempty" json:"twitter,omitempty"`
Username *string `url:"username,omitempty" json:"username,omitempty"`
WebsiteURL *string `url:"website_url,omitempty" json:"website_url,omitempty"`
}
// ModifyUser modifies an existing user. Only administrators can change attributes
// of a user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#user-modification
func (s *UsersService) ModifyUser(user int, opt *ModifyUserOptions, options ...RequestOptionFunc) (*User, *Response, error) {
var err error
var req *retryablehttp.Request
u := fmt.Sprintf("users/%d", user)
if opt.Avatar == nil || (opt.Avatar.Filename == "" && opt.Avatar.Image == nil) {
req, err = s.client.NewRequest(http.MethodPut, u, opt, options)
} else {
req, err = s.client.UploadRequest(
http.MethodPut,
u,
opt.Avatar.Image,
opt.Avatar.Filename,
UploadAvatar,
opt,
options,
)
}
if err != nil {
return nil, nil, err
}
usr := new(User)
resp, err := s.client.Do(req, usr)
if err != nil {
return nil, resp, err
}
return usr, resp, nil
}
// DeleteUser deletes a user. Available only for administrators. This is an
// idempotent function, calling this function for a non-existent user id still
// returns a status code 200 OK. The JSON response differs if the user was
// actually deleted or not. In the former the user is returned and in the
// latter not.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#user-deletion
func (s *UsersService) DeleteUser(user int, options ...RequestOptionFunc) (*Response, error) {
u := fmt.Sprintf("users/%d", user)
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// CurrentUser gets currently authenticated user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-current-user
func (s *UsersService) CurrentUser(options ...RequestOptionFunc) (*User, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "user", nil, options)
if err != nil {
return nil, nil, err
}
usr := new(User)
resp, err := s.client.Do(req, usr)
if err != nil {
return nil, resp, err
}
return usr, resp, nil
}
// UserStatus represents the current status of a user
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#user-status
type UserStatus struct {
Emoji string `json:"emoji"`
Availability AvailabilityValue `json:"availability"`
Message string `json:"message"`
MessageHTML string `json:"message_html"`
}
// CurrentUserStatus retrieves the user status
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#user-status
func (s *UsersService) CurrentUserStatus(options ...RequestOptionFunc) (*UserStatus, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "user/status", nil, options)
if err != nil {
return nil, nil, err
}
status := new(UserStatus)
resp, err := s.client.Do(req, status)
if err != nil {
return nil, resp, err
}
return status, resp, nil
}
// GetUserStatus retrieves a user's status
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#get-the-status-of-a-user
func (s *UsersService) GetUserStatus(user int, options ...RequestOptionFunc) (*UserStatus, *Response, error) {
u := fmt.Sprintf("users/%d/status", user)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
status := new(UserStatus)
resp, err := s.client.Do(req, status)
if err != nil {
return nil, resp, err
}
return status, resp, nil
}
// UserStatusOptions represents the options required to set the status
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#set-user-status
type UserStatusOptions struct {
Emoji *string `url:"emoji,omitempty" json:"emoji,omitempty"`
Availability *AvailabilityValue `url:"availability,omitempty" json:"availability,omitempty"`
Message *string `url:"message,omitempty" json:"message,omitempty"`
}
// SetUserStatus sets the user's status
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#set-user-status
func (s *UsersService) SetUserStatus(opt *UserStatusOptions, options ...RequestOptionFunc) (*UserStatus, *Response, error) {
req, err := s.client.NewRequest(http.MethodPut, "user/status", opt, options)
if err != nil {
return nil, nil, err
}
status := new(UserStatus)
resp, err := s.client.Do(req, status)
if err != nil {
return nil, resp, err
}
return status, resp, nil
}
// UserAssociationsCount represents the user associations count.
//
// Gitlab API docs: https://docs.gitlab.com/ee/api/users.html#list-associations-count-for-user
type UserAssociationsCount struct {
GroupsCount int `json:"groups_count"`
ProjectsCount int `json:"projects_count"`
IssuesCount int `json:"issues_count"`
MergeRequestsCount int `json:"merge_requests_count"`
}
// GetUserAssociationsCount gets a list of a specified user associations.
//
// Gitlab API docs: https://docs.gitlab.com/ee/api/users.html#list-associations-count-for-user
func (s *UsersService) GetUserAssociationsCount(user int, options ...RequestOptionFunc) (*UserAssociationsCount, *Response, error) {
u := fmt.Sprintf("users/%d/associations_count", user)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
uac := new(UserAssociationsCount)
resp, err := s.client.Do(req, uac)
if err != nil {
return nil, resp, err
}
return uac, resp, nil
}
// SSHKey represents a SSH key.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/user_keys.html#list-all-ssh-keys
type SSHKey struct {
ID int `json:"id"`
Title string `json:"title"`
Key string `json:"key"`
CreatedAt *time.Time `json:"created_at"`
ExpiresAt *time.Time `json:"expires_at"`
UsageType string `json:"usage_type"`
}
// ListSSHKeysOptions represents the available ListSSHKeys options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/user_keys.html#list-all-ssh-keys
type ListSSHKeysOptions ListOptions
// ListSSHKeys gets a list of currently authenticated user's SSH keys.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/user_keys.html#list-all-ssh-keys
func (s *UsersService) ListSSHKeys(opt *ListSSHKeysOptions, options ...RequestOptionFunc) ([]*SSHKey, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "user/keys", opt, options)
if err != nil {
return nil, nil, err
}
var k []*SSHKey
resp, err := s.client.Do(req, &k)
if err != nil {
return nil, resp, err
}
return k, resp, nil
}
// ListSSHKeysForUserOptions represents the available ListSSHKeysForUser() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#list-ssh-keys-for-user
type ListSSHKeysForUserOptions ListOptions
// ListSSHKeysForUser gets a list of a specified user's SSH keys.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#list-ssh-keys-for-user
func (s *UsersService) ListSSHKeysForUser(uid interface{}, opt *ListSSHKeysForUserOptions, options ...RequestOptionFunc) ([]*SSHKey, *Response, error) {
user, err := parseID(uid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("users/%s/keys", user)
req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
if err != nil {
return nil, nil, err
}
var k []*SSHKey
resp, err := s.client.Do(req, &k)
if err != nil {
return nil, resp, err
}
return k, resp, nil
}
// GetSSHKey gets a single key.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#single-ssh-key
func (s *UsersService) GetSSHKey(key int, options ...RequestOptionFunc) (*SSHKey, *Response, error) {
u := fmt.Sprintf("user/keys/%d", key)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
k := new(SSHKey)
resp, err := s.client.Do(req, k)
if err != nil {
return nil, resp, err
}
return k, resp, nil
}
// GetSSHKeyForUser gets a single key for a given user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#single-ssh-key-for-given-user
func (s *UsersService) GetSSHKeyForUser(user int, key int, options ...RequestOptionFunc) (*SSHKey, *Response, error) {
u := fmt.Sprintf("users/%d/keys/%d", user, key)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
k := new(SSHKey)
resp, err := s.client.Do(req, k)
if err != nil {
return nil, resp, err
}
return k, resp, nil
}
// AddSSHKeyOptions represents the available AddSSHKey() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-ssh-key
type AddSSHKeyOptions struct {
Title *string `url:"title,omitempty" json:"title,omitempty"`
Key *string `url:"key,omitempty" json:"key,omitempty"`
ExpiresAt *ISOTime `url:"expires_at,omitempty" json:"expires_at,omitempty"`
}
// AddSSHKey creates a new key owned by the currently authenticated user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-ssh-key
func (s *UsersService) AddSSHKey(opt *AddSSHKeyOptions, options ...RequestOptionFunc) (*SSHKey, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "user/keys", opt, options)
if err != nil {
return nil, nil, err
}
k := new(SSHKey)
resp, err := s.client.Do(req, k)
if err != nil {
return nil, resp, err
}
return k, resp, nil
}
// AddSSHKeyForUser creates new key owned by specified user. Available only for
// admin.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-ssh-key-for-user
func (s *UsersService) AddSSHKeyForUser(user int, opt *AddSSHKeyOptions, options ...RequestOptionFunc) (*SSHKey, *Response, error) {
u := fmt.Sprintf("users/%d/keys", user)
req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
if err != nil {
return nil, nil, err
}
k := new(SSHKey)
resp, err := s.client.Do(req, k)
if err != nil {
return nil, resp, err
}
return k, resp, nil
}
// DeleteSSHKey deletes key owned by currently authenticated user. This is an
// idempotent function and calling it on a key that is already deleted or not
// available results in 200 OK.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#delete-ssh-key-for-current-user
func (s *UsersService) DeleteSSHKey(key int, options ...RequestOptionFunc) (*Response, error) {
u := fmt.Sprintf("user/keys/%d", key)
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// DeleteSSHKeyForUser deletes key owned by a specified user. Available only
// for admin.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#delete-ssh-key-for-given-user
func (s *UsersService) DeleteSSHKeyForUser(user, key int, options ...RequestOptionFunc) (*Response, error) {
u := fmt.Sprintf("users/%d/keys/%d", user, key)
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// GPGKey represents a GPG key.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-all-gpg-keys
type GPGKey struct {
ID int `json:"id"`
Key string `json:"key"`
CreatedAt *time.Time `json:"created_at"`
}
// ListGPGKeys gets a list of currently authenticated user’s GPG keys.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-all-gpg-keys
func (s *UsersService) ListGPGKeys(options ...RequestOptionFunc) ([]*GPGKey, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "user/gpg_keys", nil, options)
if err != nil {
return nil, nil, err
}
var ks []*GPGKey
resp, err := s.client.Do(req, &ks)
if err != nil {
return nil, resp, err
}
return ks, resp, nil
}
// GetGPGKey gets a specific GPG key of currently authenticated user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#get-a-specific-gpg-key
func (s *UsersService) GetGPGKey(key int, options ...RequestOptionFunc) (*GPGKey, *Response, error) {
u := fmt.Sprintf("user/gpg_keys/%d", key)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
k := new(GPGKey)
resp, err := s.client.Do(req, k)
if err != nil {
return nil, resp, err
}
return k, resp, nil
}
// AddGPGKeyOptions represents the available AddGPGKey() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-a-gpg-key
type AddGPGKeyOptions struct {
Key *string `url:"key,omitempty" json:"key,omitempty"`
}
// AddGPGKey creates a new GPG key owned by the currently authenticated user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-a-gpg-key
func (s *UsersService) AddGPGKey(opt *AddGPGKeyOptions, options ...RequestOptionFunc) (*GPGKey, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "user/gpg_keys", opt, options)
if err != nil {
return nil, nil, err
}
k := new(GPGKey)
resp, err := s.client.Do(req, k)
if err != nil {
return nil, resp, err
}
return k, resp, nil
}
// DeleteGPGKey deletes a GPG key owned by currently authenticated user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#delete-a-gpg-key
func (s *UsersService) DeleteGPGKey(key int, options ...RequestOptionFunc) (*Response, error) {
u := fmt.Sprintf("user/gpg_keys/%d", key)
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// ListGPGKeysForUser gets a list of a specified user’s GPG keys.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#list-all-gpg-keys-for-given-user
func (s *UsersService) ListGPGKeysForUser(user int, options ...RequestOptionFunc) ([]*GPGKey, *Response, error) {
u := fmt.Sprintf("users/%d/gpg_keys", user)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
var ks []*GPGKey
resp, err := s.client.Do(req, &ks)
if err != nil {
return nil, resp, err
}
return ks, resp, nil
}
// GetGPGKeyForUser gets a specific GPG key for a given user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#get-a-specific-gpg-key-for-a-given-user
func (s *UsersService) GetGPGKeyForUser(user, key int, options ...RequestOptionFunc) (*GPGKey, *Response, error) {
u := fmt.Sprintf("users/%d/gpg_keys/%d", user, key)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
k := new(GPGKey)
resp, err := s.client.Do(req, k)
if err != nil {
return nil, resp, err
}
return k, resp, nil
}
// AddGPGKeyForUser creates new GPG key owned by the specified user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#add-a-gpg-key-for-a-given-user
func (s *UsersService) AddGPGKeyForUser(user int, opt *AddGPGKeyOptions, options ...RequestOptionFunc) (*GPGKey, *Response, error) {
u := fmt.Sprintf("users/%d/gpg_keys", user)
req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
if err != nil {
return nil, nil, err
}
k := new(GPGKey)
resp, err := s.client.Do(req, k)
if err != nil {
return nil, resp, err
}
return k, resp, nil
}
// DeleteGPGKeyForUser deletes a GPG key owned by a specified user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#delete-a-gpg-key-for-a-given-user
func (s *UsersService) DeleteGPGKeyForUser(user, key int, options ...RequestOptionFunc) (*Response, error) {
u := fmt.Sprintf("users/%d/gpg_keys/%d", user, key)
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// Email represents an Email.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-emails
type Email struct {
ID int `json:"id"`
Email string `json:"email"`
ConfirmedAt *time.Time `json:"confirmed_at"`
}
// ListEmails gets a list of currently authenticated user's Emails.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-emails
func (s *UsersService) ListEmails(options ...RequestOptionFunc) ([]*Email, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "user/emails", nil, options)
if err != nil {
return nil, nil, err
}
var e []*Email
resp, err := s.client.Do(req, &e)
if err != nil {
return nil, resp, err
}
return e, resp, nil
}
// ListEmailsForUserOptions represents the available ListEmailsForUser() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#list-emails-for-user
type ListEmailsForUserOptions ListOptions
// ListEmailsForUser gets a list of a specified user's Emails. Available
// only for admin
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#list-emails-for-user
func (s *UsersService) ListEmailsForUser(user int, opt *ListEmailsForUserOptions, options ...RequestOptionFunc) ([]*Email, *Response, error) {
u := fmt.Sprintf("users/%d/emails", user)
req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
if err != nil {
return nil, nil, err
}
var e []*Email
resp, err := s.client.Do(req, &e)
if err != nil {
return nil, resp, err
}
return e, resp, nil
}
// GetEmail gets a single email.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#single-email
func (s *UsersService) GetEmail(email int, options ...RequestOptionFunc) (*Email, *Response, error) {
u := fmt.Sprintf("user/emails/%d", email)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
e := new(Email)
resp, err := s.client.Do(req, e)
if err != nil {
return nil, resp, err
}
return e, resp, nil
}
// AddEmailOptions represents the available AddEmail() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-email
type AddEmailOptions struct {
Email *string `url:"email,omitempty" json:"email,omitempty"`
SkipConfirmation *bool `url:"skip_confirmation,omitempty" json:"skip_confirmation,omitempty"`
}
// AddEmail creates a new email owned by the currently authenticated user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-email
func (s *UsersService) AddEmail(opt *AddEmailOptions, options ...RequestOptionFunc) (*Email, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "user/emails", opt, options)
if err != nil {
return nil, nil, err
}
e := new(Email)
resp, err := s.client.Do(req, e)
if err != nil {
return nil, resp, err
}
return e, resp, nil
}
// AddEmailForUser creates new email owned by specified user. Available only for
// admin.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-email-for-user
func (s *UsersService) AddEmailForUser(user int, opt *AddEmailOptions, options ...RequestOptionFunc) (*Email, *Response, error) {
u := fmt.Sprintf("users/%d/emails", user)
req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
if err != nil {
return nil, nil, err
}
e := new(Email)
resp, err := s.client.Do(req, e)
if err != nil {
return nil, resp, err
}
return e, resp, nil
}
// DeleteEmail deletes email owned by currently authenticated user. This is an
// idempotent function and calling it on a key that is already deleted or not
// available results in 200 OK.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#delete-email-for-current-user
func (s *UsersService) DeleteEmail(email int, options ...RequestOptionFunc) (*Response, error) {
u := fmt.Sprintf("user/emails/%d", email)
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// DeleteEmailForUser deletes email owned by a specified user. Available only
// for admin.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#delete-email-for-given-user
func (s *UsersService) DeleteEmailForUser(user, email int, options ...RequestOptionFunc) (*Response, error) {
u := fmt.Sprintf("users/%d/emails/%d", user, email)
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// BlockUser blocks the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#block-user
func (s *UsersService) BlockUser(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/block", user)
req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return err
}
resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}
switch resp.StatusCode {
case 201:
return nil
case 403:
return ErrUserBlockPrevented
case 404:
return ErrUserNotFound
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}
// UnblockUser unblocks the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#unblock-user
func (s *UsersService) UnblockUser(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/unblock", user)
req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return err
}
resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}
switch resp.StatusCode {
case 201:
return nil
case 403:
return ErrUserUnblockPrevented
case 404:
return ErrUserNotFound
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}
// BanUser bans the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#ban-user
func (s *UsersService) BanUser(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/ban", user)
req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return err
}
resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}
switch resp.StatusCode {
case 201:
return nil
case 404:
return ErrUserNotFound
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}
// UnbanUser unbans the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#unban-user
func (s *UsersService) UnbanUser(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/unban", user)
req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return err
}
resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}
switch resp.StatusCode {
case 201:
return nil
case 404:
return ErrUserNotFound
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}
// DeactivateUser deactivate the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#deactivate-user
func (s *UsersService) DeactivateUser(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/deactivate", user)
req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return err
}
resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}
switch resp.StatusCode {
case 201:
return nil
case 403:
return ErrUserDeactivatePrevented
case 404:
return ErrUserNotFound
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}
// ActivateUser activate the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#activate-user
func (s *UsersService) ActivateUser(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/activate", user)
req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return err
}
resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}
switch resp.StatusCode {
case 201:
return nil
case 403:
return ErrUserActivatePrevented
case 404:
return ErrUserNotFound
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}
// ApproveUser approve the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#approve-user
func (s *UsersService) ApproveUser(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/approve", user)
req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return err
}
resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}
switch resp.StatusCode {
case 201:
return nil
case 403:
return ErrUserApprovePrevented
case 404:
return ErrUserNotFound
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}
// RejectUser reject the specified user. Available only for admin.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#reject-user
func (s *UsersService) RejectUser(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/reject", user)
req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return err
}
resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}
switch resp.StatusCode {
case 200:
return nil
case 403:
return ErrUserRejectPrevented
case 404:
return ErrUserNotFound
case 409:
return ErrUserConflict
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}
// ImpersonationToken represents an impersonation token.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#get-all-impersonation-tokens-of-a-user
type ImpersonationToken struct {
ID int `json:"id"`
Name string `json:"name"`
Active bool `json:"active"`
Token string `json:"token"`
Scopes []string `json:"scopes"`
Revoked bool `json:"revoked"`
CreatedAt *time.Time `json:"created_at"`
ExpiresAt *ISOTime `json:"expires_at"`
LastUsedAt *time.Time `json:"last_used_at"`
}
// GetAllImpersonationTokensOptions represents the available
// GetAllImpersonationTokens() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#get-all-impersonation-tokens-of-a-user
type GetAllImpersonationTokensOptions struct {
ListOptions
State *string `url:"state,omitempty" json:"state,omitempty"`
}
// GetAllImpersonationTokens retrieves all impersonation tokens of a user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#get-all-impersonation-tokens-of-a-user
func (s *UsersService) GetAllImpersonationTokens(user int, opt *GetAllImpersonationTokensOptions, options ...RequestOptionFunc) ([]*ImpersonationToken, *Response, error) {
u := fmt.Sprintf("users/%d/impersonation_tokens", user)
req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
if err != nil {
return nil, nil, err
}
var ts []*ImpersonationToken
resp, err := s.client.Do(req, &ts)
if err != nil {
return nil, resp, err
}
return ts, resp, nil
}
// GetImpersonationToken retrieves an impersonation token of a user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#get-an-impersonation-token-of-a-user
func (s *UsersService) GetImpersonationToken(user, token int, options ...RequestOptionFunc) (*ImpersonationToken, *Response, error) {
u := fmt.Sprintf("users/%d/impersonation_tokens/%d", user, token)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
t := new(ImpersonationToken)
resp, err := s.client.Do(req, &t)
if err != nil {
return nil, resp, err
}
return t, resp, nil
}
// CreateImpersonationTokenOptions represents the available
// CreateImpersonationToken() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-an-impersonation-token
type CreateImpersonationTokenOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
Scopes *[]string `url:"scopes,omitempty" json:"scopes,omitempty"`
ExpiresAt *time.Time `url:"expires_at,omitempty" json:"expires_at,omitempty"`
}
// CreateImpersonationToken creates an impersonation token.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-an-impersonation-token
func (s *UsersService) CreateImpersonationToken(user int, opt *CreateImpersonationTokenOptions, options ...RequestOptionFunc) (*ImpersonationToken, *Response, error) {
u := fmt.Sprintf("users/%d/impersonation_tokens", user)
req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
if err != nil {
return nil, nil, err
}
t := new(ImpersonationToken)
resp, err := s.client.Do(req, &t)
if err != nil {
return nil, resp, err
}
return t, resp, nil
}
// RevokeImpersonationToken revokes an impersonation token.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#revoke-an-impersonation-token
func (s *UsersService) RevokeImpersonationToken(user, token int, options ...RequestOptionFunc) (*Response, error) {
u := fmt.Sprintf("users/%d/impersonation_tokens/%d", user, token)
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// CreatePersonalAccessTokenOptions represents the available
// CreatePersonalAccessToken() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-a-personal-access-token
type CreatePersonalAccessTokenOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
ExpiresAt *ISOTime `url:"expires_at,omitempty" json:"expires_at,omitempty"`
Scopes *[]string `url:"scopes,omitempty" json:"scopes,omitempty"`
}
// CreatePersonalAccessToken creates a personal access token.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-a-personal-access-token
func (s *UsersService) CreatePersonalAccessToken(user int, opt *CreatePersonalAccessTokenOptions, options ...RequestOptionFunc) (*PersonalAccessToken, *Response, error) {
u := fmt.Sprintf("users/%d/personal_access_tokens", user)
req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
if err != nil {
return nil, nil, err
}
t := new(PersonalAccessToken)
resp, err := s.client.Do(req, &t)
if err != nil {
return nil, resp, err
}
return t, resp, nil
}
// CreatePersonalAccessTokenForCurrentUserOptions represents the available
// CreatePersonalAccessTokenForCurrentUser() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-a-personal-access-token-with-limited-scopes-for-the-currently-authenticated-user
type CreatePersonalAccessTokenForCurrentUserOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
Scopes *[]string `url:"scopes,omitempty" json:"scopes,omitempty"`
ExpiresAt *ISOTime `url:"expires_at,omitempty" json:"expires_at,omitempty"`
}
// CreatePersonalAccessTokenForCurrentUser creates a personal access token with limited scopes for the currently authenticated user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-a-personal-access-token-with-limited-scopes-for-the-currently-authenticated-user
func (s *UsersService) CreatePersonalAccessTokenForCurrentUser(opt *CreatePersonalAccessTokenForCurrentUserOptions, options ...RequestOptionFunc) (*PersonalAccessToken, *Response, error) {
u := "user/personal_access_tokens"
req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
if err != nil {
return nil, nil, err
}
t := new(PersonalAccessToken)
resp, err := s.client.Do(req, &t)
if err != nil {
return nil, resp, err
}
return t, resp, nil
}
// UserActivity represents an entry in the user/activities response
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#get-user-activities
type UserActivity struct {
Username string `json:"username"`
LastActivityOn *ISOTime `json:"last_activity_on"`
}
// GetUserActivitiesOptions represents the options for GetUserActivities
//
// GitLap API docs:
// https://docs.gitlab.com/ee/api/users.html#get-user-activities
type GetUserActivitiesOptions struct {
ListOptions
From *ISOTime `url:"from,omitempty" json:"from,omitempty"`
}
// GetUserActivities retrieves user activities (admin only)
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#get-user-activities
func (s *UsersService) GetUserActivities(opt *GetUserActivitiesOptions, options ...RequestOptionFunc) ([]*UserActivity, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "user/activities", opt, options)
if err != nil {
return nil, nil, err
}
var t []*UserActivity
resp, err := s.client.Do(req, &t)
if err != nil {
return nil, resp, err
}
return t, resp, nil
}
// UserMembership represents a membership of the user in a namespace or project.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#user-memberships
type UserMembership struct {
SourceID int `json:"source_id"`
SourceName string `json:"source_name"`
SourceType string `json:"source_type"`
AccessLevel AccessLevelValue `json:"access_level"`
}
// GetUserMembershipOptions represents the options available to query user memberships.
//
// GitLab API docs:
// ohttps://docs.gitlab.com/ee/api/users.html#user-memberships
type GetUserMembershipOptions struct {
ListOptions
Type *string `url:"type,omitempty" json:"type,omitempty"`
}
// GetUserMemberships retrieves a list of the user's memberships.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#user-memberships
func (s *UsersService) GetUserMemberships(user int, opt *GetUserMembershipOptions, options ...RequestOptionFunc) ([]*UserMembership, *Response, error) {
u := fmt.Sprintf("users/%d/memberships", user)
req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
if err != nil {
return nil, nil, err
}
var m []*UserMembership
resp, err := s.client.Do(req, &m)
if err != nil {
return nil, resp, err
}
return m, resp, nil
}
// DisableTwoFactor disables two factor authentication for the specified user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#disable-two-factor-authentication
func (s *UsersService) DisableTwoFactor(user int, options ...RequestOptionFunc) error {
u := fmt.Sprintf("users/%d/disable_two_factor", user)
req, err := s.client.NewRequest(http.MethodPatch, u, nil, options)
if err != nil {
return err
}
resp, err := s.client.Do(req, nil)
if err != nil && resp == nil {
return err
}
switch resp.StatusCode {
case 204:
return nil
case 400:
return ErrUserTwoFactorNotEnabled
case 403:
return ErrUserDisableTwoFactorPrevented
case 404:
return ErrUserNotFound
default:
return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
}
}
// UserRunner represents a GitLab runner linked to the current user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
type UserRunner struct {
ID int `json:"id"`
Token string `json:"token"`
TokenExpiresAt *time.Time `json:"token_expires_at"`
}
// CreateUserRunnerOptions represents the available CreateUserRunner() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
type CreateUserRunnerOptions struct {
RunnerType *string `url:"runner_type,omitempty" json:"runner_type,omitempty"`
GroupID *int `url:"group_id,omitempty" json:"group_id,omitempty"`
ProjectID *int `url:"project_id,omitempty" json:"project_id,omitempty"`
Description *string `url:"description,omitempty" json:"description,omitempty"`
Paused *bool `url:"paused,omitempty" json:"paused,omitempty"`
Locked *bool `url:"locked,omitempty" json:"locked,omitempty"`
RunUntagged *bool `url:"run_untagged,omitempty" json:"run_untagged,omitempty"`
TagList *[]string `url:"tag_list,omitempty" json:"tag_list,omitempty"`
AccessLevel *string `url:"access_level,omitempty" json:"access_level,omitempty"`
MaximumTimeout *int `url:"maximum_timeout,omitempty" json:"maximum_timeout,omitempty"`
MaintenanceNote *string `url:"maintenance_note,omitempty" json:"maintenance_note,omitempty"`
}
// CreateUserRunner creates a runner linked to the current user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-a-runner
func (s *UsersService) CreateUserRunner(opts *CreateUserRunnerOptions, options ...RequestOptionFunc) (*UserRunner, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "user/runners", opts, options)
if err != nil {
return nil, nil, err
}
r := new(UserRunner)
resp, err := s.client.Do(req, r)
if err != nil {
return nil, resp, err
}
return r, resp, nil
}
// CreateServiceAccountUserOptions represents the available CreateServiceAccountUser() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/user_service_accounts.html#create-a-service-account-user
type CreateServiceAccountUserOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
Username *string `url:"username,omitempty" json:"username,omitempty"`
}
// CreateServiceAccountUser creates a new service account user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-service-account-user
func (s *UsersService) CreateServiceAccountUser(opts *CreateServiceAccountUserOptions, options ...RequestOptionFunc) (*User, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "service_accounts", opts, options)
if err != nil {
return nil, nil, err
}
usr := new(User)
resp, err := s.client.Do(req, usr)
if err != nil {
return nil, resp, err
}
return usr, resp, nil
}
// ListServiceAccounts lists all service accounts.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-service-account-user
func (s *UsersService) ListServiceAccounts(opt *ListServiceAccountsOptions, options ...RequestOptionFunc) ([]*ServiceAccount, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "service_accounts", opt, options)
if err != nil {
return nil, nil, err
}
var sas []*ServiceAccount
resp, err := s.client.Do(req, &sas)
if err != nil {
return nil, resp, err
}
return sas, resp, nil
}
// UploadAvatar uploads an avatar to the current user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#upload-a-current-user-avatar
func (s *UsersService) UploadAvatar(avatar io.Reader, filename string, options ...RequestOptionFunc) (*User, *Response, error) {
u := "user/avatar"
req, err := s.client.UploadRequest(
http.MethodPut,
u,
avatar,
filename,
UploadAvatar,
nil,
options,
)
if err != nil {
return nil, nil, err
}
usr := new(User)
resp, err := s.client.Do(req, usr)
if err != nil {
return nil, resp, err
}
return usr, resp, nil
}
|