Skip to content

Commit

Permalink
Merge pull request #1 from lowply/refactoring
Browse files Browse the repository at this point in the history
Refactored code
  • Loading branch information
lowply authored May 30, 2019
2 parents 2e3c706 + ee42e68 commit 0ad78d3
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 124 deletions.
113 changes: 113 additions & 0 deletions src/auto_closer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
)

type autoCloser struct {
token string
repository string
endpoint string
keep int
label string
issues []issue
}

func newAutoCloser() (*autoCloser, error) {
a := &autoCloser{}

if os.Getenv("AC_KEEP") == "" {
a.keep = 1
} else {
keep, err := strconv.Atoi(os.Getenv("AC_KEEP"))
if err != nil {
return nil, err
}
a.keep = keep
}

a.token = os.Getenv("GITHUB_TOKEN")
a.repository = os.Getenv("GITHUB_REPOSITORY")
a.label = os.Getenv("AC_LABEL")
a.endpoint = "https://api.github.com/repos/" + a.repository + "/issues?labels=" + a.label
return a, nil
}

func (a *autoCloser) getIssuesList() error {
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, a.endpoint, nil)
if err != nil {
return err
}

req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("Authorization", "token "+a.token)

resp, err := client.Do(req)
if err != nil {
return err
}

if resp.StatusCode != 200 {
return errors.New("Error getting a list of isues: " + resp.Status)
}

defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

err = json.Unmarshal(b, &a.issues)
if err != nil {
return err
}

return nil
}

func (a *autoCloser) closeIssues() error {
if len(a.issues) == 0 {
fmt.Println("No issues found with the label: " + a.label)
return nil
}

oldIssues := a.issues[a.keep:len(a.issues)]

for i := range oldIssues {
patchData := `{"state":"closed"}`

client := &http.Client{}
req, err := http.NewRequest(http.MethodPatch, oldIssues[i].URL, strings.NewReader(patchData))
if err != nil {
return err
}

req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("Authorization", "token "+a.token)

resp, err := client.Do(req)
if err != nil {
return err
}

if resp.StatusCode != 200 {
return errors.New("Error posting a comment: " + resp.Status)
}

fmt.Println("Closed issue:\n" + oldIssues[i].Title)

time.Sleep(1 * time.Second)
}

return nil
}
6 changes: 6 additions & 0 deletions src/issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

type issue struct {
Title string `json:"title"`
URL string `json:"url"`
}
131 changes: 7 additions & 124 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -1,137 +1,20 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)

type AutoCloser struct {
Config Config
Issues []Issue
}

type Config struct {
Token string
Repository string
ListEndpoint string
Keep int
Label string
}

type Issue struct {
Title string `json:"title"`
URL string `json:"url"`
}

func (a *AutoCloser) setConfig() error {
if os.Getenv("GITHUB_TOKEN") == "" {
return errors.New("GITHUB_TOKEN is empty")
}

if os.Getenv("GITHUB_REPOSITORY") == "" {
return errors.New("GITHUB_REPOSITORY is empty")
}

if os.Getenv("AC_LABEL") == "" {
return errors.New("AC_LABEL is empty")
}

if os.Getenv("AC_KEEP") == "" {
a.Config.Keep = 1
} else {
keep, err := strconv.Atoi(os.Getenv("AC_KEEP"))
if err != nil {
return err
}
a.Config.Keep = keep
}

a.Config.Token = os.Getenv("GITHUB_TOKEN")
a.Config.Repository = os.Getenv("GITHUB_REPOSITORY")
a.Config.Label = os.Getenv("AC_LABEL")
a.Config.ListEndpoint = "https://api.github.com/repos/" + a.Config.Repository + "/issues?labels=" + a.Config.Label

return nil
}

func (a *AutoCloser) getIssuesList() error {
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, a.Config.ListEndpoint, nil)
if err != nil {
return err
}

req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("Authorization", "token "+a.Config.Token)

resp, err := client.Do(req)
if err != nil {
return err
}

if resp.StatusCode != 200 {
return errors.New("Error getting a list of isues: " + resp.Status)
}

defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

err = json.Unmarshal(b, &a.Issues)
if err != nil {
return err
}

return nil
}

func (a *AutoCloser) closeIssues() error {
oldIssues := a.Issues[a.Config.Keep:len(a.Issues)]

for i := range oldIssues {
patchData := `{"state":"closed"}`

client := &http.Client{}
req, err := http.NewRequest(http.MethodPatch, oldIssues[i].URL, strings.NewReader(patchData))
if err != nil {
return err
}

req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("Authorization", "token "+a.Config.Token)

resp, err := client.Do(req)
if err != nil {
return err
}

if resp.StatusCode != 200 {
return errors.New("Error posting a comment: " + resp.Status)
func main() {
// Required
required := []string{"GITHUB_TOKEN", "GITHUB_REPOSITORY", "AC_LABEL"}
for _, v := range required {
if os.Getenv(v) == "" {
log.Fatal(v + " is empty.")
}

fmt.Println("Closed issue:\n" + oldIssues[i].Title)

time.Sleep(1 * time.Second)
}

return nil
}

func main() {
a := AutoCloser{}

err := a.setConfig()
a, err := newAutoCloser()
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 0ad78d3

Please sign in to comment.