From 3d9ca6e9e87ac31bc4625fe9b9eecbb97979418c Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 11 Oct 2017 10:04:05 -0500 Subject: [PATCH] adds tags to issue list - fixes issue #23 (#30) * adds tags to issue list - fixes issue #23 * made code simpler - labels on newline * cleaner label filter plus test --- issues.go | 47 ++++++++++++++++++++++++++++++++---------- issues_test.go | 23 +++++++++++++++++++++ public/css/profile.css | 4 ++++ public/js/profile.js | 10 ++++++++- 4 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 issues_test.go diff --git a/issues.go b/issues.go index a59f50d..b2f1b62 100644 --- a/issues.go +++ b/issues.go @@ -12,12 +12,22 @@ import ( "github.com/pkg/errors" ) +// labels to fetch issues for +var labels = map[string]bool{"hacktoberfest": true, "help wanted": true} + // Issue is a requested change against one of our tracked GitHub repos. type Issue struct { - Title string - Date time.Time - URL string - Repo Repo + Title string + Date time.Time + URL string + Repo Repo + Labels map[string]string +} + +// Labels are labels on a tracked issue. +type Labels []struct { + Name string `json:"name"` + Color string `json:"color"` } func issues(w http.ResponseWriter, r *http.Request) { @@ -45,9 +55,6 @@ func issues(w http.ResponseWriter, r *http.Request) { // label:B only label:A AND label:B so we have to make multiple requests. func fetchIssues(ctx context.Context, token string) ([]Issue, error) { - // Kick off a worker for each of these labels - labels := []string{"hacktoberfest", "help wanted"} - // main chan where workers send their results ch := make(chan Issue) @@ -62,7 +69,7 @@ func fetchIssues(ctx context.Context, token string) ([]Issue, error) { var wg sync.WaitGroup wg.Add(len(labels)) - for _, l := range labels { + for l := range labels { go func(l string) { if err := issueSearch(cCtx, l, token, ch); err != nil { errors <- err @@ -162,6 +169,7 @@ func issueSearch(ctx context.Context, label, token string, ch chan<- Issue) erro CreatedAt time.Time `json:"created_at"` URL string `json:"url"` RepoURL string `json:"repository_url"` + Labels `json:"labels"` } `json:"items"` } if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { @@ -169,10 +177,15 @@ func issueSearch(ctx context.Context, label, token string, ch chan<- Issue) erro } for _, item := range data.Items { + + // filter out hacktoberfest labels + issueLabels := labelFilter(item.Labels) + issue := Issue{ - Title: item.Title, - Date: item.CreatedAt, - URL: item.URL, + Title: item.Title, + Date: item.CreatedAt, + URL: item.URL, + Labels: issueLabels, } issue.Repo, err = repoFromURL(item.RepoURL) @@ -192,3 +205,15 @@ func issueSearch(ctx context.Context, label, token string, ch chan<- Issue) erro } return nil } + +// labelFilter filters to show only labels that are +// not related to hacktoberfest. +func labelFilter(lbs Labels) map[string]string { + issueLabels := make(map[string]string) + for _, label := range lbs { + if !labels[label.Name] { + issueLabels[label.Name] = label.Color + } + } + return issueLabels +} diff --git a/issues_test.go b/issues_test.go new file mode 100644 index 0000000..5e72a1a --- /dev/null +++ b/issues_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestLabelFilter(t *testing.T) { + data := Labels{ + {Name: "hacktoberfest", Color: "#ffffff"}, + {Name: "help wanted", Color: "#ffffff"}, + {Name: "test", Color: "#ffffff"}, + } + + want := map[string]string{"test": "#ffffff"} + got := labelFilter(data) + + if !reflect.DeepEqual(got, want) { + t.Errorf("labelFilter(%v) failed", data) + t.Errorf("got %v", got) + t.Errorf("want %v", want) + } +} diff --git a/public/css/profile.css b/public/css/profile.css index d249bca..1975dca 100644 --- a/public/css/profile.css +++ b/public/css/profile.css @@ -11,3 +11,7 @@ .navbar img { height: 3em; } + +.is-issue-tag { + margin-left: 6px; +} diff --git a/public/js/profile.js b/public/js/profile.js index 23a4742..db331e4 100644 --- a/public/js/profile.js +++ b/public/js/profile.js @@ -21,8 +21,16 @@ function loadIssues() { } var rows = ''; data.forEach(function(issue) { + var tags = ""; + for (var tag in issue["Labels"]) { + tags += "" + tag + ""; + } + if (tags != "") { + tags = "
" + tags; + } + var row = "" + - "" + issue["Title"] + "" + + "" + issue["Title"] + tags + "" + "" + issue["Repo"]["Owner"] + "/" + issue["Repo"]["Name"] + "" + " Open Issue" + "";