Skip to content

Commit

Permalink
enhancement: Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Czernek committed Aug 2, 2020
1 parent fc816bb commit 3be7293
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ build-all:
GOOS=linux GOARCH=$(GOARCH) $(GOBUILD) -o bin/$(BINARY_NAME)_linux -v $(GOPKG)
GOOS=darwin GOARCH=$(GOARCH) $(GOBUILD) -o bin/$(BINARY_NAME)_darwin -v $(GOPKG)

.PHONY: clean
clean:
-rm bin/*

.PHONY: all
all: build
27 changes: 18 additions & 9 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,34 @@ var removed int = 0
func main() {
parseCmdFlags()
cfg, err := GetConfig(configPath)
if err != nil {
fmt.Printf("Could not parse configuration yaml: %v\n", err)
os.Exit(1)
}
handleError(err, "[ERR] Could not parse configuration yaml\n", true)

client := getGithubClient(cfg)

var wg sync.WaitGroup

issues, _, _ := client.Issues.ListByRepo(context.Background(), cfg.Repo.Owner, cfg.Repo.Name, nil)
issues, _, err := client.Issues.ListByRepo(context.Background(), cfg.Repo.Owner, cfg.Repo.Name, nil)
handleError(err, "[ERR] Could not connect to GitHub API, check API token?\n", true)

wg.Add(len(issues))

for _, issue := range issues {
go deleteIssueReactions(&wg, client, cfg, *issue.Number)
}
wg.Wait()
fmt.Printf("Successfully removed %d issue reactions from repo %v\n", removed, cfg.Repo)
fmt.Printf("Removed %d issue reactions from repo %v\n", removed, cfg.Repo)
}

func deleteIssueReactions(wg *sync.WaitGroup, client *github.Client, cfg *Config, issueNumber int) {
defer wg.Done()
ctx := context.Background()
reactions, _, _ := client.Reactions.ListIssueReactions(ctx, cfg.Repo.Owner, cfg.Repo.Name, issueNumber, nil)
reactions, _, err := client.Reactions.ListIssueReactions(ctx, cfg.Repo.Owner, cfg.Repo.Name, issueNumber, nil)
handleError(err, fmt.Sprintf("[WARN] Could not list reactions for issue %d", issueNumber), false)
for _, reaction := range reactions {
if *reaction.User.Login == cfg.Auth.Login {
_, err := client.Reactions.DeleteIssueReaction(ctx, cfg.Repo.Owner, cfg.Repo.Name, issueNumber, *reaction.ID)
if err != nil {
fmt.Println("[ERR] Could not delete reaction ", reaction)
fmt.Println(err)
fmt.Fprintf(os.Stderr, "[WARN] Could not delete reaction %v\nerror:\n%v", reaction, err)
} else {
removed++
}
Expand All @@ -72,3 +71,13 @@ func getGithubClient(cfg *Config) *github.Client {
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc)
}

func handleError(err error, msg string, fatal bool) {
if err != nil {
fmt.Fprintf(os.Stderr, msg)
fmt.Fprintf(os.Stderr, "%v\n", err)
if fatal {
os.Exit(1)
}
}
}

0 comments on commit 3be7293

Please sign in to comment.