Complete process functionality
This commit is contained in:
+71
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package src
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Api api
|
||||
|
||||
type api struct {}
|
||||
|
||||
func (api) CallApi(date string, from_time string, to_time string, item_id string, description string) error {
|
||||
time1, err := time.Parse("15:04", from_time)
|
||||
if err != nil { return fmt.Errorf("error parsing from_time: %s", err) }
|
||||
|
||||
time2, err := time.Parse("15:04", to_time)
|
||||
if err != nil { return fmt.Errorf("error parsing to_time: %s", err) }
|
||||
|
||||
duration := time2.Sub(time1)
|
||||
seconds := int(duration.Seconds())
|
||||
if seconds < 0 { return fmt.Errorf("from_time is later than to_time: %s > %s", from_time, to_time) }
|
||||
|
||||
var url string = base_url + "/rest/api/2/issue/" + item_id + "/worklog"
|
||||
|
||||
data := map[string]string{
|
||||
"comment": description,
|
||||
"started": fmt.Sprintf("%sT%s:00.000+0000", date, from_time), // "2021-01-17T12:34:00.000+0000",
|
||||
"timeSpentSeconds": strconv.Itoa(seconds),
|
||||
}
|
||||
|
||||
json_data, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error marshaling JSON: %s", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(json_data))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating request: %s", err)
|
||||
}
|
||||
|
||||
auth := username + ":" + password
|
||||
authHeader := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
|
||||
req.Header.Set("Authorization", authHeader)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil { return fmt.Errorf("error making request: %s", err) }
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil { return fmt.Errorf("error reading response body: %s", err) }
|
||||
|
||||
if resp.Status != "201 Created" {
|
||||
return fmt.Errorf("invalid Jira request:\n%s", string(body))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,3 +1,9 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package src
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package src
|
||||
|
||||
func assert(err error) {
|
||||
|
||||
+8
-8
@@ -1,3 +1,9 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package src
|
||||
|
||||
import "bufio"
|
||||
@@ -44,12 +50,6 @@ func (file) Parse(path string, job func(line string, line_number int) string, ov
|
||||
err = scanner.Err()
|
||||
assert(err)
|
||||
|
||||
// TODO: move file
|
||||
err = os.Rename(path + ".tmp", path)
|
||||
assert(err)
|
||||
}
|
||||
|
||||
// - [v] while looping, start writing a new file, .tmp, Q: write per line or per chunk? how big are the chunks?
|
||||
// - [v] mark table start with processed mark
|
||||
// - [ ] on table end, call into REST API
|
||||
// - [ ] if true, continue
|
||||
// - [ ] if false, delete .tmp file, panic
|
||||
// - [ ] if no errors, overwrite file with .tmp file
|
||||
|
||||
+18
-16
@@ -1,8 +1,15 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package src
|
||||
|
||||
import "errors"
|
||||
import "fmt"
|
||||
import "regexp"
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Date struct {
|
||||
clock_in string
|
||||
@@ -18,6 +25,7 @@ type Process struct {
|
||||
clock_in *regexp.Regexp
|
||||
task_line *regexp.Regexp
|
||||
clock_out *regexp.Regexp
|
||||
mark_processed *regexp.Regexp
|
||||
dates map[string]Date
|
||||
}
|
||||
|
||||
@@ -27,6 +35,7 @@ func MakeProcess() Process {
|
||||
clock_in: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+IN\s+\|`),
|
||||
task_line: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+[0-9]{2}:[0-9]{2}\s+\|`),
|
||||
clock_out: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+OUT\s+\|`),
|
||||
mark_processed: Util.CompileRegex(`\|\s+X\s+\|`),
|
||||
dates: make(map[string]Date),
|
||||
}
|
||||
}
|
||||
@@ -37,12 +46,15 @@ func (self *Process) Process(line string, line_number int) string {
|
||||
err = self.parseClockIn(line, line_number)
|
||||
} else if self.task_line.MatchString(line) {
|
||||
err = self.parseTask(line, line_number)
|
||||
// This marks the current task line as "processed",
|
||||
// even though we do the API call on the previous task each iteration.
|
||||
// When an error occurs, you should take this into account!
|
||||
line = self.mark_processed.ReplaceAllString(line, "| V |")
|
||||
} else if self.clock_out.MatchString(line) {
|
||||
err = self.parseClockOut(line, line_number)
|
||||
}
|
||||
assert(err)
|
||||
|
||||
// fmt.Println(line)
|
||||
return line
|
||||
}
|
||||
|
||||
@@ -73,7 +85,7 @@ func (self *Process) parseTask(line string, line_number int) error {
|
||||
|
||||
// Call API for the previous task
|
||||
if date.last_time != "" && date.last_item_id != "" && date.last_description != "" {
|
||||
err = self.callApi(data[0], date.last_time, data[1], date.last_item_id, date.last_description)
|
||||
err = Api.CallApi(data[0], date.last_time, data[1], date.last_item_id, date.last_description)
|
||||
}
|
||||
|
||||
if err != nil { return err }
|
||||
@@ -107,17 +119,7 @@ func (self *Process) parseClockOut(line string, line_number int) error {
|
||||
}
|
||||
|
||||
// Call API for last task of the day
|
||||
self.callApi(data[0], date.last_time, date.clock_out, date.last_item_id, date.last_description)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Process) callApi(date string, from_time string, to_time string, item_id string, description string) error {
|
||||
fmt.Println("API |" + date + "|" + from_time + "|" + to_time + "|" + item_id + "|" + description)
|
||||
|
||||
// parse line
|
||||
// call API
|
||||
// error checking
|
||||
Api.CallApi(data[0], date.last_time, date.clock_out, date.last_item_id, date.last_description)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package src
|
||||
|
||||
import (
|
||||
|
||||
Reference in New Issue
Block a user