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

checks API WIP #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ to set up your token.
Then create a secret using that token.

```bash
# First, copy your token to the clipboard.
# Before running the next command, copy your token to the clipboard.
# Then use pbpaste to paste that token into a file called "token".
pbpaste > token
kubectl create secret generic github-token --from-file=token -n argo
rm token
Expand All @@ -51,8 +52,8 @@ rm token

Install:

kubectl apply -f github-executor-plugin-configmap.yaml
kubectl apply -n argo -f github-executor-plugin-configmap.yaml

Uninstall:

kubectl delete cm github-executor-plugin
kubectl delete -n argo cm github-executor-plugin
2 changes: 1 addition & 1 deletion cmd/github-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
gitHubClient := &githubexecutor.GitHubClient{Issues: client.Issues}
gitHubClient := &githubexecutor.GitHubClient{Issues: client.Issues, Checks: client.Checks}
executor := githubexecutor.NewGitHubExecutor(gitHubClient, string(agentToken))
http.HandleFunc("/api/v1/template.execute", githubexecutor.GitHubPlugin(&executor))
err = http.ListenAndServe(":4356", nil)
Expand Down
20 changes: 20 additions & 0 deletions internal/github_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func (e *GitHubExecutor) runAction(plugin *PluginSpec) (string, error) {
var expectedResponseCode int
if plugin.GitHub.Issue != nil {
response, expectedResponseCode, err = e.runIssueAction(ctx, plugin.GitHub.Issue)
} else if plugin.GitHub.Check != nil {
response, expectedResponseCode, err = e.runCheckAction(ctx, plugin.GitHub.Check)
} else {
return "", fmt.Errorf("unsupported action")
}
Expand Down Expand Up @@ -121,6 +123,24 @@ func (e *GitHubExecutor) runIssueAction(ctx context.Context, issueAction *IssueA
return nil, 0, fmt.Errorf("unsupported issue action")
}

func (e *GitHubExecutor) runCheckAction(ctx context.Context, checkAction *CheckActionSpec) (*github.Response, int, error) {
if err := validateCheckAction(checkAction); err != nil {
return nil, 0, fmt.Errorf("failed to validate check action: %w", err)
}
_, response, err := e.client.Checks.CreateCheckRun(ctx, checkAction.Create.Owner, checkAction.Create.Repo, checkAction.Create.Request)
return response, 201, err
}

func validateCheckAction(action *CheckActionSpec) error {
if action.Create == nil {
return err
}
if action.Create.Repo == "" {
return err
}
if action.Create.
}

func validateIssueAction(action *IssueActionSpec) error {
if action.Comment == nil && action.Create == nil {
return fmt.Errorf("the only available issue actions are 'comment' and 'create")
Expand Down
5 changes: 5 additions & 0 deletions internal/github_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import (

type GitHubClient struct {
Issues GitHubIssuesClient
Checks GitHubChecksClient
}

type GitHubIssuesClient interface {
CreateComment(ctx context.Context, owner, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
Create(ctx context.Context, owner, repo string, issue *github.IssueRequest) (*github.Issue, *github.Response, error)
}

type GitHubChecksClient interface {
CreateCheckRun(ctx context.Context, owner, repo string, opt github.CreateCheckRunOptions) (*github.CheckRun, *github.Response, error)
}
11 changes: 11 additions & 0 deletions internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type PluginSpec struct {

type ActionSpec struct {
Issue *IssueActionSpec `json:"issue,omitempty"`
Check *CheckActionSpec `json:"check,omitempty"`
Timeout string `json:"timeout,omitempty"`
}

Expand All @@ -29,3 +30,13 @@ type IssueCreateAction struct {
Repo string `json:"repo,omitempty"`
Request *github.IssueRequest `json:"-"`
}

type CheckCreateAction struct {
Owner string `json:"owner,omitempty"`
Repo string `json:"repo,omitempty"`
Request github.CreateCheckRunOptions `json:"-"`
}

type CheckActionSpec struct {
Create CheckCreateAction `json:"create,omitempty"`
}
32 changes: 32 additions & 0 deletions plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,38 @@ metadata:
number: "1" # PR number, from the
owner: crenshaw-dev
repo: github-executor-plugin
check:
create:
owner: # Required
repo: # Required
name: # Required
head_branch: # Optional
head_sha: # Optional
details_url: # Optional
external_id: # Optional
status: # Optional
conclusion: # Optional
started_at: # Optional
completed_at: # Optional
output: # Optional
title:
summary:
text:
annotations_count:
annotations_url:
annotations:
- filename:
blob_href:
start_line:
end_line:
warning_level:
message:
title:
raw_details:
images:
- alt:
image_url:
caption:
```

## Prerequisites
Expand Down