From e27b7cc23391ca73bd80862add3d53ce8f289fdf Mon Sep 17 00:00:00 2001 From: Mandresy RABENJAHARISON Date: Mon, 25 Aug 2025 13:46:37 +0300 Subject: [PATCH] feat: extract odoo ticket number from reference branch --- action.yml | 4 ++-- main.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/action.yml b/action.yml index 98a762b..5da4ca9 100644 --- a/action.yml +++ b/action.yml @@ -1,7 +1,7 @@ name: 'create-odoo-timesheet' description: 'Create an Odoo timesheet via FastAPI when time is added on a Gitea Issue.' -author: 'Mandresy RABENJAHARISON ' +author: 'Mandresy RABENJAHARISON ' branding: icon: clock color: blue @@ -19,7 +19,7 @@ runs: ISSUE_NUMBER: ${{ gitea.event.issue.number }} ISSUE_TITLE: ${{ gitea.event.issue.title }} SENDER_LOGIN: ${{ gitea.event.sender.login }} - # Issue time tracking fields (from the event payload) TRACKED_SECONDS: ${{ gitea.event.tracked_time.time }} TRACKED_CREATED: ${{ gitea.event.tracked_time.created }} EVENT_ACTION: ${{ gitea.event.action }} + BRANCH_REF: ${{ gitea.event.issue.ref }} diff --git a/main.go b/main.go index d193578..6def899 100644 --- a/main.go +++ b/main.go @@ -20,16 +20,54 @@ type TimesheetPayload struct { TaskID int64 `json:"task_id"` } +// parseOdooTicketID extracts the numeric ticket id from a ref formatted like +// "/ticket-". It scans path segments and picks the first segment +// that starts with "ticket-" (case-insensitive), then reads leading digits. +func parseOdooTicketID(ref string) (int64, error) { + if strings.TrimSpace(ref) == "" { + return 0, fmt.Errorf("empty ref") + } + parts := strings.Split(ref, "/") + for _, seg := range parts { + if seg == "" { + continue + } + lower := strings.ToLower(seg) + const pfx = "ticket-" + if strings.HasPrefix(lower, pfx) { + numPart := seg[len(pfx):] + // collect leading digits + var digits strings.Builder + for _, r := range numPart { + if r >= '0' && r <= '9' { + digits.WriteRune(r) + } else { + break + } + } + if digits.Len() == 0 { + return 0, fmt.Errorf("no digits after ticket- in segment: %s", seg) + } + id, err := strconv.ParseInt(digits.String(), 10, 64) + if err != nil { + return 0, err + } + return id, nil + } + } + return 0, fmt.Errorf("no ticket- segment found in ref: %s", ref) +} + func main() { baseURL := os.Getenv("BASE_URL") - issueNumber := os.Getenv("ISSUE_NUMBER") + branchRef := os.Getenv("BRANCH_REF") 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 == "" { + if baseURL == "" || branchRef == "" || sender == "" || trackedSeconds == "" || trackedCreated == "" { fmt.Println("Some required environment variables are missing.") os.Exit(1) } @@ -48,8 +86,12 @@ func main() { defer logFile.Close() logger := log.New(io.MultiWriter(os.Stdout, logFile), "", log.LstdFlags) - var taskID int64 - fmt.Sscanf(issueNumber, "%d", &taskID) + // Extract task ID from branch ref + taskID, err := parseOdooTicketID(branchRef) + if err != nil { + logger.Printf("Failed to parse Odoo ticket from BRANCH_REF '%s': %v\n", branchRef, err) + os.Exit(1) + } seconds, err := strconv.ParseInt(trackedSeconds, 10, 64) if err != nil {