|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/fbiville/markdown-table-formatter/pkg/markdown" |
| 8 | + "github.com/google/go-github/v50/github" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +func jsonFormat(data string) string { |
| 14 | + var out bytes.Buffer |
| 15 | + err := json.Indent(&out, []byte(data), "", "\t") |
| 16 | + if err != nil { |
| 17 | + return data |
| 18 | + } |
| 19 | + return out.String() |
| 20 | +} |
| 21 | + |
| 22 | +func createOutputDir() error { |
| 23 | + if _, err := os.Stat(opts.outputPath); os.IsNotExist(err) { |
| 24 | + err = os.MkdirAll(opts.outputPath, os.ModePerm) |
| 25 | + if err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + } |
| 29 | + return nil |
| 30 | +} |
| 31 | + |
| 32 | +func writeChangelog(data string) error { |
| 33 | + err := createOutputDir() |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + if opts.format == "json" { |
| 38 | + data = jsonFormat(data) |
| 39 | + } |
| 40 | + err = os.WriteFile(opts.outputPath+"changelog.json", []byte(data), 0644) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func writeBacklog(data string) error { |
| 48 | + err := createOutputDir() |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + if opts.format == "json" { |
| 53 | + data = jsonFormat(data) |
| 54 | + } |
| 55 | + err = os.WriteFile(opts.outputPath+"backlog.json", []byte(data), 0644) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func writeCuration(issues []*github.Issue) error { |
| 63 | + err := createOutputDir() |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + var issuesTable [][]string |
| 69 | + for _, issue := range issues { |
| 70 | + issuesTable = append(issuesTable, []string{fmt.Sprintf("%d", *issue.Number), *issue.Title, *issue.HTMLURL}) |
| 71 | + } |
| 72 | + err = os.WriteFile(opts.outputPath+"curation.json", []byte("data"), 0644) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func writeTips(data string) error { |
| 80 | + err := createOutputDir() |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + // Format at Markdown format |
| 86 | + var table [][]string |
| 87 | + var tweets TweetSearch |
| 88 | + authors := make(map[string]string) |
| 89 | + |
| 90 | + _ = json.Unmarshal([]byte(data), &tweets) |
| 91 | + for _, user := range tweets.Includes.Users { |
| 92 | + authors[user.Id] = user.Username |
| 93 | + } |
| 94 | + for _, tweet := range tweets.Data { |
| 95 | + table = append(table, []string{authors[tweet.AuthorId], strings.Replace(tweet.Text, "\n", "", -1), tweet.CreatedAt}) |
| 96 | + } |
| 97 | + |
| 98 | + //Maybe build our own table formatter |
| 99 | + markdownTable, err := markdown.NewTableFormatterBuilder(). |
| 100 | + Build("Author", "Text", "Created at"). |
| 101 | + Format(table) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + result := fmt.Sprintf("# Tips\n\nThere is **%d new tweet** about gno since %s\n\n%s", tweets.Meta.ResultCount, opts.since, markdownTable) |
| 106 | + |
| 107 | + err = os.WriteFile(opts.outputPath+"report.md", []byte(result), 0644) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
0 commit comments