package main import ( "bytes" "context" "encoding/csv" "encoding/json" "errors" "fmt" "io" "net/http" "net/url" "os" "os/signal" "path" "runtime" "strconv" "syscall" "time" "gitea.urkob.com/mcr-swiss/gogstea/internal/domain" "gitea.urkob.com/mcr-swiss/gogstea/kit/config" ) func main() { ctx, cancel := context.WithCancel(signalContext(context.Background())) defer cancel() cfgFile := os.Getenv("CONFIG_FILE") if cfgFile == "" { // Get root path _, filename, _, _ := runtime.Caller(0) cfgFile = path.Join(path.Dir(filename), "configs", "app.yml") } cfg, err := config.LoadConfig(cfgFile) if err != nil { panic(err) } wd, err := os.Getwd() if err != nil { panic(err) } f1, err := os.Open(cfg.Issues.CSVPath) if err != nil { panic(err) } defer f1.Close() r1 := csv.NewReader(f1) r1.Comma = ',' // Set the delimiter to comma r1.LazyQuotes = true r1.TrimLeadingSpace = false outputFile, err := os.Create(wd + "/issues-output.txt") if err != nil { panic(err) } defer outputFile.Close() for { record, err := r1.Read() if err != nil { if !errors.Is(err, io.EOF) { panic(err) } break // Stop on EOF or other errors. } cli := http.DefaultClient parsedURL, err := url.Parse(fmt.Sprintf("%s/repos/%s/%s/issues", cfg.Gitea.URL, record[1], record[0])) if err != nil { panic(err) } isClosedInt, err := strconv.Atoi(record[7]) if err != nil { panic(err) } var isClosed bool switch isClosedInt { case 0: isClosed = false case 1: isClosed = false default: panic(fmt.Errorf("isclosed is %d", isClosedInt)) } issue := domain.IssueCreateRequest{ Assignee: record[2], Assignees: nil, Body: record[4], Closed: isClosed, DueDate: time.Time{}, Labels: []int{}, Milestone: 0, Ref: "", Title: record[3], } bts, err := json.Marshal(issue) if err != nil { panic(err) } req, err := http.NewRequestWithContext(ctx, http.MethodPost, parsedURL.String(), bytes.NewReader(bts)) if err != nil { panic(err) } req.Header.Add("Authorization", "token "+cfg.Gitea.ApiKey) req.Header.Add("Content-Type", "application/json") resp, err := cli.Do(req) if err != nil { panic(err) } if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { bts, _ := io.ReadAll(resp.Body) if _, err := outputFile.WriteString(fmt.Sprintf("ERROR ISSUE | %d | %s | %s\n", resp.StatusCode, parsedURL.String(), string(bts))); err != nil { panic(err) } continue } bts, err = io.ReadAll(resp.Body) if err != nil { panic(err) } if _, err := outputFile.WriteString(fmt.Sprintf("OK ISSUE | %s \n", string(bts))); err != nil { panic(err) } } } func signalContext(ctx context.Context) context.Context { ctx, cancel := context.WithCancel(ctx) sigs := make(chan os.Signal, 1) signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) go func() { <-sigs signal.Stop(sigs) close(sigs) cancel() }() return ctx }