|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
5 | | - "errors" |
6 | | - "fmt" |
7 | | - "io/ioutil" |
8 | 4 | "log" |
9 | | - "net/http" |
10 | 5 | "os" |
11 | | - "strconv" |
12 | | - "strings" |
13 | | - "time" |
14 | 6 | ) |
15 | 7 |
|
16 | | -type AutoCloser struct { |
17 | | - Config Config |
18 | | - Issues []Issue |
19 | | -} |
20 | | - |
21 | | -type Config struct { |
22 | | - Token string |
23 | | - Repository string |
24 | | - ListEndpoint string |
25 | | - Keep int |
26 | | - Label string |
27 | | -} |
28 | | - |
29 | | -type Issue struct { |
30 | | - Title string `json:"title"` |
31 | | - URL string `json:"url"` |
32 | | -} |
33 | | - |
34 | | -func (a *AutoCloser) setConfig() error { |
35 | | - if os.Getenv("GITHUB_TOKEN") == "" { |
36 | | - return errors.New("GITHUB_TOKEN is empty") |
37 | | - } |
38 | | - |
39 | | - if os.Getenv("GITHUB_REPOSITORY") == "" { |
40 | | - return errors.New("GITHUB_REPOSITORY is empty") |
41 | | - } |
42 | | - |
43 | | - if os.Getenv("AC_LABEL") == "" { |
44 | | - return errors.New("AC_LABEL is empty") |
45 | | - } |
46 | | - |
47 | | - if os.Getenv("AC_KEEP") == "" { |
48 | | - a.Config.Keep = 1 |
49 | | - } else { |
50 | | - keep, err := strconv.Atoi(os.Getenv("AC_KEEP")) |
51 | | - if err != nil { |
52 | | - return err |
53 | | - } |
54 | | - a.Config.Keep = keep |
55 | | - } |
56 | | - |
57 | | - a.Config.Token = os.Getenv("GITHUB_TOKEN") |
58 | | - a.Config.Repository = os.Getenv("GITHUB_REPOSITORY") |
59 | | - a.Config.Label = os.Getenv("AC_LABEL") |
60 | | - a.Config.ListEndpoint = "https://api.github.com/repos/" + a.Config.Repository + "/issues?labels=" + a.Config.Label |
61 | | - |
62 | | - return nil |
63 | | -} |
64 | | - |
65 | | -func (a *AutoCloser) getIssuesList() error { |
66 | | - client := &http.Client{} |
67 | | - req, err := http.NewRequest(http.MethodGet, a.Config.ListEndpoint, nil) |
68 | | - if err != nil { |
69 | | - return err |
70 | | - } |
71 | | - |
72 | | - req.Header.Add("Accept", "application/vnd.github.v3+json") |
73 | | - req.Header.Add("Authorization", "token "+a.Config.Token) |
74 | | - |
75 | | - resp, err := client.Do(req) |
76 | | - if err != nil { |
77 | | - return err |
78 | | - } |
79 | | - |
80 | | - if resp.StatusCode != 200 { |
81 | | - return errors.New("Error getting a list of isues: " + resp.Status) |
82 | | - } |
83 | | - |
84 | | - defer resp.Body.Close() |
85 | | - |
86 | | - b, err := ioutil.ReadAll(resp.Body) |
87 | | - if err != nil { |
88 | | - return err |
89 | | - } |
90 | | - |
91 | | - err = json.Unmarshal(b, &a.Issues) |
92 | | - if err != nil { |
93 | | - return err |
94 | | - } |
95 | | - |
96 | | - return nil |
97 | | -} |
98 | | - |
99 | | -func (a *AutoCloser) closeIssues() error { |
100 | | - oldIssues := a.Issues[a.Config.Keep:len(a.Issues)] |
101 | | - |
102 | | - for i := range oldIssues { |
103 | | - patchData := `{"state":"closed"}` |
104 | | - |
105 | | - client := &http.Client{} |
106 | | - req, err := http.NewRequest(http.MethodPatch, oldIssues[i].URL, strings.NewReader(patchData)) |
107 | | - if err != nil { |
108 | | - return err |
109 | | - } |
110 | | - |
111 | | - req.Header.Add("Accept", "application/vnd.github.v3+json") |
112 | | - req.Header.Add("Authorization", "token "+a.Config.Token) |
113 | | - |
114 | | - resp, err := client.Do(req) |
115 | | - if err != nil { |
116 | | - return err |
117 | | - } |
118 | | - |
119 | | - if resp.StatusCode != 200 { |
120 | | - return errors.New("Error posting a comment: " + resp.Status) |
| 8 | +func main() { |
| 9 | + // Required |
| 10 | + required := []string{"GITHUB_TOKEN", "GITHUB_REPOSITORY", "AC_LABEL"} |
| 11 | + for _, v := range required { |
| 12 | + if os.Getenv(v) == "" { |
| 13 | + log.Fatal(v + " is empty.") |
121 | 14 | } |
122 | | - |
123 | | - fmt.Println("Closed issue:\n" + oldIssues[i].Title) |
124 | | - |
125 | | - time.Sleep(1 * time.Second) |
126 | 15 | } |
127 | 16 |
|
128 | | - return nil |
129 | | -} |
130 | | - |
131 | | -func main() { |
132 | | - a := AutoCloser{} |
133 | | - |
134 | | - err := a.setConfig() |
| 17 | + a, err := newAutoCloser() |
135 | 18 | if err != nil { |
136 | 19 | log.Fatal(err) |
137 | 20 | } |
|
0 commit comments