Skip to content

Commit 8bf5ebf

Browse files
authored
Stargazers (#99)
* display stargazers * filter on <= 5 stargazers
1 parent b0c1646 commit 8bf5ebf

File tree

1 file changed

+78
-15
lines changed

1 file changed

+78
-15
lines changed

src/Project.elm

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Project exposing (DisplayType(..), Language(..), Project, getProjects, la
22

33
-- Represents a single "project"
44

5+
import Ansi.Log exposing (LineDiscipline(..))
56
import BackendTask exposing (BackendTask)
67
import BackendTask.Env
78
import BackendTask.Glob as Glob
@@ -41,6 +42,10 @@ type alias Project =
4142
type alias ProcessedData =
4243
{ imgPath : String
4344
, year : Year
45+
46+
-- only add github stars if the project has a github repo and it's above 5 stars (arbitrary)
47+
-- kinda embarassing to display star count if it's THAT low...
48+
, stargazers : Maybe Int
4449
}
4550

4651

@@ -120,6 +125,7 @@ addProcessedData decodedProj =
120125
BackendTask.succeed ProcessedData
121126
|> BackendTask.andMap (addImagePath decodedProj)
122127
|> BackendTask.andMap (addYear decodedProj)
128+
|> BackendTask.andMap (addStargazers decodedProj)
123129
|> BackendTask.map
124130
(\processed ->
125131
{ raw = decodedProj
@@ -139,6 +145,53 @@ getEnvs =
139145
|> BackendTask.andMap (BackendTask.Env.get "GITHUB_TOKEN")
140146

141147

148+
githubApiAuthHeader : Env -> List ( String, String )
149+
githubApiAuthHeader env =
150+
case env.githubToken of
151+
Just token ->
152+
[ ( "Authorization", "token " ++ token ) ]
153+
154+
Nothing ->
155+
[]
156+
157+
158+
159+
-- it's ok to re-fetch the github API since the get requests are cached
160+
161+
162+
addStargazers : RawData -> BackendTask FatalError (Maybe Int)
163+
addStargazers proj =
164+
case proj.githubRepo of
165+
Just repoName ->
166+
getEnvs
167+
|> BackendTask.andThen (getStargazers repoName)
168+
169+
Nothing ->
170+
BackendTask.succeed Nothing
171+
172+
173+
getStargazers : String -> Env -> BackendTask FatalError (Maybe Int)
174+
getStargazers repoName env =
175+
BackendTask.Http.getWithOptions
176+
{ url = "https://api.github.com/repos/" ++ repoName
177+
, expect = BackendTask.Http.expectJson (Json.Decode.field "stargazers_count" Json.Decode.int)
178+
, headers = githubApiAuthHeader env
179+
, cacheStrategy = Nothing
180+
, retries = Nothing
181+
, timeoutInMs = Nothing
182+
, cachePath = Nothing
183+
}
184+
|> BackendTask.allowFatal
185+
|> BackendTask.map
186+
(\stargazers ->
187+
if stargazers > 5 then
188+
Just stargazers
189+
190+
else
191+
Nothing
192+
)
193+
194+
142195

143196
-- if the project doesn't have a "year" field, scrape the github API
144197
-- to get the date of the first commit and latest commit, and use that to create a range
@@ -169,19 +222,11 @@ getYearFromGithub repoName env =
169222
Json.Decode.map2 Range
170223
(Json.Decode.field "created_at" dateDecoder)
171224
(Json.Decode.field "pushed_at" dateDecoder)
172-
173-
headers =
174-
case env.githubToken of
175-
Just token ->
176-
[ ( "Authorization", "token " ++ token ) ]
177-
178-
Nothing ->
179-
[]
180225
in
181226
BackendTask.Http.getWithOptions
182227
{ url = "https://api.github.com/repos/" ++ repoName
183228
, expect = BackendTask.Http.expectJson yearDecoder
184-
, headers = headers
229+
, headers = githubApiAuthHeader env
185230
, cacheStrategy = Nothing
186231
, retries = Nothing
187232
, timeoutInMs = Nothing
@@ -421,7 +466,11 @@ view proj =
421466
]
422467
]
423468
[ projectTitle proj.raw
424-
, projectYear proj.processed.year
469+
, Html.p
470+
[ css [ fontSize (em 0.75) ] ]
471+
[ projectStars proj.processed.stargazers
472+
, projectYear proj.processed.year
473+
]
425474
, Html.p [] [ Html.text proj.raw.blurb ]
426475
, languagesAndConcepts Util.Row { languages = proj.raw.languages, concepts = proj.raw.concepts }
427476
]
@@ -446,7 +495,11 @@ viewFeatured proj =
446495
}
447496
[ padding2 (px 0) (em 1) ]
448497
, projectTitle proj.raw
449-
, projectYear proj.processed.year
498+
, Html.p
499+
[ css [ fontSize (em 0.75) ] ]
500+
[ projectStars proj.processed.stargazers
501+
, projectYear proj.processed.year
502+
]
450503
, Html.p [] [ Html.text proj.raw.blurb ]
451504

452505
-- height-filling empty div to align the languages/concepts and links to the bottom
@@ -499,6 +552,18 @@ projectTitle raw =
499552
[ Html.text raw.name ]
500553

501554

555+
projectStars : Maybe Int -> Html msg
556+
projectStars stargazers =
557+
case stargazers of
558+
Just stars ->
559+
Html.span
560+
[]
561+
[ Html.text <| String.fromInt stars ++ " ⭐ ⋅ " ]
562+
563+
Nothing ->
564+
Html.text ""
565+
566+
502567
projectYear : Year -> Html msg
503568
projectYear year =
504569
let
@@ -528,10 +593,8 @@ projectYear year =
528593
Range start end ->
529594
displayRange start end
530595
in
531-
Html.p
532-
[ css
533-
[ fontSize (em 0.75) ]
534-
, case year of
596+
Html.span
597+
[ case year of
535598
Manual _ ->
536599
Html.Styled.Attributes.attribute "data-datetype" "manual"
537600

0 commit comments

Comments
 (0)