105 lines
2.7 KiB
Go
105 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type TimesheetPayload struct {
|
|
Date string `json:"date"`
|
|
Description string `json:"description"`
|
|
GiteaUsername string `json:"gitea_username"`
|
|
HourSpent float64 `json:"hour_spent"`
|
|
TaskID int64 `json:"task_id"`
|
|
}
|
|
|
|
func main() {
|
|
baseURL := os.Getenv("BASE_URL")
|
|
issueNumber := os.Getenv("ISSUE_NUMBER")
|
|
sender := os.Getenv("SENDER_LOGIN")
|
|
title := os.Getenv("ISSUE_TITLE")
|
|
trackedSeconds := os.Getenv("TRACKED_SECONDS")
|
|
trackedCreated := os.Getenv("TRACKED_CREATED")
|
|
action := os.Getenv("EVENT_ACTION")
|
|
|
|
if baseURL == "" || issueNumber == "" || sender == "" || trackedSeconds == "" || trackedCreated == "" {
|
|
fmt.Println("Some required environment variables are missing.")
|
|
os.Exit(1)
|
|
}
|
|
if action != "add_time" && action != "edited" && action != "time_tracked" && action != "time_added" {
|
|
// Best-effort: only proceed for time-related actions; allow edited if your instance sends edited
|
|
fmt.Printf("Skipping action: %s\n", action)
|
|
return
|
|
}
|
|
|
|
// Logger
|
|
logFile, err := os.OpenFile("action.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
|
if err != nil {
|
|
fmt.Printf("Error creating log file: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer logFile.Close()
|
|
logger := log.New(io.MultiWriter(os.Stdout, logFile), "", log.LstdFlags)
|
|
|
|
var taskID int64
|
|
fmt.Sscanf(issueNumber, "%d", &taskID)
|
|
|
|
seconds, err := strconv.ParseInt(trackedSeconds, 10, 64)
|
|
if err != nil {
|
|
logger.Printf("Invalid TRACKED_SECONDS: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
hours := float64(seconds) / 3600.0
|
|
|
|
desc := title
|
|
if strings.TrimSpace(desc) == "" {
|
|
desc = "/"
|
|
}
|
|
|
|
date := trackedCreated
|
|
if idx := strings.Index(date, "T"); idx > 0 {
|
|
date = date[:idx]
|
|
}
|
|
|
|
payload := TimesheetPayload{
|
|
Date: date,
|
|
Description: desc,
|
|
GiteaUsername: sender,
|
|
HourSpent: hours,
|
|
TaskID: taskID,
|
|
}
|
|
|
|
body, err := json.Marshal(payload)
|
|
if err != nil {
|
|
logger.Printf("Error marshalling payload: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
endpoint := strings.TrimRight(baseURL, "/") + "/api/v1/account_analytic_gitea_odoo/"
|
|
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewReader(body))
|
|
if err != nil {
|
|
logger.Printf("Error creating POST request: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
logger.Printf("Error calling FastAPI: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer resp.Body.Close()
|
|
respBody, _ := io.ReadAll(resp.Body)
|
|
logger.Printf("FastAPI status: %s\nResponse: %s\n", resp.Status, string(respBody))
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
os.Exit(1)
|
|
}
|
|
}
|