Skip to content

Commit

Permalink
Merge pull request #357 from rollbar/pawel/added_list_projects_team_api
Browse files Browse the repository at this point in the history
added API for listing project's teams
  • Loading branch information
pawelsz-rb authored Mar 24, 2023
2 parents ca981da + e19795b commit a4a5f5c
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 172 deletions.
200 changes: 161 additions & 39 deletions client/fixtures/vcr/update_project_teams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -315,54 +315,20 @@ interactions:
headers:
User-Agent:
- go-resty/2.3.0 (https://github.com/go-resty/resty)
url: https://api.rollbar.com/api/1/teams
url: https://api.rollbar.com/api/1/project/423511/teams?exclude_builtin_teams=true
method: GET
response:
body: |-
{
"err": 0,
"result": [
{
"id": 689493,
"account_id": 317418,
"name": "bar",
"access_level": "standard"
},
{
"id": 662037,
"account_id": 317418,
"name": "Everyone",
"access_level": "everyone"
},
{
"id": 689492,
"account_id": 317418,
"name": "foo",
"access_level": "standard"
"team_id": 693607,
"project_id": 423511
},
{
"id": 662036,
"account_id": 317418,
"name": "Owners",
"access_level": "owner"
},
{
"id": 693606,
"account_id": 317418,
"name": "tf-acc-test-updateprojectteams-0",
"access_level": "standard"
},
{
"id": 693607,
"account_id": 317418,
"name": "tf-acc-test-updateprojectteams-1",
"access_level": "standard"
},
{
"id": 693608,
"account_id": 317418,
"name": "tf-acc-test-updateprojectteams-2",
"access_level": "standard"
"team_id": 693608,
"project_id": 423511
}
]
}
Expand Down Expand Up @@ -398,6 +364,162 @@ interactions:
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- go-resty/2.3.0 (https://github.com/go-resty/resty)
url: https://api.rollbar.com/api/1/project/423511/teams?exclude_builtin_teams=true
method: GET
response:
body: |-
{
"err": 0,
"result": [
{
"team_id": 693607,
"project_id": 423511
},
{
"team_id": 693608,
"project_id": 423511
}
]
}
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Origin:
- '*'
Alt-Svc:
- clear
Content-Length:
- "929"
Content-Type:
- application/json; charset=utf-8
Date:
- Wed, 11 Nov 2020 09:14:52 GMT
Etag:
- '"-271558731"'
Server:
- nginx/1.17.9
Via:
- 1.1 google
X-Rate-Limit-Limit:
- "5000"
X-Rate-Limit-Remaining:
- "4993"
X-Rate-Limit-Remaining-Seconds:
- "55"
X-Rate-Limit-Reset:
- "1605086147"
X-Response-Time:
- 10ms
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- go-resty/2.3.0 (https://github.com/go-resty/resty)
url: https://api.rollbar.com/api/1/project/0/teams?exclude_builtin_teams=true
method: GET
response:
body: |-
{
"err": 1,
"message": "Invalid or missing project id"
}
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Origin:
- '*'
Alt-Svc:
- clear
Content-Length:
- "929"
Content-Type:
- application/json; charset=utf-8
Date:
- Wed, 11 Nov 2020 09:14:52 GMT
Etag:
- '"-271558731"'
Server:
- nginx/1.17.9
Via:
- 1.1 google
X-Rate-Limit-Limit:
- "5000"
X-Rate-Limit-Remaining:
- "4993"
X-Rate-Limit-Remaining-Seconds:
- "55"
X-Rate-Limit-Reset:
- "1605086147"
X-Response-Time:
- 10ms
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- go-resty/2.3.0 (https://github.com/go-resty/resty)
url: https://api.rollbar.com/api/1/project/423511/teams?exclude_builtin_teams=true
method: GET
response:
body: |-
{
"err": 0,
"result": [
{
"team_id": 693607,
"project_id": 423511
},
{
"team_id": 693608,
"project_id": 423511
}
]
}
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Origin:
- '*'
Alt-Svc:
- clear
Content-Length:
- "929"
Content-Type:
- application/json; charset=utf-8
Date:
- Wed, 11 Nov 2020 09:14:52 GMT
Etag:
- '"-271558731"'
Server:
- nginx/1.17.9
Via:
- 1.1 google
X-Rate-Limit-Limit:
- "5000"
X-Rate-Limit-Remaining:
- "4993"
X-Rate-Limit-Remaining-Seconds:
- "55"
X-Rate-Limit-Reset:
- "1605086147"
X-Response-Time:
- 10ms
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
Expand Down
4 changes: 2 additions & 2 deletions client/paths.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Rollbar, Inc.
* Copyright (c) 2023 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -30,13 +30,13 @@ const (
pathProjectRead = "/api/1/project/{projectID}"
pathProjectToken = "/api/1/project/{projectID}/access_token/{accessToken}"
pathProjectTokens = "/api/1/project/{projectID}/access_tokens"
pathProjectTeams = "/api/1/project/{projectID}/teams"
pathTeamCreate = "/api/1/teams"
pathTeamRead = "/api/1/team/{teamID}"
pathTeamList = "/api/1/teams"
pathTeamDelete = "/api/1/team/{teamID}"
pathTeamUser = "/api/1/team/{teamID}/user/{userID}"
pathTeamProject = "/api/1/team/{teamID}/project/{projectID}"
pathTeamProjects = "/api/1/team/{teamID}/projects"
pathUser = "/api/1/user/{userID}"
pathUserTeams = "/api/1/user/{userID}/teams"
pathUsers = "/api/1/users"
Expand Down
35 changes: 22 additions & 13 deletions client/project.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Rollbar, Inc.
* Copyright (c) 2023 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,8 +23,9 @@
package client

import (
"github.com/rs/zerolog/log"
"strconv"

"github.com/rs/zerolog/log"
)

// Project represents a Rollbar project.
Expand Down Expand Up @@ -212,22 +213,30 @@ func (c *RollbarAPIClient) FindProjectTeamIDs(projectID int) ([]int, error) {
l.Debug().Msg("Finding teams assigned to project")
var projectTeamIDs []int

allTeams, err := c.ListCustomTeams()
u := c.BaseURL + pathProjectTeams
resp, err := c.Resty.R().
SetResult(teamProjectListResponse{}).
SetError(ErrorResult{}).
SetQueryParams(map[string]string{
"exclude_builtin_teams": "true"}).
SetPathParams(map[string]string{
"projectID": strconv.Itoa(projectID),
}).
Get(u)
if err != nil {
l.Err(err).Send()
return nil, err
}
err = errorFromResponse(resp)
if err != nil {
l.Err(err).Send()
return nil, err
}
allTeams := resp.Result().(*teamProjectListResponse).Result

for _, t := range allTeams {
teamID := t.ID
projectIDs, err := c.ListTeamProjectIDs(teamID)
if err != nil && err != ErrNotFound {
l.Err(err).Send()
return nil, err
}
for _, id := range projectIDs {
if id == projectID {
projectTeamIDs = append(projectTeamIDs, teamID)
}
if t.ProjectID == projectID {
projectTeamIDs = append(projectTeamIDs, t.TeamID)
}
}
count := len(projectTeamIDs)
Expand Down
61 changes: 6 additions & 55 deletions client/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ package client

import (
"encoding/json"
"github.com/brianvoe/gofakeit/v5"
"github.com/dnaeon/go-vcr/cassette"
"github.com/dnaeon/go-vcr/recorder"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"strconv"
"strings"
"testing"

"github.com/brianvoe/gofakeit/v5"
"github.com/dnaeon/go-vcr/cassette"
"github.com/dnaeon/go-vcr/recorder"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)

// TestListProjects tests listing Rollbar projects.
Expand Down Expand Up @@ -148,56 +149,6 @@ func (s *Suite) TestDeleteProject() {
})
}

// TestFindProjectTeamIDs tests finding the team IDs for a Rollbar project.
func (s *Suite) TestFindProjectTeamIDs() {
var u string // URL
var r httpmock.Responder
projectID := 423092
teamID := 689492
expected := []int{teamID}
// https://api.rollbar.com/api/1/team/689493/projects?page=1
// Mock list team projects
u = s.client.BaseURL + pathTeamProjects
u = strings.ReplaceAll(u, "{teamID}", strconv.Itoa(teamID))
r = responderFromFixture("team/list_projects_689492.json", http.StatusOK)
httpmock.RegisterResponderWithQuery("GET", u, "page=1", r)
u = s.client.BaseURL + pathTeamProjects
u = strings.ReplaceAll(u, "{teamID}", strconv.Itoa(teamID))
r = responderFromFixture("team/list_projects_689493.json", http.StatusOK)
httpmock.RegisterResponderWithQuery("GET", u, "page=2", r)

u = s.client.BaseURL + pathTeamProjects
u = strings.ReplaceAll(u, "{teamID}", "689493")
r = responderFromFixture("team/list_projects_689493.json", http.StatusOK)
httpmock.RegisterResponderWithQuery("GET", u, "page=1", r)

// Mock list teams
u = s.client.BaseURL + pathTeamList
r = responderFromFixture("team/list_2.json", http.StatusOK)
httpmock.RegisterResponder("GET", u, r)

actual, err := s.client.FindProjectTeamIDs(projectID)
s.Nil(err)
s.Equal(expected, actual)

expectedCallCount :=
map[string]int{
"GET https://api.rollbar.com/api/1/team/689492/projects?page=1": 1,
"GET https://api.rollbar.com/api/1/team/689492/projects?page=2": 1,
"GET https://api.rollbar.com/api/1/team/689493/projects?page=1": 1,
"GET https://api.rollbar.com/api/1/teams": 1,
}
actualCallCount := httpmock.GetCallCountInfo()
for call, count := range expectedCallCount {
s.Equal(count, actualCallCount[call])
}

s.checkServerErrors("GET", u, func() error {
_, err := s.client.FindProjectTeamIDs(projectID)
return err
})
}

// TestUpdateProjectTeams tests updating the set of teams attached to a Rollbar
// project.
func TestUpdateProjectTeams(t *testing.T) {
Expand Down
Loading

0 comments on commit a4a5f5c

Please sign in to comment.