commit da5cc061d5be0b1d4454b02e0d2ace980708f466 from: Sergey Bronnikov via: Sergey Bronnikov date: Fri Mar 26 16:23:26 2021 UTC backends: add support of Circle CI commit - fa187aacd577fdf8fc1410827de589c7e7930eb0 commit + da5cc061d5be0b1d4454b02e0d2ace980708f466 blob - 98158de4d6dde069b3d5bb6ab16f7d207c7e73df blob + 6c98f2abaeb2fe2c1571bd8955832c2aee275cf3 --- README.md +++ README.md @@ -73,6 +73,19 @@ projects follow steps in [documentation](https://circleci.com/docs/api/#add-an-api-token) to obtain API token. +Example of configuration file for [Helios project](https://github.com/spotify/helios): + +```yaml +projects: +- name: helios + backends: + - type: circleci + branch: master + base: https://circleci.com/ + project: spotify/helios + username: + secret: +``` ### Cirrus CI Cirrus CI allows to [store test report blob - 2a33f857ce99c99d81cbb498d987ab8d6b61fac5 blob + 25c002da9397bec11d4dcccffa1cad4e0e984d97 --- backends/backend_circleci.go +++ backends/backend_circleci.go @@ -7,42 +7,73 @@ import ( "github.com/ligurio/testres-db/formats" "log" "net/http" + "strconv" "strings" ) +// limit a number of builds that should be processed +// -1 means all available builds +const buildsNumber = 10 +const isDebug = false + func SyncCircleCI(client *http.Client, b *Backend) (*[]formats.TestResult, error) { project_path := strings.Split(b.Project, "/") if len(project_path) != 2 { log.Println("Perhaps wrong project name specified") + return nil, nil } account := project_path[0] repo := project_path[1] - connection := &circleci.Client{Token: b.Secret, HTTPClient: client, Debug: true} - builds, err := connection.ListRecentBuildsForProject(account, repo, b.Branch, "", -1, 0) + connection := &circleci.Client{Token: b.Secret, HTTPClient: client, Debug: isDebug} + builds, err := connection.ListRecentBuildsForProject(account, repo, b.Branch, "", buildsNumber, 0) if err != nil { log.Println(err) return nil, err } + log.Printf("Found %d builds\n", len(builds)) + if len(builds) == 0 { + log.Println("no builds found") + return nil, nil + } + + status := map[string]formats.TestStatus{ + "success": formats.StatusPass, + "skipped": formats.StatusSkip, + "failed": formats.StatusFail, + } + + results := make([]formats.TestResult, len(builds)) for _, build := range builds { log.Printf("Found build: %d, status %s\n", build.BuildNum, build.Status) metadata, err := connection.ListTestMetadata(account, repo, build.BuildNum) if err != nil { log.Println(err) - return nil, err + continue } + if len(metadata) == 0 { + log.Printf("\tno tests\n") + continue + } else { + log.Printf("\ttests %d\n", len(metadata)) + } artifacts, err := connection.ListBuildArtifacts(account, repo, build.BuildNum) for _, artifact := range artifacts { log.Println("Found artifact:", artifact.URL) } + var testcases []formats.TestCase for _, test := range metadata { + var testcase formats.TestCase log.Println("Found test:", test.Result, test.Name, test.RunTime) + testcase = formats.TestCase{Name: test.Name, Status: status[test.Result]} + testcases = append(testcases, testcase) } + results = append(results, formats.TestResult{Name: strconv.Itoa(build.BuildNum), TestCases: testcases}) } - return nil, nil + return &results, nil } blob - 036bd24a3d6f4d2c4daf823e22c49e5c090194fe blob + 3720381818e2409b89936a7a492a51e4fd38d367 --- backends/backend_circleci_test.go +++ backends/backend_circleci_test.go @@ -1,10 +1,23 @@ -// https://github.com/drone/drone-go/blob/master/drone/client_test.go -// https://github.com/ktrysmt/go-bitbucket/blob/master/tests/repository_test.go - package backends -import "testing" +import ( + "os" + "testing" +) func TestSyncCircleCI(t *testing.T) { - t.Log("TestSyncCircleCI") + t.Log("Basic test with spotify/helios project") + username := os.Getenv("CIRCLECI_USERNAME") + token := os.Getenv("CIRCLECI_TOKEN") + if username == "" || token == "" { + t.Skip("No CIRCLECI_USERNAME and CIRCLECI_TOKEN.") + } + backend := Backend{Type: "circleci", Base: "https://circleci.com/", Name: "spotify/helios", + Project: "spotify/helios", Branch: "master", Username: username, Secret: token} + + httpClient := NewAPIClient() + builds, err := SyncCircleCI(httpClient, &backend) + if builds == nil || err != nil { + t.Failed() + } }