forked from posener/learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
190 lines (161 loc) · 4.23 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
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"github.com/google/go-github/v31/github"
"github.com/posener/goaction"
"github.com/posener/goaction/actionutil"
"github.com/posener/goaction/log"
)
//goaction:required
//goaction:description A token for Github APIs.
var token = os.Getenv("GITHUB_TOKEN")
const path = "questions.json"
func main() {
ctx := context.Background()
if !goaction.CI {
log.Debugf("Not in Github action mode, quiting.")
return
}
if goaction.Event != goaction.EventIssues {
log.Debugf("Not an issue action, nothing to do here.")
return
}
issue, err := goaction.GetIssues()
if err != nil {
log.Errorf("Failed getting issue information: %s", err)
os.Exit(1)
}
// Create a Github Client using the token provided through environment.
if token == "" {
log.Errorf("Token was not provided, please define the Github action 'with' 'github-token' as '${{ secrets.GITHUB_TOKEN }}'")
}
gh := actionutil.NewClientWithToken(ctx, token)
fail := func(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
log.Errorf(msg)
gh.IssuesCreateComment(ctx, issue.GetIssue().GetNumber(), &github.IssueComment{
Body: github.String(msg),
})
os.Exit(1)
}
// Interact with the create issue according to the triggering action:
if action := issue.GetAction(); action != "labeled" {
log.Debugf("Ignoring issue action: %q", action)
return
}
if label := issue.GetLabel().GetName(); label != "approved" {
log.Debugf("Ignoring label %s", label)
return
}
newQ, err := parseBody(issue.Issue.GetBody())
if err != nil {
fail("Failed pare question body: %s", err)
}
file, err := os.OpenFile(path, os.O_RDWR, 0666)
if err != nil {
fail("Failed open %q: %s", path, err)
}
defer file.Close()
var questions []*Question
err = json.NewDecoder(file).Decode(&questions)
if err != nil {
fail("Failed decode %q: %s", path, err)
}
questions = append(questions, newQ)
// Write the new questions
_, err = file.Seek(0, 0)
if err != nil {
fail("Failed file seek: %s", err)
}
err = json.NewEncoder(file).Encode(questions)
if err != nil {
fail("Failed encoding questions: %s", err)
}
err = actionutil.GitCommitPush([]string{path}, fmt.Sprintf("Update question from issue #%d", *issue.Issue.ID))
if err != nil {
fail("Failed encoding questions: %s", err)
}
// Close issue
}
type Question struct {
Question string `json:"quesion"`
Options []string `json:"options"`
Answer int `json:"answer"`
Explain string `json:"explain"`
}
type state string
const (
stateNone state = "none"
stateReadQuestion state = "question"
stateReadAnswer state = "answer"
stateReadOption state = "option"
stateReadExplain state = "explain"
)
func parseBody(body string) (*Question, error) {
var q Question
state := stateNone
currentValue := ""
s := bufio.NewScanner(strings.NewReader(body))
scan:
for s.Scan() {
if s.Err() != nil {
break
}
line := s.Text()
line = strings.TrimSpace(line)
switch {
case line == "":
continue scan
case strings.HasPrefix(line, "### "):
// Handle last instruction
err := q.set(state, currentValue)
currentValue = ""
if err != nil {
return nil, fmt.Errorf("failed set state %q to %q", state, currentValue)
}
switch state {
case stateReadQuestion:
}
instruction := strings.TrimPrefix(line, "### ")
switch {
case instruction == "question":
state = stateReadQuestion
case strings.HasPrefix(instruction, "option"):
state = stateReadOption
case instruction == "answer":
state = stateReadAnswer
case instruction == "explain":
state = stateReadExplain
default:
return nil, fmt.Errorf("unknown instruction %q", instruction)
}
default:
currentValue += line
}
}
err := q.set(state, currentValue) // Set the last state.
if err != nil {
return nil, fmt.Errorf("failed set state %q to %q", state, currentValue)
}
return &q, nil
}
func (q *Question) set(state state, value string) error {
var err error
switch state {
case stateReadQuestion:
q.Question = value
case stateReadOption:
q.Options = append(q.Options, value)
case stateReadAnswer:
q.Answer, err = strconv.Atoi(value)
case stateReadExplain:
q.Explain = value
}
return err
}