Mandresy RABENJAHARISON d6f8214a00 initial commit
2025-08-11 15:58:36 +03:00

102 lines
2.4 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
)
type Payload struct {
TaskID int64 `json:"task_id"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
Author string `json:"author"`
Assignees []string `json:"assignees"`
Labels []string `json:"labels"`
DueDate string `json:"date_deadline,omitempty"`
}
func splitCSV(value string) []string {
if strings.TrimSpace(value) == "" {
return []string{}
}
parts := strings.Split(value, ",")
result := make([]string, 0, len(parts))
for _, p := range parts {
p = strings.TrimSpace(p)
if p != "" {
result = append(result, p)
}
}
return result
}
func main() {
baseURL := os.Getenv("BASE_URL")
if baseURL == "" {
fmt.Println("BASE_URL not provided.")
os.Exit(1)
}
// 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)
// Build payload from env
issueNumber := os.Getenv("ISSUE_NUMBER")
issueTitle := os.Getenv("ISSUE_TITLE")
issueBody := os.Getenv("ISSUE_BODY")
issueState := os.Getenv("ISSUE_STATE")
issueAuthor := os.Getenv("ISSUE_AUTHOR")
issueAssignees := splitCSV(os.Getenv("ISSUE_ASSIGNEES"))
issueLabels := splitCSV(os.Getenv("ISSUE_LABELS"))
issueDueDate := os.Getenv("ISSUE_DUE_DATE")
var taskID int64
if issueNumber != "" {
// Best-effort parse; if it fails, taskID remains 0
fmt.Sscanf(issueNumber, "%d", &taskID)
}
payload := Payload{
TaskID: taskID,
Title: issueTitle,
Body: issueBody,
State: issueState,
Author: issueAuthor,
Assignees: issueAssignees,
Labels: issueLabels,
DueDate: issueDueDate,
}
payloadBytes, err := json.Marshal([]Payload{payload})
if err != nil {
logger.Printf("Error marshalling JSON: %v\n", err)
os.Exit(1)
}
endpoint := strings.TrimRight(baseURL, "/") + "/api/v1/patch_gitea_odoo"
resp, err := http.Post(endpoint, "application/json", bytes.NewBuffer(payloadBytes))
if err != nil {
logger.Printf("HTTP request failed: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
logger.Printf("Status: %s\nResponse: %s\n", resp.Status, string(body))
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
os.Exit(1)
}
}