Skip to content

Commit 0ad78d3

Browse files
authored
Merge pull request #1 from lowply/refactoring
Refactored code
2 parents 2e3c706 + ee42e68 commit 0ad78d3

3 files changed

Lines changed: 126 additions & 124 deletions

File tree

src/auto_closer.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"os"
10+
"strconv"
11+
"strings"
12+
"time"
13+
)
14+
15+
type autoCloser struct {
16+
token string
17+
repository string
18+
endpoint string
19+
keep int
20+
label string
21+
issues []issue
22+
}
23+
24+
func newAutoCloser() (*autoCloser, error) {
25+
a := &autoCloser{}
26+
27+
if os.Getenv("AC_KEEP") == "" {
28+
a.keep = 1
29+
} else {
30+
keep, err := strconv.Atoi(os.Getenv("AC_KEEP"))
31+
if err != nil {
32+
return nil, err
33+
}
34+
a.keep = keep
35+
}
36+
37+
a.token = os.Getenv("GITHUB_TOKEN")
38+
a.repository = os.Getenv("GITHUB_REPOSITORY")
39+
a.label = os.Getenv("AC_LABEL")
40+
a.endpoint = "https://api.github.com/repos/" + a.repository + "/issues?labels=" + a.label
41+
return a, nil
42+
}
43+
44+
func (a *autoCloser) getIssuesList() error {
45+
client := &http.Client{}
46+
req, err := http.NewRequest(http.MethodGet, a.endpoint, nil)
47+
if err != nil {
48+
return err
49+
}
50+
51+
req.Header.Add("Accept", "application/vnd.github.v3+json")
52+
req.Header.Add("Authorization", "token "+a.token)
53+
54+
resp, err := client.Do(req)
55+
if err != nil {
56+
return err
57+
}
58+
59+
if resp.StatusCode != 200 {
60+
return errors.New("Error getting a list of isues: " + resp.Status)
61+
}
62+
63+
defer resp.Body.Close()
64+
65+
b, err := ioutil.ReadAll(resp.Body)
66+
if err != nil {
67+
return err
68+
}
69+
70+
err = json.Unmarshal(b, &a.issues)
71+
if err != nil {
72+
return err
73+
}
74+
75+
return nil
76+
}
77+
78+
func (a *autoCloser) closeIssues() error {
79+
if len(a.issues) == 0 {
80+
fmt.Println("No issues found with the label: " + a.label)
81+
return nil
82+
}
83+
84+
oldIssues := a.issues[a.keep:len(a.issues)]
85+
86+
for i := range oldIssues {
87+
patchData := `{"state":"closed"}`
88+
89+
client := &http.Client{}
90+
req, err := http.NewRequest(http.MethodPatch, oldIssues[i].URL, strings.NewReader(patchData))
91+
if err != nil {
92+
return err
93+
}
94+
95+
req.Header.Add("Accept", "application/vnd.github.v3+json")
96+
req.Header.Add("Authorization", "token "+a.token)
97+
98+
resp, err := client.Do(req)
99+
if err != nil {
100+
return err
101+
}
102+
103+
if resp.StatusCode != 200 {
104+
return errors.New("Error posting a comment: " + resp.Status)
105+
}
106+
107+
fmt.Println("Closed issue:\n" + oldIssues[i].Title)
108+
109+
time.Sleep(1 * time.Second)
110+
}
111+
112+
return nil
113+
}

src/issue.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
type issue struct {
4+
Title string `json:"title"`
5+
URL string `json:"url"`
6+
}

src/main.go

Lines changed: 7 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,20 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"errors"
6-
"fmt"
7-
"io/ioutil"
84
"log"
9-
"net/http"
105
"os"
11-
"strconv"
12-
"strings"
13-
"time"
146
)
157

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.")
12114
}
122-
123-
fmt.Println("Closed issue:\n" + oldIssues[i].Title)
124-
125-
time.Sleep(1 * time.Second)
12615
}
12716

128-
return nil
129-
}
130-
131-
func main() {
132-
a := AutoCloser{}
133-
134-
err := a.setConfig()
17+
a, err := newAutoCloser()
13518
if err != nil {
13619
log.Fatal(err)
13720
}

0 commit comments

Comments
 (0)