Skip to content

Commit

Permalink
adds tags to issue list - fixes issue #23 (#30)
Browse files Browse the repository at this point in the history
* adds tags to issue list - fixes issue #23

* made code simpler - labels on newline

* cleaner label filter plus test
  • Loading branch information
aaronarduino authored and jcbwlkr committed Oct 11, 2017
1 parent a5de519 commit 3d9ca6e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
47 changes: 36 additions & 11 deletions issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -162,17 +169,23 @@ 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 {
return errors.Wrap(err, "could not decode json")
}

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)
Expand All @@ -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
}
23 changes: 23 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 4 additions & 0 deletions public/css/profile.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
.navbar img {
height: 3em;
}

.is-issue-tag {
margin-left: 6px;
}
10 changes: 9 additions & 1 deletion public/js/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ function loadIssues() {
}
var rows = '';
data.forEach(function(issue) {
var tags = "";
for (var tag in issue["Labels"]) {
tags += "<span class='badge badge-default is-issue-tag' style='background-color: #" + issue["Labels"][tag] + ";'>" + tag + "</span>";
}
if (tags != "") {
tags = "</br>" + tags;
}

var row = "<tr>" +
"<td>" + issue["Title"] + "</td>" +
"<td>" + issue["Title"] + tags + "</td>" +
"<td>" + issue["Repo"]["Owner"] + "/" + issue["Repo"]["Name"] + "</td>" +
"<td> <a class='btn btn-sm btn-success' href='https://github.com/" + issue["URL"].split("repos")[1] + "' target='_blank'>Open Issue</a></td>" +
"</tr>";
Expand Down

0 comments on commit 3d9ca6e

Please sign in to comment.