Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Github Actions Runners endpoint by decorating github client #1

Merged
merged 2 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ GARO currently supports listing a page of organization repositories.
export GH_TOKEN=MYDUMMYPERSONALGHTOKEN
./garo my-gh-organization
```

## Test

```bash
export GH_TOKEN=MYDUMMYPERSONALGHTOKEN
go test -v ./...
```
111 changes: 111 additions & 0 deletions github/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package github

import (
"context"
"fmt"
"net/http"

"github.com/google/go-github/github"
)

// Runner represents a self hosted runner object
type Runner struct {
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
OS *string `json:"os,omitempty"`
Status *string `json:"status,omitempty"`
}

// RunnerDownload represents a self hosted runner download object
type RunnerDownload struct {
OS *string `json:"os,omitempty"`
Architecture *string `json:"architecture,omitempty"`
DownloadURL *string `json:"download_url,omitempty"`
Filename *string `json:"filename,omitempty"`
}

// ActionsToken represents a create/remove token for a Github actions runner
type ActionsToken struct {
Token *string `json:"token,omitempty"`
ExpiresAt *github.Timestamp `json:"expires_at,omitempty"`
}

// Client decorates the github.Client with newly added github actions API calls.
type Client struct {
*github.Client
}

// NewClient creates an instance of the decorated github.Client with newly added github actions API calls.
func NewClient(httpClient *http.Client) *Client {
Copy link
Member Author

@marcofranssen marcofranssen Feb 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This decorator and related structures is only temporarely until all the github action APIs are merged on the github client.
google/go-github#1399 (comment)

return &Client{github.NewClient(httpClient)}
}

// ListActionsRunners Lists all self-hosted runners for a repository. Anyone with admin access to the repository can use this endpoint. GitHub Apps must have the administration permission to use this endpoint.
func (c *Client) ListActionsRunners(ctx context.Context, owner, repo string) ([]*Runner, *github.Response, error) {
req, err := RestRequest(http.MethodGet, "%srepos/%s/%s/actions/runners", c.BaseURL, owner, repo)
if err != nil {
return nil, nil, err
}

var runners []*Runner
res, err := c.Do(ctx, req, &runners)
if err != nil {
return nil, res, err
}
return runners, res, nil
}

// ListActionsRunnersDownloads Lists binaries for the self-hosted runner application that you can download and run. Anyone with admin access to the repository can use this endpoint. GitHub Apps must have the administration permission to use this endpoint.
func (c *Client) ListActionsRunnersDownloads(ctx context.Context, owner, repo string) ([]*RunnerDownload, *github.Response, error) {
req, err := RestRequest(http.MethodGet, "%srepos/%s/%s/actions/runners/downloads", c.BaseURL, owner, repo)
if err != nil {
return nil, nil, err
}

var runners []*RunnerDownload
res, err := c.Do(ctx, req, &runners)
if err != nil {
return nil, res, err
}
return runners, res, nil
}

// CreateActionRunnersRegistrationToken Returns a token that you can pass to the config script. The token expires after one hour. Anyone with admin access to the repository can use this endpoint. GitHub Apps must have the administration permission to use this endpoint.
func (c *Client) CreateActionRunnersRegistrationToken(ctx context.Context, owner, repo string) (*ActionsToken, *github.Response, error) {
return c.CreateActionRunnersTokenRequest(ctx, owner, repo, "create")
}

// CreateActionRunnersRemoveToken Returns a token that you can pass to remove a self-hosted runner from a repository. The token expires after one hour. Anyone with admin access to the repository can use this endpoint. GitHub Apps must have the administration permission to use this endpoint.
func (c *Client) CreateActionRunnersRemoveToken(ctx context.Context, owner, repo string) (*ActionsToken, *github.Response, error) {
return c.CreateActionRunnersTokenRequest(ctx, owner, repo, "remove")
}

// CreateActionRunnersTokenRequest Returns a create/remove token which can be used for the config script or remove script. The token expires after one hour. Anyone with admin access to the repository can use this endpoint. GitHub Apps must have the administration permission to use this endpoint.
func (c *Client) CreateActionRunnersTokenRequest(ctx context.Context, owner, repo, tokenType string) (*ActionsToken, *github.Response, error) {
req, err := RestRequest(http.MethodPost, "%srepos/%s/%s/actions/runners/%s-token", c.BaseURL, owner, repo, tokenType)
if err != nil {
return nil, nil, err
}

var token ActionsToken
res, err := c.Do(ctx, req, &token)
if err != nil {
return nil, res, err
}
return &token, res, nil
}

// DeleteActionsRunners Forces the removal of a self-hosted runner from a repository. You can use this endpoint to completely remove the runner when the machine you were using no longer exists. Anyone with admin access to the repository can use this endpoint. GitHub Apps must have the administration permission to use this endpoint.
func (c *Client) DeleteActionsRunners(ctx context.Context, owner, repo string, runnerID int64) (*github.Response, error) {
req, err := RestRequest(http.MethodDelete, "%srepos/%s/%s/actions/runners/%d", c.BaseURL, owner, repo, runnerID)
if err != nil {
return nil, err
}
return c.Do(ctx, req, nil)
}

// RestRequest creates a http.Request for the given method and format string to build the url
func RestRequest(method string, format string, args ...interface{}) (*http.Request, error) {
url := fmt.Sprintf(format, args...)
return http.NewRequest(method, url, nil)
}
44 changes: 44 additions & 0 deletions github/actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package github

import (
"context"
"log"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

var (
ctx context.Context
client *Client
)

func init() {
token := os.Getenv("GH_TOKEN")
if token == "" {
log.Fatal("You need to export the environment variable GH_TOKEN")
}
ctx = context.Background()
oauthClient := PersonalAccessToken(ctx, token)
client = NewClient(oauthClient)
}

func TestListActionRunners(t *testing.T) {
assert := assert.New(t)
org := "philips-internal"
repo := "fact-service"
runners, _, err := client.ListActionsRunners(ctx, org, repo)
assert.NoError(err, "Failed listing action runners for %s/%s", org, repo)
assert.NotEmpty(runners)
}

func TestListActionRunnerDownloads(t *testing.T) {
assert := assert.New(t)
org := "philips-internal"
repo := "fact-service"
downloads, _, err := client.ListActionsRunnersDownloads(ctx, org, repo)
assert.NoError(err, "Failed listing action runner downloads for %s/%s", org, repo)
assert.NotEmpty(downloads)
assert.Len(downloads, 5, "Expected to have 5 downloads")
}
2 changes: 1 addition & 1 deletion oauth2.go → github/oauth2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package github

import (
"context"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ go 1.13
require (
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -15,3 +22,6 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"os"

"github.com/google/go-github/github"
"github.com/philips-labs/garo/github"
)

func main() {
Expand All @@ -19,7 +19,7 @@ func main() {
}

ctx := context.Background()
oauthClient := PersonalAccessToken(ctx, token)
oauthClient := github.PersonalAccessToken(ctx, token)
client := github.NewClient(oauthClient)

repos, _, err := client.Repositories.ListByOrg(ctx, args[0], nil)
Expand Down