Compare commits

...

9 Commits
v0.1 ... main

4 changed files with 283 additions and 26 deletions

19
Makefile Normal file
View File

@ -0,0 +1,19 @@
BINARY_DIR=bin
BINARY_NAME=webhook-listener
COVERAGE_DIR=coverage
lint:
golangci-lint run ./...
goreportcard:
goreportcard-cli -v
test:
go test ./...
test-coverage:
rm -rf ${COVERAGE_DIR}
mkdir ${COVERAGE_DIR}
go test -v -coverprofile ${COVERAGE_DIR}/cover.out ./...
go tool cover -html ${COVERAGE_DIR}/cover.out -o ${COVERAGE_DIR}/cover.html
build:
rm -rf ${BINARY_DIR}
mkdir ${BINARY_DIR}
env GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -o ./${BINARY_DIR}/${BINARY_NAME} main.go

184
internal/gitea.go Normal file
View File

@ -0,0 +1,184 @@
package internal
import "time"
type WebhookPayload struct {
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
CompareURL string `json:"compare_url"`
Commits []struct {
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"committer"`
Verification any `json:"verification"`
Timestamp string `json:"timestamp"`
Added []any `json:"added"`
Removed []any `json:"removed"`
Modified []string `json:"modified"`
} `json:"commits"`
TotalCommits int `json:"total_commits"`
HeadCommit struct {
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"committer"`
Verification any `json:"verification"`
Timestamp string `json:"timestamp"`
Added []any `json:"added"`
Removed []any `json:"removed"`
Modified []string `json:"modified"`
} `json:"head_commit"`
Repository struct {
ID int `json:"id"`
Owner struct {
ID int `json:"id"`
Login string `json:"login"`
LoginName string `json:"login_name"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Language string `json:"language"`
IsAdmin bool `json:"is_admin"`
LastLogin time.Time `json:"last_login"`
Created time.Time `json:"created"`
Restricted bool `json:"restricted"`
Active bool `json:"active"`
ProhibitLogin bool `json:"prohibit_login"`
Location string `json:"location"`
Website string `json:"website"`
Description string `json:"description"`
Visibility string `json:"visibility"`
FollowersCount int `json:"followers_count"`
FollowingCount int `json:"following_count"`
StarredReposCount int `json:"starred_repos_count"`
Username string `json:"username"`
} `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
Empty bool `json:"empty"`
Private bool `json:"private"`
Fork bool `json:"fork"`
Template bool `json:"template"`
Parent any `json:"parent"`
Mirror bool `json:"mirror"`
Size int `json:"size"`
Language string `json:"language"`
LanguagesURL string `json:"languages_url"`
HTMLURL string `json:"html_url"`
URL string `json:"url"`
Link string `json:"link"`
SSHURL string `json:"ssh_url"`
CloneURL string `json:"clone_url"`
OriginalURL string `json:"original_url"`
Website string `json:"website"`
StarsCount int `json:"stars_count"`
ForksCount int `json:"forks_count"`
WatchersCount int `json:"watchers_count"`
OpenIssuesCount int `json:"open_issues_count"`
OpenPrCounter int `json:"open_pr_counter"`
ReleaseCounter int `json:"release_counter"`
DefaultBranch string `json:"default_branch"`
Archived bool `json:"archived"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ArchivedAt time.Time `json:"archived_at"`
Permissions struct {
Admin bool `json:"admin"`
Push bool `json:"push"`
Pull bool `json:"pull"`
} `json:"permissions"`
HasIssues bool `json:"has_issues"`
InternalTracker struct {
EnableTimeTracker bool `json:"enable_time_tracker"`
AllowOnlyContributorsToTrackTime bool `json:"allow_only_contributors_to_track_time"`
EnableIssueDependencies bool `json:"enable_issue_dependencies"`
} `json:"internal_tracker"`
HasWiki bool `json:"has_wiki"`
HasPullRequests bool `json:"has_pull_requests"`
HasProjects bool `json:"has_projects"`
HasReleases bool `json:"has_releases"`
HasPackages bool `json:"has_packages"`
HasActions bool `json:"has_actions"`
IgnoreWhitespaceConflicts bool `json:"ignore_whitespace_conflicts"`
AllowMergeCommits bool `json:"allow_merge_commits"`
AllowRebase bool `json:"allow_rebase"`
AllowRebaseExplicit bool `json:"allow_rebase_explicit"`
AllowSquashMerge bool `json:"allow_squash_merge"`
AllowRebaseUpdate bool `json:"allow_rebase_update"`
DefaultDeleteBranchAfterMerge bool `json:"default_delete_branch_after_merge"`
DefaultMergeStyle string `json:"default_merge_style"`
DefaultAllowMaintainerEdit bool `json:"default_allow_maintainer_edit"`
AvatarURL string `json:"avatar_url"`
Internal bool `json:"internal"`
MirrorInterval string `json:"mirror_interval"`
MirrorUpdated time.Time `json:"mirror_updated"`
RepoTransfer any `json:"repo_transfer"`
} `json:"repository"`
Pusher struct {
ID int `json:"id"`
Login string `json:"login"`
LoginName string `json:"login_name"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Language string `json:"language"`
IsAdmin bool `json:"is_admin"`
LastLogin time.Time `json:"last_login"`
Created time.Time `json:"created"`
Restricted bool `json:"restricted"`
Active bool `json:"active"`
ProhibitLogin bool `json:"prohibit_login"`
Location string `json:"location"`
Website string `json:"website"`
Description string `json:"description"`
Visibility string `json:"visibility"`
FollowersCount int `json:"followers_count"`
FollowingCount int `json:"following_count"`
StarredReposCount int `json:"starred_repos_count"`
Username string `json:"username"`
} `json:"pusher"`
Sender struct {
ID int `json:"id"`
Login string `json:"login"`
LoginName string `json:"login_name"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Language string `json:"language"`
IsAdmin bool `json:"is_admin"`
LastLogin time.Time `json:"last_login"`
Created time.Time `json:"created"`
Restricted bool `json:"restricted"`
Active bool `json:"active"`
ProhibitLogin bool `json:"prohibit_login"`
Location string `json:"location"`
Website string `json:"website"`
Description string `json:"description"`
Visibility string `json:"visibility"`
FollowersCount int `json:"followers_count"`
FollowingCount int `json:"following_count"`
StarredReposCount int `json:"starred_repos_count"`
Username string `json:"username"`
} `json:"sender"`
}

View File

@ -7,13 +7,14 @@ import (
) )
type Config struct { type Config struct {
Secret string `yaml:"secret"` Secret string `yaml:"secret"`
Port int `yaml:"port"` Port int `yaml:"port"`
Scripts map[string]ConfigScript `yaml:"scripts"` Projects map[string][]ConfigScript `yaml:"projects"`
} }
type ConfigScript struct { type ConfigScript struct {
BinaryPath string `yaml:"binary"` Environment string `yaml:"environment"`
ScriptPath string `yaml:"script"` Command string `yaml:"command"`
Arguments []string `yaml:"args"`
} }
func LoadConfig(path string) (*Config, error) { func LoadConfig(path string) (*Config, error) {

95
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -8,24 +9,46 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"path" "path"
"runtime" "runtime"
"strings"
"syscall"
"gitea.urkob.com/urko/gitea-webhook-listener/internal"
"gitea.urkob.com/urko/gitea-webhook-listener/kit/config" "gitea.urkob.com/urko/gitea-webhook-listener/kit/config"
) )
func main() { func main() {
// Get root path var err error
_, filename, _, _ := runtime.Caller(0) ctx, cancel := context.WithCancel(signalContext(context.Background()))
cfg, err := config.LoadConfig(path.Join(path.Dir(filename), "configs", "app.yml")) defer cancel()
cfgFile := os.Getenv("CONFIG_FILE")
if cfgFile == "" {
// Get root path
_, filename, _, _ := runtime.Caller(0)
cfgFile = path.Join(path.Dir(filename), "configs", "app.yml")
}
cfg, err := config.LoadConfig(cfgFile)
if err != nil { if err != nil {
log.Fatalf("Error loading config: %v", err) log.Fatalf("Error loading config: %v", err)
} }
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Scripts)) http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Projects))
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
go func() {
err = http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
}()
log.Printf("server is up on %d\n", cfg.Port)
<-ctx.Done()
if err != nil {
log.Println("some error happened", err)
}
log.Println("server shutdown")
} }
func handlePayload(secret string, scripts map[string]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) { func handlePayload(secret string, projects map[string][]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) {
return (func(w http.ResponseWriter, r *http.Request) { return (func(w http.ResponseWriter, r *http.Request) {
// Read the request body // Read the request body
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
@ -36,7 +59,6 @@ func handlePayload(secret string, scripts map[string]config.ConfigScript) func(w
defer r.Body.Close() defer r.Body.Close()
authHeader := r.Header.Get("Authorization") authHeader := r.Header.Get("Authorization")
log.Println("authHeader", authHeader)
if authHeader != secret { if authHeader != secret {
http.Error(w, "Signatures didn't match", http.StatusUnauthorized) http.Error(w, "Signatures didn't match", http.StatusUnauthorized)
return return
@ -48,38 +70,69 @@ func handlePayload(secret string, scripts map[string]config.ConfigScript) func(w
} }
project := r.URL.Query().Get("project") project := r.URL.Query().Get("project")
scr, found := scripts[project] proj, found := projects[project]
if !found { if !found {
http.Error(w, "not found", http.StatusNotFound) http.Error(w, "not found", http.StatusNotFound)
log.Printf("project %s not found\n", project)
return return
} }
var payload internal.WebhookPayload
log.Println("body", body) if err = json.Unmarshal(body, &payload); err != nil {
// Parse the JSON payload
var payload interface{}
err = json.Unmarshal(body, &payload)
if err != nil {
http.Error(w, "Failed to parse JSON payload", http.StatusBadRequest) http.Error(w, "Failed to parse JSON payload", http.StatusBadRequest)
return return
} }
branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1]
log.Println("branchName", branchName)
found = false
var scr config.ConfigScript
for i := range proj {
if proj[i].Environment == branchName {
found = true
scr = proj[i]
break
}
// TODO: Do something with the payload
fmt.Fprintf(w, "I got some JSON: %v", payload)
if err := execute(scr.BinaryPath, scr.ScriptPath); err != nil {
panic(err)
} }
if !found {
http.Error(w, "not found", http.StatusNotFound)
log.Printf("project %s with branch %s not found\n", project, branchName)
return
}
go func() {
if err := execute(scr.Command, scr.Arguments...); err != nil {
log.Println(err)
}
log.Println("script was deployed successful")
}()
}) })
} }
func execute(binaryPath, scriptPath string) error { func execute(command string, args ...string) error {
cmd := exec.Command(binaryPath, scriptPath) cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return fmt.Errorf("cmd.Run %w", err) return fmt.Errorf("cmd.Run %w", err)
} }
log.Println()
return nil return nil
} }
func signalContext(ctx context.Context) context.Context {
ctx, cancel := context.WithCancel(ctx)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
signal.Stop(sigs)
close(sigs)
cancel()
}()
return ctx
}