Skip to content
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
15 changes: 10 additions & 5 deletions gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ func main() {
return
}

out := filepath.Join(".", *outputDir)
err := os.MkdirAll(out, os.ModePerm)
imp, err := goGithubImportPath("go.mod")
if err != nil {
panic(err)
}
params.GoGithubImport = imp

out := filepath.Join(".", *outputDir)
if err := os.MkdirAll(out, os.ModePerm); err != nil {
panic("failed to create output directory")
}

// create events.go
err = ExecuteWebhookEventTemplate(filepath.Join(out, "events"), params)
if err != nil {
if err := ExecuteWebhookEventTemplate(filepath.Join(out, "events"), params); err != nil {
panic(err)
}

Expand All @@ -59,7 +63,8 @@ func main() {
fileName := "events_" + param.Name
outFile := filepath.Join(out, fileName)
err := ExecuteWebhookEventTypesTemplate(outFile, TemplateParameters{
Webhooks: []GithubWebhooks{param},
GoGithubImport: imp,
Webhooks: []GithubWebhooks{param},
})
if err != nil {
panic(err)
Expand Down
38 changes: 38 additions & 0 deletions gen/gomod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"os"
"regexp"
"sort"
)

var goGithubModuleRE = regexp.MustCompile(`github\.com/google/go-github/v\d+`)

// goGithubImportPath reads gomodPath and returns the go-github package import
// path (module path + "/github") derived from the module's require directive.
// This keeps the go-github major version defined in exactly one place.
func goGithubImportPath(gomodPath string) (string, error) {
data, err := os.ReadFile(gomodPath)
if err != nil {
return "", fmt.Errorf("read %s: %w", gomodPath, err)
}
matches := goGithubModuleRE.FindAllString(string(data), -1)
uniq := map[string]struct{}{}
for _, m := range matches {
uniq[m] = struct{}{}
}
switch len(uniq) {
case 0:
return "", fmt.Errorf("no github.com/google/go-github require found in %s", gomodPath)
case 1:
return matches[0] + "/github", nil
default:
uniqueVersions := make([]string, 0, len(uniq))
for v := range uniq {
uniqueVersions = append(uniqueVersions, v)
}
sort.Strings(uniqueVersions)
return "", fmt.Errorf("multiple go-github module versions found in %s: %v", gomodPath, uniqueVersions)
}
}
58 changes: 58 additions & 0 deletions gen/gomod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"os"
"path/filepath"
"testing"
)

func writeGoMod(t *testing.T, content string) string {
t.Helper()
dir := t.TempDir()
p := filepath.Join(dir, "go.mod")
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
return p
}

func TestGoGithubImportPath(t *testing.T) {
p := writeGoMod(t, `module github.com/cbrgm/githubevents/v2

go 1.25.0

require (
github.com/google/go-github/v89 v89.0.0
golang.org/x/sync v0.22.0
)
`)
got, err := goGithubImportPath(p)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if want := "github.com/google/go-github/v89/github"; got != want {
t.Fatalf("got %q, want %q", got, want)
}
}

func TestGoGithubImportPathMissing(t *testing.T) {
p := writeGoMod(t, "module x\n\ngo 1.25.0\n")
if _, err := goGithubImportPath(p); err == nil {
t.Fatal("expected error when no go-github require present")
}
}

func TestGoGithubImportPathMultiple(t *testing.T) {
p := writeGoMod(t, `module github.com/cbrgm/githubevents/v2

go 1.25.0

require (
github.com/google/go-github/v89 v89.0.0
github.com/google/go-github/v90 v90.0.0
)
`)
if _, err := goGithubImportPath(p); err == nil {
t.Fatal("expected error when multiple distinct go-github versions present")
}
}
4 changes: 3 additions & 1 deletion gen/template_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

// TemplateParameters represents template parameters.
type TemplateParameters struct {
Webhooks []GithubWebhooks
// GoGithubImport is the go-github package import path, derived from go.mod.
GoGithubImport string
Webhooks []GithubWebhooks
}

// GithubWebhooks represents a Github webhook event type parameters.
Expand Down
2 changes: 1 addition & 1 deletion gen/template_webhook_event.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package githubevents
import (
"context"
"fmt"
"github.com/google/go-github/v89/github"
"{{ .GoGithubImport }}"
"golang.org/x/sync/errgroup"
"net/http"
"sync"
Expand Down
2 changes: 1 addition & 1 deletion gen/template_webhook_event_tests.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package githubevents
import (
"context"
"errors"
"github.com/google/go-github/v89/github"
"{{ .GoGithubImport }}"
"testing"
"sync"
)
Expand Down
2 changes: 1 addition & 1 deletion gen/template_webhook_event_types.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package githubevents
import (
"context"
"fmt"
"github.com/google/go-github/v89/github"
"{{ .GoGithubImport }}"
"golang.org/x/sync/errgroup"
)

Expand Down