generated from Schroedinger-Hat/template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (162 loc) · 4.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"bytes"
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
utils "cog/utils"
)
type model struct {
issues []utils.Issue
index int
width int
height int
spinner spinner.Model
progress progress.Model
done bool
}
var csvFilePath string
var githubUser string
var githubRepository string
var GHToken string
var (
currentIssueNameStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("211"))
doneStyle = lipgloss.NewStyle().Margin(1, 2)
checkMark = lipgloss.NewStyle().Foreground(lipgloss.Color("42")).SetString("✓")
)
func newModel() model {
p := progress.New(
progress.WithDefaultGradient(),
progress.WithWidth(40),
progress.WithoutPercentage(),
)
s := spinner.New()
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("63"))
return model{
issues: utils.GetIssue(csvFilePath),
spinner: s,
progress: p,
}
}
func (m model) Init() tea.Cmd {
return tea.Batch(parseAndCreate(m.issues[m.index]), m.spinner.Tick)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width, m.height = msg.Width, msg.Height
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc", "q":
return m, tea.Quit
}
case createdIssueMsg:
if m.index >= len(m.issues)-1 {
m.done = true
return m, tea.Quit
}
progressCmd := m.progress.SetPercent(float64(m.index) / float64(len(m.issues)-1))
m.index++
return m, tea.Batch(
progressCmd,
tea.Printf("%s %s", checkMark, m.issues[m.index]), // print success message above our program
parseAndCreate(m.issues[m.index]), // create the next issue
)
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
case progress.FrameMsg:
newModel, cmd := m.progress.Update(msg)
if newModel, ok := newModel.(progress.Model); ok {
m.progress = newModel
}
return m, cmd
}
return m, nil
}
func (m model) View() string {
n := len(m.issues) - 1
w := lipgloss.Width(fmt.Sprintf("%d", n))
if m.done {
return doneStyle.Render(fmt.Sprintf("Done! Created %d Issue.\n", n))
}
issuesCount := fmt.Sprintf(" %*d/%*d", w, m.index, w, n-1)
spin := m.spinner.View() + " "
prog := m.progress.View()
cellsAvail := max(0, m.width-lipgloss.Width(spin+prog+issuesCount))
issueName := currentIssueNameStyle.Render(m.issues[m.index].Name)
info := lipgloss.NewStyle().MaxWidth(cellsAvail).Render("Creating " + issueName)
cellsRemaining := max(0, m.width-lipgloss.Width(spin+info+prog+issuesCount))
gap := strings.Repeat(" ", cellsRemaining)
return spin + info + gap + prog + issuesCount
}
type createdIssueMsg string
func parseAndCreate(issue utils.Issue) tea.Cmd {
// change with the creating of the issue
url := "https://api.github.com/repos/" + githubUser + "/" + githubRepository + "/issues"
title := "\"title\":\"" + issue.Name + "\""
description := "\"body\":\"" + issue.Description + "\""
labels := "\"labels\":["
for index, label := range issue.Labels {
labels += "\"" + label + "\""
if index < len(issue.Labels)-1 {
labels += ","
}
}
labels += "]"
payload := []byte("{" + title + "," + description + "," + labels + "}")
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
fmt.Println("Error creating request:", err)
os.Exit(0)
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", "Bearer "+GHToken)
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
os.Exit(0)
}
defer resp.Body.Close()
if resp.StatusCode == 201 {
return func() tea.Msg {
return createdIssueMsg(issue.Name + ":" + issue.Description)
}
} else {
return nil
}
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func main() {
envGHTokenName := "GHTOKEN"
envGHTokenValue, exists := os.LookupEnv(envGHTokenName)
if !exists {
fmt.Println("Environment variable " + envGHTokenName + " not set!")
}
GHToken = envGHTokenValue
flag.StringVar(&csvFilePath, "csv", "", "The path of the csv with all the infos about issues to create")
flag.StringVar(&githubUser, "gh-user", "", "The user of the repository where we want to create the issues")
flag.StringVar(&githubRepository, "gh-repository", "", "The repository where we want to create the issues")
flag.Parse()
if csvFilePath == "" || githubRepository == "" || githubUser == "" {
fmt.Println("Error: you must pass all the arguments!")
os.Exit(0)
}
if _, err := tea.NewProgram(newModel()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}