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

Initial commit for databrickslabs/sandbox/acceptance #56

Merged
merged 43 commits into from
Feb 2, 2024
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
32 changes: 32 additions & 0 deletions .github/workflows/acceptance-go-libs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: acceptance

on:
pull_request:
types: [opened, synchronize]
paths: ['go-libs/**']

permissions:
id-token: write
contents: read
pull-requests: write

jobs:
acceptance:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.21

- name: Acceptance
uses: databrickslabs/sandbox/acceptance@gh-action
with:
directory: go-libs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

21 changes: 21 additions & 0 deletions acceptance/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: 'Databricks Labs Acceptance Suite'
description: 'Run relevant acceptance suite'
author: Serge Smertin
inputs:
go-version:
description: 'Go version'
required: false
default: 1.21.0
directory:
description: 'Working directory'
required: false
default: .
outputs:
sample:
description: 'Sample output'
value: ${{ steps.invoke.outputs.sample }}

runs:
using: node20
main: shim.js
124 changes: 124 additions & 0 deletions acceptance/boilerplate/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package boilerplate

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/databrickslabs/sandbox/go-libs/env"
"github.com/databrickslabs/sandbox/go-libs/github"
"github.com/sethvargo/go-githubactions"
)

func New(ctx context.Context, opts ...githubactions.Option) (*boilerplate, error) {
opts = append(opts, githubactions.WithGetenv(func(key string) string {
return env.Get(ctx, key)
}))
a := githubactions.New(opts...)
// token, err := a.GetIDToken(context.Background(), "")
// if err != nil {
// return nil, fmt.Errorf("oidc: %w", err)
// }
context, err := a.Context()
if err != nil {
return nil, err
}
return &boilerplate{
Action: a,
context: context,
GitHub: github.NewClient(&github.GitHubConfig{
GitHubTokenSource: github.GitHubTokenSource{},
}),
}, nil
}

type boilerplate struct {
Action *githubactions.Action
context *githubactions.GitHubContext
GitHub *github.GitHubClient
}

func (a *boilerplate) RunURL(ctx context.Context) (string, error) {
org, repo := a.context.Repo()
workflowJobs := a.GitHub.ListWorkflowJobs(ctx, org, repo, a.context.RunID)
for workflowJobs.HasNext(ctx) {
job, err := workflowJobs.Next(ctx)
if err != nil {
return "", err
}
if job.RunnerName == a.Action.Getenv("RUNNER_NAME") {
url := fmt.Sprintf("%s/%s/actions/runs/%d/job/%d", // ?pr=56
a.context.ServerURL, a.context.Repository, a.context.RunID, job.ID)
return url, nil
}
}
return "", fmt.Errorf("id not found for current run: %s", a.context.Job)
}

func (a *boilerplate) tag() string {
// The ref path to the workflow. For example,
// octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch.
return fmt.Sprintf("\n<!-- workflow:%s -->", a.Action.Getenv("GITHUB_WORKFLOW_REF"))
}

func (a *boilerplate) taggedComment(ctx context.Context, body string) (string, error) {
runUrl, err := a.RunURL(ctx)
if err != nil {
return "", fmt.Errorf("run url: %w", err)
}
return fmt.Sprintf("%s\n\n<sub>Running from [%s #%d](%s)</sub>%s",
body, a.context.Workflow, a.context.RunNumber, runUrl, a.tag()), nil
}

func (a *boilerplate) currentPullRequest(ctx context.Context) (*github.PullRequest, error) {
if a.context.Event == nil {
return nil, fmt.Errorf("missing actions event")
}
raw, err := json.MarshalIndent(a.context.Event, "", " ")
if err != nil {
return nil, fmt.Errorf("marshall: %w", err)
}
var event struct {
PullRequest *github.PullRequest `json:"pull_request"`
}
err = json.Unmarshal(raw, &event)
if err != nil {
return nil, fmt.Errorf("unmarshall: %w", err)
}
return event.PullRequest, nil
}

func (a *boilerplate) Comment(ctx context.Context, commentText string) error {
pr, err := a.currentPullRequest(ctx)
if err != nil {
return fmt.Errorf("pr: %w", err)
}
tag := a.tag()
org, repo := a.context.Repo()
it := a.GitHub.GetIssueComments(ctx, org, repo, pr.Number)
for it.HasNext(ctx) {
comment, err := it.Next(ctx)
if err != nil {
return fmt.Errorf("comment: %w", err)
}
if !strings.Contains(comment.Body, tag) {
continue
}
text, err := a.taggedComment(ctx, commentText)
if err != nil {
return fmt.Errorf("text: %w", err)
}
_, err = a.GitHub.UpdateIssueComment(ctx, org, repo, comment.ID, text)
return err
}
text, err := a.taggedComment(ctx, commentText)
if err != nil {
return fmt.Errorf("text: %w", err)
}
_, err = a.GitHub.CreateIssueComment(ctx, org, repo, pr.Number, text)
if err != nil {
return fmt.Errorf("new comment: %w", err)
}
return nil
}
Loading
Loading