Skip to content

Commit 8a4532a

Browse files
committed
fix: re-factors code, adds unit tests for helpers
1 parent 9e9dc3b commit 8a4532a

16 files changed

+131
-92
lines changed

data/data.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package data
2+
3+
import "github.com/google/go-github/v35/github"
4+
5+
type GithubRepoInfo struct {
6+
OrgName string
7+
RepositoryName string
8+
Stars int
9+
License string
10+
LicenseId string
11+
Issues []*github.Issue
12+
CommitActivity []*github.WeeklyCommitActivity
13+
Contributors []*github.ContributorStats
14+
}

helpers/helpers.go

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,23 @@ package helpers
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"net/http"
78
"regexp"
89
"sync"
910

1011
"github.com/google/go-github/v35/github"
1112
"github.com/schaermu/go-github-judge-bot/config"
13+
"github.com/schaermu/go-github-judge-bot/data"
14+
"github.com/schaermu/go-github-judge-bot/scoring"
15+
"github.com/slack-go/slack"
1216
)
1317

14-
type GithubRepoInfo struct {
15-
OrgName string
16-
RepositoryName string
17-
Stars int
18-
License string
19-
LicenseId string
20-
Issues []*github.Issue
21-
CommitActivity []*github.WeeklyCommitActivity
22-
Contributors []*github.ContributorStats
23-
}
24-
2518
type GithubHelper struct {
2619
Config config.GithubConfig
2720
}
2821

29-
func (gh GithubHelper) GetRepositoryData(repoUrl string) (info GithubRepoInfo, err error) {
22+
func (gh GithubHelper) GetRepositoryData(repoUrl string) (info data.GithubRepoInfo, err error) {
3023
tp := http.DefaultClient
3124
if gh.Config.Username != "" && gh.Config.PersonalAccessToken != "" {
3225
basicAuth := github.BasicAuthTransport{
@@ -49,7 +42,7 @@ func (gh GithubHelper) GetRepositoryData(repoUrl string) (info GithubRepoInfo, e
4942
return
5043
}
5144

52-
info = GithubRepoInfo{
45+
info = data.GithubRepoInfo{
5346
OrgName: org,
5447
RepositoryName: repo,
5548
Stars: repoInfo.GetStargazersCount(),
@@ -70,19 +63,19 @@ func (gh GithubHelper) GetRepositoryData(repoUrl string) (info GithubRepoInfo, e
7063
return
7164
}
7265

73-
func getContributorStats(info *GithubRepoInfo, client *github.Client, waitgroup *sync.WaitGroup) {
66+
func getContributorStats(info *data.GithubRepoInfo, client *github.Client, waitgroup *sync.WaitGroup) {
7467
contributors, _, _ := client.Repositories.ListContributorsStats(context.Background(), info.OrgName, info.RepositoryName)
7568
info.Contributors = contributors
7669
waitgroup.Done()
7770
}
7871

79-
func getCommitActivity(info *GithubRepoInfo, client *github.Client, waitgroup *sync.WaitGroup) {
72+
func getCommitActivity(info *data.GithubRepoInfo, client *github.Client, waitgroup *sync.WaitGroup) {
8073
commitActivity, _, _ := client.Repositories.ListCommitActivity(context.Background(), info.OrgName, info.RepositoryName)
8174
info.CommitActivity = commitActivity
8275
waitgroup.Done()
8376
}
8477

85-
func getRepoIssues(info *GithubRepoInfo, client *github.Client, waitgroup *sync.WaitGroup) {
78+
func getRepoIssues(info *data.GithubRepoInfo, client *github.Client, waitgroup *sync.WaitGroup) {
8679
issues, _, _ := client.Issues.ListByRepo(context.Background(), info.OrgName, info.RepositoryName, &github.IssueListByRepoOptions{
8780
Sort: "updated",
8881
State: "all",
@@ -99,3 +92,44 @@ func ExtractInfoFromUrl(repoUrl string) (org string, repo string, err error) {
9992
}
10093
return matches[1], matches[2], nil
10194
}
95+
96+
func GetSlackMessageColorAndIcon(score float64, maxScore float64) (color string, icon string) {
97+
if maxScore/100*score < .4 {
98+
return "danger", ":exclamation:"
99+
}
100+
if maxScore/100*score < .8 {
101+
return "warning", ":warning:"
102+
}
103+
return "good", ":+1::skin-tone-2:"
104+
}
105+
106+
func BuildSlackResponse(org string, repository string, score float64, maxScore float64, penalties []scoring.ScoringPenalty) []slack.MsgOption {
107+
messageColor, messageIcon := GetSlackMessageColorAndIcon(score, maxScore)
108+
109+
// build default message
110+
res := []slack.MsgOption{
111+
slack.MsgOptionIconEmoji(messageIcon),
112+
slack.MsgOptionText(fmt.Sprintf("Analysis of `%s/%s` complete, final score is *%.2f/10.00* points!", org, repository, score), false),
113+
}
114+
115+
// append penalty attachment containing details
116+
if len(penalties) > 0 {
117+
penaltyOutput := ""
118+
for _, penalty := range penalties {
119+
penaltyOutput += fmt.Sprintf("-*%.2f* _%s_\n", penalty.Amount, penalty.Reason)
120+
}
121+
122+
attachment := slack.MsgOptionAttachments(
123+
slack.Attachment{
124+
Color: messageColor,
125+
MarkdownIn: []string{"text"},
126+
Text: penaltyOutput,
127+
Pretext: "The following reasons lead to penalties",
128+
},
129+
)
130+
131+
res = append(res, attachment)
132+
}
133+
134+
return res
135+
}

helpers/helpers_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package helpers
33
import (
44
"testing"
55

6+
"github.com/schaermu/go-github-judge-bot/scoring"
67
"github.com/stretchr/testify/assert"
78
)
89

@@ -35,3 +36,36 @@ func TestGetRepositoryData(t *testing.T) {
3536
assert.Equal(t, "github-judge-bot", info.RepositoryName)
3637
assert.GreaterOrEqual(t, 1, len(info.Contributors))
3738
}
39+
40+
func TestGetSlackMessageColors(t *testing.T) {
41+
color, icon := GetSlackMessageColorAndIcon(10, 10)
42+
assert.Equal(t, "good", color)
43+
assert.Equal(t, ":+1::skin-tone-2:", icon)
44+
}
45+
46+
func TestGetSlackMessageColorsWarn(t *testing.T) {
47+
color, icon := GetSlackMessageColorAndIcon(10, 7)
48+
assert.Equal(t, "warning", color)
49+
assert.Equal(t, ":warning:", icon)
50+
}
51+
func TestGetSlackMessageColorsDanger(t *testing.T) {
52+
color, icon := GetSlackMessageColorAndIcon(10, 2)
53+
assert.Equal(t, "danger", color)
54+
assert.Equal(t, ":exclamation:", icon)
55+
}
56+
57+
func TestBuildSlackResponse(t *testing.T) {
58+
penalties := []scoring.ScoringPenalty{}
59+
60+
msgBlocks := BuildSlackResponse("foo", "bar", 10, 10, penalties)
61+
62+
assert.Len(t, msgBlocks, 2)
63+
}
64+
65+
func TestBuildSlackResponsePenalties(t *testing.T) {
66+
penalties := []scoring.ScoringPenalty{{Reason: "Test reason", Amount: 3}}
67+
68+
msgBlocks := BuildSlackResponse("foo", "bar", 7, 10, penalties)
69+
70+
assert.Len(t, msgBlocks, 3)
71+
}

main.go

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func main() {
7878
fmt.Printf("Error while fetching github info: %e", err)
7979
}
8080

81-
score, penalties := scoring.GetTotalScore(info, cfg.Scorers)
82-
msgBlocks := buildSlackResponse(info.OrgName, info.RepositoryName, score, penalties)
81+
score, maxScore, penalties := scoring.GetTotalScore(info, cfg.Scorers)
82+
msgBlocks := helpers.BuildSlackResponse(info.OrgName, info.RepositoryName, score, maxScore, penalties)
8383
api.PostMessage(ev.Channel, msgBlocks...)
8484
}
8585
}
@@ -94,45 +94,3 @@ func main() {
9494

9595
client.Run()
9696
}
97-
98-
func buildSlackResponse(org string, repository string, score float64, penalties []scoring.ScoringPenalty) []slack.MsgOption {
99-
messageColor := "good"
100-
messageIcon := ":+1::skin-tone-2:"
101-
102-
if score < 8 {
103-
messageColor = "warning"
104-
messageIcon = ":warning:"
105-
}
106-
107-
if score < 4 {
108-
messageColor = "danger"
109-
messageIcon = ":exclamation:"
110-
}
111-
112-
// build default message
113-
res := []slack.MsgOption{
114-
slack.MsgOptionIconEmoji(messageIcon),
115-
slack.MsgOptionText(fmt.Sprintf("Analysis of `%s/%s` complete, final score is *%.2f/10.00* points!", org, repository, score), false),
116-
}
117-
118-
// append penalty attachment containing details
119-
if len(penalties) > 0 {
120-
penaltyOutput := ""
121-
for _, penalty := range penalties {
122-
penaltyOutput += fmt.Sprintf("-*%.2f* _%s_\n", penalty.Amount, penalty.Reason)
123-
}
124-
125-
attachment := slack.MsgOptionAttachments(
126-
slack.Attachment{
127-
Color: messageColor,
128-
MarkdownIn: []string{"text"},
129-
Text: penaltyOutput,
130-
Pretext: "The following reasons lead to penalties",
131-
},
132-
)
133-
134-
res = append(res, attachment)
135-
}
136-
137-
return res
138-
}

scoring/commit_activity_scorer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"math"
66

77
"github.com/schaermu/go-github-judge-bot/config"
8-
"github.com/schaermu/go-github-judge-bot/helpers"
8+
"github.com/schaermu/go-github-judge-bot/data"
99
)
1010

1111
type CommitActivityScorer struct {
12-
data helpers.GithubRepoInfo
12+
data data.GithubRepoInfo
1313
config config.ScorerConfig
1414
}
1515

scoring/commit_activity_scorer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/google/go-github/v35/github"
99
"github.com/schaermu/go-github-judge-bot/config"
10-
"github.com/schaermu/go-github-judge-bot/helpers"
10+
"github.com/schaermu/go-github-judge-bot/data"
1111
"github.com/stretchr/testify/assert"
1212
)
1313

@@ -29,7 +29,7 @@ func getTestCommitActivityData(inactiveWeekCount int) []*github.WeeklyCommitActi
2929

3030
func getTestCommitActivityScorer(inactiveWeekCount int, penaltyPerWeek float64) CommitActivityScorer {
3131
return CommitActivityScorer{
32-
data: helpers.GithubRepoInfo{
32+
data: data.GithubRepoInfo{
3333
CommitActivity: getTestCommitActivityData(inactiveWeekCount),
3434
},
3535
config: config.ScorerConfig{

scoring/contributors_scorer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"math"
66

77
"github.com/schaermu/go-github-judge-bot/config"
8-
"github.com/schaermu/go-github-judge-bot/helpers"
8+
"github.com/schaermu/go-github-judge-bot/data"
99
)
1010

1111
type ContributorsScorer struct {
1212
Scorer
13-
data helpers.GithubRepoInfo
13+
data data.GithubRepoInfo
1414
config config.ScorerConfig
1515
}
1616

scoring/contributors_scorer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/google/go-github/v35/github"
88
"github.com/schaermu/go-github-judge-bot/config"
9-
"github.com/schaermu/go-github-judge-bot/helpers"
9+
"github.com/schaermu/go-github-judge-bot/data"
1010
"github.com/stretchr/testify/assert"
1111
)
1212

@@ -23,7 +23,7 @@ func getTestContributorData(contributorCount int) []*github.ContributorStats {
2323

2424
func getTestContributorScorer(contributorCount int, minContributors int) ContributorsScorer {
2525
return ContributorsScorer{
26-
data: helpers.GithubRepoInfo{
26+
data: data.GithubRepoInfo{
2727
Contributors: getTestContributorData(contributorCount),
2828
},
2929
config: config.ScorerConfig{

scoring/issue_scorer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/google/go-github/v35/github"
88
"github.com/schaermu/go-github-judge-bot/config"
9-
"github.com/schaermu/go-github-judge-bot/helpers"
9+
"github.com/schaermu/go-github-judge-bot/data"
1010
"github.com/stretchr/testify/assert"
1111
)
1212

@@ -25,7 +25,7 @@ func getTestIssueData(closedOpenRatio float64, closedIssueCount int, openIssueCo
2525

2626
func getTestIssueScorer(closedOpenRatio float64, closedIssueCount int, openIssueCount int) IssuesScorer {
2727
return IssuesScorer{
28-
data: helpers.GithubRepoInfo{
28+
data: data.GithubRepoInfo{
2929
Issues: getTestIssueData(closedOpenRatio, closedIssueCount, openIssueCount),
3030
},
3131
config: config.ScorerConfig{

scoring/issues_scorer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"math"
66

77
"github.com/schaermu/go-github-judge-bot/config"
8-
"github.com/schaermu/go-github-judge-bot/helpers"
8+
"github.com/schaermu/go-github-judge-bot/data"
99
)
1010

1111
type IssuesScorer struct {
12-
data helpers.GithubRepoInfo
12+
data data.GithubRepoInfo
1313
config config.ScorerConfig
1414
}
1515

scoring/license_scorer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55

66
"github.com/mitchellh/go-spdx"
77
"github.com/schaermu/go-github-judge-bot/config"
8-
"github.com/schaermu/go-github-judge-bot/helpers"
8+
"github.com/schaermu/go-github-judge-bot/data"
99
)
1010

1111
type LicenseScorer struct {
12-
data helpers.GithubRepoInfo
12+
data data.GithubRepoInfo
1313
config config.ScorerConfig
1414
}
1515

scoring/license_scorer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"testing"
66

77
"github.com/schaermu/go-github-judge-bot/config"
8-
"github.com/schaermu/go-github-judge-bot/helpers"
8+
"github.com/schaermu/go-github-judge-bot/data"
99
"github.com/stretchr/testify/assert"
1010
)
1111

1212
func getTestLicenseScorer(license string, validLicenses []string) LicenseScorer {
1313
return LicenseScorer{
14-
data: helpers.GithubRepoInfo{
14+
data: data.GithubRepoInfo{
1515
License: license,
1616
LicenseId: license,
1717
},

0 commit comments

Comments
 (0)