Skip to content

Commit

Permalink
Add cirrus
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienvermeille committed Sep 23, 2023
1 parent f6844b6 commit f7d928c
Show file tree
Hide file tree
Showing 5 changed files with 412 additions and 60 deletions.
44 changes: 36 additions & 8 deletions cirrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,46 @@ func NewClient(cirrusToken string) graphql.Client {
}
}()

var httpClient http.Client
if cirrusToken == "" {
err = fmt.Errorf("must set GITHUB_TOKEN=<github token>")
return nil
}
// based on cirrus: https://cirrus-ci.org/api/#authorization
// if you only need read access to public repositories of your organization you can skip this step and don't provide Authorization header.
httpClient = http.Client{
Transport: &anonymousTransport{
wrapped: http.DefaultTransport,
},
}

httpClient := http.Client{
Transport: &authedTransport{
key: cirrusToken,
wrapped: http.DefaultTransport,
},
} else {
httpClient = http.Client{
Transport: &authedTransport{
key: cirrusToken,
wrapped: http.DefaultTransport,
},
}
}

graphqlClient := graphql.NewClient("https://api.cirrus-ci.com/graphql", &httpClient)

return graphqlClient
}

type authedTransport struct {
key string
wrapped http.RoundTripper
}

func (t *authedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "bearer "+t.key)
return t.wrapped.RoundTrip(req)
}

type anonymousTransport struct {
wrapped http.RoundTripper
}

func (t *anonymousTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.wrapped.RoundTrip(req)
}

//go:generate go run github.com/Khan/genqlient genqlient.yaml
19 changes: 19 additions & 0 deletions cirrus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cirrus

import (
"context"
"fmt"
"testing"
)

func TestNewClient(t *testing.T) {
emptyToken := ""
graphqlClient := NewClient(emptyToken)
var response *getRepositoryBranchStatusResponse
var err error
response, err = getRepositoryBranchStatus(context.Background(), graphqlClient, "github", "sebastienvermeille", "gocirrus-graphql-client", "master")
if err != nil {
return
}
fmt.Println("last build status: ", response.OwnerRepository.Builds.Edges[0].Node.Status)
}
Loading

0 comments on commit f7d928c

Please sign in to comment.