|
6 | 6 | package org
|
7 | 7 |
|
8 | 8 | import (
|
| 9 | + "strings" |
| 10 | + |
9 | 11 | "code.gitea.io/gitea/models"
|
10 | 12 | "code.gitea.io/gitea/modules/context"
|
| 13 | + "code.gitea.io/gitea/modules/log" |
11 | 14 | api "code.gitea.io/gitea/modules/structs"
|
12 | 15 | "code.gitea.io/gitea/routers/api/v1/convert"
|
13 | 16 | "code.gitea.io/gitea/routers/api/v1/user"
|
@@ -504,3 +507,83 @@ func RemoveTeamRepository(ctx *context.APIContext) {
|
504 | 507 | }
|
505 | 508 | ctx.Status(204)
|
506 | 509 | }
|
| 510 | + |
| 511 | +// SearchTeam api for searching teams |
| 512 | +func SearchTeam(ctx *context.APIContext) { |
| 513 | + // swagger:operation GET /orgs/{org}/teams/search organization teamSearch |
| 514 | + // --- |
| 515 | + // summary: Search for teams within an organization |
| 516 | + // produces: |
| 517 | + // - application/json |
| 518 | + // parameters: |
| 519 | + // - name: org |
| 520 | + // in: path |
| 521 | + // description: name of the organization |
| 522 | + // type: string |
| 523 | + // required: true |
| 524 | + // - name: q |
| 525 | + // in: query |
| 526 | + // description: keywords to search |
| 527 | + // type: string |
| 528 | + // - name: include_desc |
| 529 | + // in: query |
| 530 | + // description: include search within team description (defaults to true) |
| 531 | + // type: boolean |
| 532 | + // - name: limit |
| 533 | + // in: query |
| 534 | + // description: limit size of results |
| 535 | + // type: integer |
| 536 | + // - name: page |
| 537 | + // in: query |
| 538 | + // description: page number of results to return (1-based) |
| 539 | + // type: integer |
| 540 | + // responses: |
| 541 | + // "200": |
| 542 | + // description: "SearchResults of a successful search" |
| 543 | + // schema: |
| 544 | + // type: object |
| 545 | + // properties: |
| 546 | + // ok: |
| 547 | + // type: boolean |
| 548 | + // data: |
| 549 | + // type: array |
| 550 | + // items: |
| 551 | + // "$ref": "#/definitions/Team" |
| 552 | + opts := &models.SearchTeamOptions{ |
| 553 | + UserID: ctx.User.ID, |
| 554 | + Keyword: strings.TrimSpace(ctx.Query("q")), |
| 555 | + OrgID: ctx.Org.Organization.ID, |
| 556 | + IncludeDesc: (ctx.Query("include_desc") == "" || ctx.QueryBool("include_desc")), |
| 557 | + PageSize: ctx.QueryInt("limit"), |
| 558 | + Page: ctx.QueryInt("page"), |
| 559 | + } |
| 560 | + |
| 561 | + teams, _, err := models.SearchTeam(opts) |
| 562 | + if err != nil { |
| 563 | + log.Error("SearchTeam failed: %v", err) |
| 564 | + ctx.JSON(500, map[string]interface{}{ |
| 565 | + "ok": false, |
| 566 | + "error": "SearchTeam internal failure", |
| 567 | + }) |
| 568 | + return |
| 569 | + } |
| 570 | + |
| 571 | + apiTeams := make([]*api.Team, len(teams)) |
| 572 | + for i := range teams { |
| 573 | + if err := teams[i].GetUnits(); err != nil { |
| 574 | + log.Error("Team GetUnits failed: %v", err) |
| 575 | + ctx.JSON(500, map[string]interface{}{ |
| 576 | + "ok": false, |
| 577 | + "error": "SearchTeam failed to get units", |
| 578 | + }) |
| 579 | + return |
| 580 | + } |
| 581 | + apiTeams[i] = convert.ToTeam(teams[i]) |
| 582 | + } |
| 583 | + |
| 584 | + ctx.JSON(200, map[string]interface{}{ |
| 585 | + "ok": true, |
| 586 | + "data": apiTeams, |
| 587 | + }) |
| 588 | + |
| 589 | +} |
0 commit comments