-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package github | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)