|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/fbiville/markdown-table-formatter/pkg/markdown" |
| 7 | + "github.com/google/go-github/v50/github" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const awesomeGnoEmbedURL = "**[gnolang/awesome-Gno](https://github.com/gnolang/awesome-gno)**" |
| 13 | +const gnoEmbedURL = "**[gnolang/gno](https://github.com/gnolang/gno)**" |
| 14 | + |
| 15 | +func createOutputFile() (*os.File, error) { |
| 16 | + if _, err := os.Stat(opts.outputPath); os.IsNotExist(err) { |
| 17 | + err = os.MkdirAll(opts.outputPath, os.ModePerm) |
| 18 | + if err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + } |
| 22 | + outputFile, err := os.Create(opts.outputPath + "report.md") |
| 23 | + if err != nil { |
| 24 | + return nil, err |
| 25 | + } |
| 26 | + return outputFile, nil |
| 27 | +} |
| 28 | + |
| 29 | +// TODO: Refacto table creation ~Don't repeat yourself |
| 30 | +func writeChangelog(issues []*github.Issue, pullRequests []*github.PullRequest, commits []*github.RepositoryCommit, outputFile *os.File) error { |
| 31 | + var issuesTable [][]string |
| 32 | + for _, issue := range issues { |
| 33 | + issuesTable = append(issuesTable, []string{issue.GetTitle(), issue.GetURL(), issue.GetUser().GetLogin()}) |
| 34 | + } |
| 35 | + |
| 36 | + markdownTable, err := markdown.NewTableFormatterBuilder(). |
| 37 | + Build("Title", "Link to Body", "Assignee"). |
| 38 | + Format(issuesTable) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + result := fmt.Sprintf("# Changelog ⚙️\n\nThere is **%d new closed issues** in %s since %s\n\n%s", len(issues), gnoEmbedURL, opts.since, markdownTable) |
| 43 | + |
| 44 | + var pullRequestRows [][]string |
| 45 | + for _, pr := range pullRequests { |
| 46 | + pullRequestRows = append(pullRequestRows, []string{pr.GetTitle(), pr.GetURL(), pr.GetUser().GetLogin()}) |
| 47 | + } |
| 48 | + pullRequestsTable, err := markdown.NewTableFormatterBuilder(). |
| 49 | + Build("Title", "Link to PR", "Author"). |
| 50 | + Format(pullRequestRows) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + result += fmt.Sprintf("\n\n## New PR\nThere is **%d new closed PR** in %s since %s\n\n%s", len(pullRequests), gnoEmbedURL, opts.since, pullRequestsTable) |
| 55 | + |
| 56 | + result += fmt.Sprintf("\n\n## New commits\nThere is **%d new commits** in %s since %s\n\n", len(commits), gnoEmbedURL, opts.since) |
| 57 | + |
| 58 | + _, err = outputFile.WriteString(result) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +// TODO: Refacto table creation ~Don't repeat yourself |
| 66 | +func writeBacklog(issues []*github.Issue, pullRequests []*github.PullRequest, outputFile *os.File) error { |
| 67 | + var issuesTable [][]string |
| 68 | + for _, issue := range issues { |
| 69 | + issuesTable = append(issuesTable, []string{issue.GetTitle(), issue.GetURL(), issue.GetUser().GetLogin()}) |
| 70 | + } |
| 71 | + |
| 72 | + markdownTable, err := markdown.NewTableFormatterBuilder(). |
| 73 | + Build("Title", "Link to Body", "Assignee"). |
| 74 | + Format(issuesTable) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + result := fmt.Sprintf("# Backlog 💡\n\nThere is **%d new open issues** in %s since %s\n\n%s", len(issues), gnoEmbedURL, opts.since, markdownTable) |
| 79 | + |
| 80 | + var pullRequestRows [][]string |
| 81 | + for _, pr := range pullRequests { |
| 82 | + pullRequestRows = append(pullRequestRows, []string{pr.GetTitle(), pr.GetURL(), pr.GetUser().GetLogin()}) |
| 83 | + } |
| 84 | + pullRequestsTable, err := markdown.NewTableFormatterBuilder(). |
| 85 | + Build("Title", "Link to PR", "Author"). |
| 86 | + Format(pullRequestRows) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + result += fmt.Sprintf("\n\n## New PR\nThere is **%d new open PR** in %s since %s\n\n%s", len(pullRequests), gnoEmbedURL, opts.since, pullRequestsTable) |
| 91 | + |
| 92 | + _, err = outputFile.WriteString(result) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// TODO: Refacto table creation ~Don't repeat yourself |
| 100 | +func writeCuration(issues []*github.Issue, pullRequests []*github.PullRequest, commits []*github.RepositoryCommit, outputFile *os.File) error { |
| 101 | + // Format at Markdown format |
| 102 | + var issuesRows [][]string |
| 103 | + for _, issue := range issues { |
| 104 | + issuesRows = append(issuesRows, []string{issue.GetTitle(), issue.GetURL(), issue.GetUser().GetLogin()}) |
| 105 | + } |
| 106 | + issuesTable, err := markdown.NewTableFormatterBuilder(). |
| 107 | + Build("Title", "Link to issue", "Author"). |
| 108 | + Format(issuesRows) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + result := fmt.Sprintf("# Curation 📚\n\n## New issues\nThere is **%d updated issues** in %s since %s\n\n%s", len(issues), awesomeGnoEmbedURL, opts.since, issuesTable) |
| 113 | + |
| 114 | + var pullRequestRows [][]string |
| 115 | + for _, pr := range pullRequests { |
| 116 | + pullRequestRows = append(pullRequestRows, []string{pr.GetTitle(), pr.GetURL(), pr.GetUser().GetLogin()}) |
| 117 | + } |
| 118 | + pullRequestsTable, err := markdown.NewTableFormatterBuilder(). |
| 119 | + Build("Title", "Link to PR", "Author"). |
| 120 | + Format(pullRequestRows) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + result += fmt.Sprintf("\n\n## New PR\nThere is **%d updated PR** in %s since %s\n\n%s", len(pullRequests), awesomeGnoEmbedURL, opts.since, pullRequestsTable) |
| 125 | + |
| 126 | + result += fmt.Sprintf("\n\n## New commits\nThere is **%d new commits** in %s since %s\n\n", len(commits), awesomeGnoEmbedURL, opts.since) |
| 127 | + |
| 128 | + _, err = outputFile.WriteString(result) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +// TODO: Refacto table creation ~Don't repeat yourself |
| 136 | +func writeTips(data string, outputFile *os.File) error { |
| 137 | + // Format at Markdown format |
| 138 | + var rows [][]string |
| 139 | + var tweets TweetSearch |
| 140 | + authors := make(map[string]string) |
| 141 | + |
| 142 | + _ = json.Unmarshal([]byte(data), &tweets) |
| 143 | + for _, user := range tweets.Includes.Users { |
| 144 | + authors[user.Id] = user.Username |
| 145 | + } |
| 146 | + for _, tweet := range tweets.Data { |
| 147 | + rows = append(rows, []string{authors[tweet.AuthorId], strings.Replace(tweet.Text, "\n", " ", -1), tweet.CreatedAt}) |
| 148 | + } |
| 149 | + |
| 150 | + //Maybe build our own table formatter |
| 151 | + table, err := markdown.NewTableFormatterBuilder(). |
| 152 | + Build("Author", "Text", "Created at"). |
| 153 | + Format(rows) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + result := fmt.Sprintf("# Tips 🐦\n\n## New Tweets with #gnotips\nThere is **%d new tweet** about gnotips since %s\n\n%s", tweets.Meta.ResultCount, opts.since, table) |
| 158 | + |
| 159 | + _, err = outputFile.WriteString(result) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + return nil |
| 164 | +} |
0 commit comments