Upgraded the patch-odoo-ticket action to version v1.3 and added support for an authentication token to secure communication with the FastAPI server. Modified the workflow, main logic, and action definition to handle the auth_token input and ensure proper authorization. Includes error handling for missing or invalid tokens.
113 lines
2.8 KiB
Go
113 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type Payload struct {
|
|
TaskID int64 `json:"task_id"`
|
|
State string `json:"state"`
|
|
UserIDs []string `json:"user_ids"`
|
|
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
|
|
issueState := os.Getenv("ISSUE_STATE")
|
|
issueAssignees := splitCSV(os.Getenv("ISSUE_ASSIGNEES"))
|
|
issueDueDate := os.Getenv("ISSUE_DUE_DATE")
|
|
branchRef := os.Getenv("BRANCH_REF")
|
|
authToken := os.Getenv("AUTH_TOKEN")
|
|
if strings.TrimSpace(authToken) == "" {
|
|
logger.Println("AUTH_TOKEN not provided. Please pass 'auth_token' input mapped to a secret.")
|
|
os.Exit(1)
|
|
}
|
|
|
|
var odooTicketNumber int64
|
|
if branchRef != "" {
|
|
// Expect format: <prefix>/ticket-OdooTicketNumber
|
|
parts := strings.Split(branchRef, "/")
|
|
for _, part := range parts {
|
|
if strings.HasPrefix(part, "ticket-") {
|
|
ticketStr := strings.TrimPrefix(part, "ticket-")
|
|
fmt.Sscanf(ticketStr, "%d", &odooTicketNumber)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
payload := Payload{
|
|
TaskID: odooTicketNumber,
|
|
State: issueState,
|
|
UserIDs: issueAssignees,
|
|
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"
|
|
req, err := http.NewRequest(http.MethodPatch, endpoint, bytes.NewBuffer(payloadBytes))
|
|
if err != nil {
|
|
logger.Printf("Error creating HTTP request: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+authToken)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
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 == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
|
|
logger.Println("Authentication to FastAPI failed. Check FASTAPI token.")
|
|
}
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
os.Exit(1)
|
|
}
|
|
}
|