commit e513313b7c7ad7a6ee5877b600c238a7dcd4f3d0 Author: Mandresy RABENJAHARISON Date: Tue Sep 16 14:56:47 2025 +0300 feat: release v1 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7ed3891 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +######################## +# — Build stage — +######################## +FROM golang:1.23.4-alpine AS builder + +RUN apk add --no-cache git ca-certificates + +WORKDIR /src + +COPY go.mod go.sum ./ +RUN go mod download || true + +COPY . . + +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/action ./main.go + +######################## +# — Runtime stage — +######################## +FROM alpine:3.20 +RUN apk add --no-cache ca-certificates +ENV PATH="/usr/bin:/usr/sbin:/bin:/sbin" +COPY --from=builder /out/action /usr/local/bin/action +ENTRYPOINT ["/usr/local/bin/action"] diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..192e8c2 --- /dev/null +++ b/action.yml @@ -0,0 +1,25 @@ +name: 'weekly-odoo-timesheets' +description: 'List weekly Gitea issue tracked times (Mon–Sat) and create Odoo timesheets via FastAPI.' + +author: 'Mandresy RABENJAHARISON ' +branding: + icon: clock + color: blue + +inputs: + fastapi-base-url: + description: 'Base URL of the FastAPI server.' + required: true + gitea-base-url: + description: 'Base URL of the Gitea API (optional; defaults to https://gitea.ethumada.com).' + required: false + +runs: + using: 'docker' + image: 'Dockerfile' + env: + FASTAPI_BASE_URL: ${{ inputs.fastapi-base-url }} + GITEA_BASE_URL: https://gitea.ethumada.com + REPO_OWNER: ${{ gitea.event.repository.owner.login }} + REPO_NAME: ${{ gitea.event.repository.name }} + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f6d7c10 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module weekly-odoo-timesheets + +go 1.23.4 + +require code.gitea.io/sdk/gitea v0.22.0 + +require ( + github.com/42wim/httpsig v1.2.3 // indirect + github.com/davidmz/go-pageant v1.0.2 // indirect + github.com/go-fed/httpsig v1.1.0 // indirect + github.com/hashicorp/go-version v1.7.0 // indirect + golang.org/x/crypto v0.39.0 // indirect + golang.org/x/sys v0.33.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..65e8949 --- /dev/null +++ b/go.sum @@ -0,0 +1,34 @@ +code.gitea.io/sdk/gitea v0.22.0 h1:HCKq7bX/HQ85Nw7c/HAhWgRye+vBp5nQOE8Md1+9Ef0= +code.gitea.io/sdk/gitea v0.22.0/go.mod h1:yyF5+GhljqvA30sRDreoyHILruNiy4ASufugzYg0VHM= +github.com/42wim/httpsig v1.2.3 h1:xb0YyWhkYj57SPtfSttIobJUPJZB9as1nsfo7KWVcEs= +github.com/42wim/httpsig v1.2.3/go.mod h1:nZq9OlYKDrUBhptd77IHx4/sZZD+IxTBADvAPI9G/EM= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davidmz/go-pageant v1.0.2 h1:bPblRCh5jGU+Uptpz6LgMZGD5hJoOt7otgT454WvHn0= +github.com/davidmz/go-pageant v1.0.2/go.mod h1:P2EDDnMqIwG5Rrp05dTRITj9z2zpGcD9efWSkTNKLIE= +github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI= +github.com/go-fed/httpsig v1.1.0/go.mod h1:RCMrTZvN1bJYtofsG4rd5NaO5obxQ5xBkdiS7xsT7bM= +github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= +github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= +golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM= +golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..635cfbf --- /dev/null +++ b/main.go @@ -0,0 +1,184 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "os" + "strconv" + "strings" + "time" + + "code.gitea.io/sdk/gitea" +) + +type TimesheetItem struct { + Date string `json:"date"` + TaskID int64 `json:"task_id"` + GiteaUsername string `json:"gitea_username"` + Description string `json:"description"` + HourSpent float64 `json:"hour_spent"` +} + +// parseOdooTicketID extracts numeric id from a ref segment like "ticket-" +func parseOdooTicketID(ref string) (int64, error) { + ref = strings.TrimSpace(ref) + if 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):] + 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") +} + +// weekRangeMonSat returns [since, before) covering Monday 00:00 to Sunday 00:00 for the current week +func weekRangeMonSat(now time.Time) (time.Time, time.Time) { + loc := now.Location() + t := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc) + // 0=Sunday..6=Saturday + weekday := int(t.Weekday()) + daysSinceMonday := (weekday + 6) % 7 + // Monday=0, Sunday=6 + monday := t.AddDate(0, 0, -daysSinceMonday) + // Sunday 00:00 (Mon + 6 days) + before := monday.AddDate(0, 0, 6) + return monday, before +} + +func main() { + // 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) + + giteaBaseUrl := os.Getenv("GITEA_BASE_URL") + fastApiBaseUrl := os.Getenv("FASTAPI_BASE_URL") + token := os.Getenv("GITEA_TOKEN") + owner := os.Getenv("REPO_OWNER") + repo := os.Getenv("REPO_NAME") + + if strings.TrimSpace(giteaBaseUrl) == "" || owner == "" || repo == "" || token == "" { + logger.Println("Missing required env vars: GITEA_BASE_URL, REPO_OWNER, REPO_NAME, or GITEA_TOKEN") + os.Exit(1) + } + + // Gitea client + giteaAPIURL := os.Getenv("GITEA_API_BASE_URL") + if strings.TrimSpace(giteaAPIURL) == "" { + giteaAPIURL = "https://gitea.ethumada.com" + } + client, err := gitea.NewClient(giteaAPIURL, gitea.SetToken(token)) + if err != nil { + logger.Printf("Failed to create Gitea client: %v\n", err) + os.Exit(1) + } + + // Compute current week's Monday..Saturday window + since, before := weekRangeMonSat(time.Now()) + logger.Printf("Fetching tracked times from %s to %s (UTC+3)\n", since.Format(time.RFC3339), before.Format(time.RFC3339)) + + // Fetch all tracked times for the repository + opt := gitea.ListTrackedTimesOptions{ + ListOptions: gitea.ListOptions{Page: -1, PageSize: 1000}, + Since: since, + Before: before, + } + tracked, _, err := client.ListRepoTrackedTimes(owner, repo, opt) + if err != nil { + logger.Printf("ListRepoTrackedTimes failed: %v\n", err) + os.Exit(1) + } + + items := make([]TimesheetItem, 0, len(tracked)) + for _, tt := range tracked { + if tt.Issue == nil { + logger.Printf("Skipping tracked time %d: no issue attached\n", tt.ID) + continue + } + + var taskID int64 + if tt.Issue.Ref != "" { + if id, err := parseOdooTicketID(tt.Issue.Ref); err == nil { + taskID = id + } else { + // Fallback to issue number if ref not properly formatted + taskID = tt.Issue.Index + } + } else { + // Fallback to issue number if ref is empty + taskID = tt.Issue.Index + } + + dateStr := tt.Created.UTC().Format("2006-01-02") + items = append(items, TimesheetItem{ + Date: dateStr, + TaskID: taskID, + GiteaUsername: tt.UserName, + Description: "/", + HourSpent: float64(tt.Time) / 3600.0, + }) + } + + if len(items) == 0 { + logger.Println("No tracked times found for the specified window. Nothing to send.") + return + } + + body, err := json.Marshal(items) + if err != nil { + logger.Printf("Failed to marshal payload: %v\n", err) + os.Exit(1) + } + + endpoint := strings.TrimRight(fastApiBaseUrl, "/") + "/api/v1/account_analytic_gitea_odoo" + req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewReader(body)) + if err != nil { + logger.Printf("Failed to create 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("HTTP request failed: %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) + } +}