Skip to content
This repository has been archived by the owner on Dec 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5 from babbel/change-endpoint
Browse files Browse the repository at this point in the history
Change endpoint
  • Loading branch information
Kirill Zonov committed May 9, 2019
2 parents 26f3109 + 9a3728d commit afc646d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 62 deletions.
8 changes: 4 additions & 4 deletions codeclimate/data_source_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ func dataSourceRepository() *schema.Resource {
Read: dataSourceRepositoryRead,

Schema: map[string]*schema.Schema{
"repository_id": &schema.Schema{
"repository_slug": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"test_reporter_id": {
"test_reporter_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
Expand All @@ -23,10 +23,10 @@ func dataSourceRepository() *schema.Resource {
}

func dataSourceRepositoryRead(d *schema.ResourceData, client interface{}) error {
repositoryId := d.Get("repository_id").(string)
repositorySlug := d.Get("repository_slug").(string)

c := client.(codeclimateclient.Client)
repository, err := c.GetRepository(repositoryId)
repository, err := c.GetRepository(repositorySlug)
if err != nil {
return err
}
Expand Down
12 changes: 7 additions & 5 deletions codeclimateclient/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ type Repository struct {
// The structure describes just what we need from the response.
// For the full description look at: https://developer.codeclimate.com/?shell#get-repository
type readRepositoryResponse struct {
Data struct {
Data []struct {
ID string `json:"id"`
Attributes struct {
TestReporterID string `json:"test_reporter_id"`
} `json:"attributes"`
} `json:"data"`
}

func (client *Client) GetRepository(repoId string) (*Repository, error) {
func (client *Client) GetRepository(repositorySlug string) (*Repository, error) {
var repositoryData readRepositoryResponse

data, err := client.makeRequest(fmt.Sprintf("/repos/%s", repoId))
data, err := client.makeRequest(fmt.Sprintf("repos?github_slug=%s", repositorySlug))

if err != nil {
return nil, err
Expand All @@ -35,9 +35,11 @@ func (client *Client) GetRepository(repoId string) (*Repository, error) {
return nil, err
}

// TODO: check size of data

repository := &Repository{
Id: repositoryData.Data.ID,
TestReporterId: repositoryData.Data.Attributes.TestReporterID,
Id: repositoryData.Data[0].ID,
TestReporterId: repositoryData.Data[0].Attributes.TestReporterID,
}

return repository, nil
Expand Down
10 changes: 6 additions & 4 deletions codeclimateclient/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ import (
)

const (
repositoryId = "5b6abdc65b6abdc65b6abdc6"
repositorySlug = "lessonnine/testarepo"
expectedTestReporterId = "0c89092bc2c088d667612ddd1a992ec62f643ded331f40783bcf6b847561234d"
)

func TestGetId(t *testing.T) {
teardown := setup()
defer teardown()

handURL := fmt.Sprintf("/repos/%s", repositoryId)
mux.HandleFunc("/repos", func(w http.ResponseWriter, r *http.Request) {
if r.FormValue("github_slug") != repositorySlug {
t.Fatal(fmt.Errorf("received slug doesn match `%s`", repositorySlug))
}

mux.HandleFunc(handURL, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, getFixture("repositories/repository.json"))
})

repository, err := client.GetRepository(repositoryId)
repository, err := client.GetRepository(repositorySlug)

if err != nil {
t.Fatal(err)
Expand Down
100 changes: 51 additions & 49 deletions codeclimateclient/testdata/fixtures/repositories/repository.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
{
"data": {
"id": "5b6abdc65b6abdc65b6abdc6",
"type": "repos",
"attributes": {
"analysis_version": 51450,
"badge_token": "d6dd39dd84d8575d1ddd",
"branch": "master",
"created_at": "2019-02-14T10:49:44.319Z",
"delegated_config_repo_id": "",
"diff_coverage_enforced": true,
"diff_coverage_threshold": 50,
"enable_notifications": true,
"github_slug": "lessonnine/testarepo",
"human_name": "testarepo",
"last_activity_at": "2019-04-24T15:14:09.719Z",
"test_reporter_id": "0c89092bc2c088d667612ddd1a992ec62f643ded331f40783bcf6b847561234d",
"total_coverage_enforced": true,
"vcs_database_id": "170111000",
"vcs_host": "https://github.com",
"score": null
},
"relationships": {
"latest_default_branch_snapshot": {
"data": {
"id": "5cc07d3643131c000101372f",
"type": "snapshots"
}
"data": [
{
"id": "5b6abdc65b6abdc65b6abdc6",
"type": "repos",
"attributes": {
"analysis_version": 51450,
"badge_token": "d6dd39dd84d8575d1ddd",
"branch": "master",
"created_at": "2019-02-14T10:49:44.319Z",
"delegated_config_repo_id": "",
"diff_coverage_enforced": true,
"diff_coverage_threshold": 50,
"enable_notifications": true,
"github_slug": "lessonnine/testarepo",
"human_name": "testarepo",
"last_activity_at": "2019-04-24T15:14:09.719Z",
"test_reporter_id": "0c89092bc2c088d667612ddd1a992ec62f643ded331f40783bcf6b847561234d",
"total_coverage_enforced": true,
"vcs_database_id": "170111000",
"vcs_host": "https://github.com",
"score": null
},
"latest_default_branch_test_report": {
"data": {
"id": "5cc07da55941051a3400872a",
"type": "test_reports"
"relationships": {
"latest_default_branch_snapshot": {
"data": {
"id": "5cc07d3643131c000101372f",
"type": "snapshots"
}
},
"latest_default_branch_test_report": {
"data": {
"id": "5cc07da55941051a3400872a",
"type": "test_reports"
}
},
"account": {
"data": {
"id": "52526f7356b102306b01a594",
"type": "orgs"
}
}
},
"account": {
"data": {
"id": "52526f7356b102306b01a594",
"type": "orgs"
"links": {
"self": "https://codeclimate.com/repos/5b6abdc65b6abdc65b6abdc6",
"services": "https://api.codeclimate.com/v1/repos/5b6abdc65b6abdc65b6abdc6/services",
"web_coverage": "https://codeclimate.com/repos/5b6abdc65b6abdc65b6abdc6/coverage",
"web_issues": "https://codeclimate.com/repos/5b6abdc65b6abdc65b6abdc6/issues",
"maintainability_badge": "https://api.codeclimate.com/v1/badges/d6dd39dd84d8575d1ddd/maintainability",
"test_coverage_badge": "https://api.codeclimate.com/v1/badges/d6dd39dd84d8575d1ddd/test_coverage"
},
"meta": {
"permissions": {
"admin": true
}
}
},
"links": {
"self": "https://codeclimate.com/repos/5b6abdc65b6abdc65b6abdc6",
"services": "https://api.codeclimate.com/v1/repos/5b6abdc65b6abdc65b6abdc6/services",
"web_coverage": "https://codeclimate.com/repos/5b6abdc65b6abdc65b6abdc6/coverage",
"web_issues": "https://codeclimate.com/repos/5b6abdc65b6abdc65b6abdc6/issues",
"maintainability_badge": "https://api.codeclimate.com/v1/badges/d6dd39dd84d8575d1ddd/maintainability",
"test_coverage_badge": "https://api.codeclimate.com/v1/badges/d6dd39dd84d8575d1ddd/test_coverage"
},
"meta": {
"permissions": {
"admin": true
}
}
}
]
}

0 comments on commit afc646d

Please sign in to comment.