Created
May 8, 2015 07:06
-
-
Save rnons/9323cc10272a78ac3b5f to your computer and use it in GitHub Desktop.
Report pull request numbers to BearyChat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env runhaskell | |
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Applicative ((<$>)) | |
import Control.Monad (forM, void, when) | |
import Data.Aeson ((.=), Value, object, encode, decode) | |
import qualified Data.ByteString.Lazy as L | |
import Data.Maybe (fromMaybe, fromJust) | |
import Data.List (intercalate) | |
import Network.HTTP.Conduit ( Request(..), applyBasicAuth, parseUrl | |
, urlEncodedBody, responseBody | |
, withManager, httpLbs) | |
import Network.HTTP.Types (hUserAgent) | |
-- Basic Auth: https://developer.github.com/v3/auth/#via-username-and-password | |
-- List PR: https://developer.github.com/v3/pulls/#list-pull-requests | |
mkRequest :: String -> String -> Request | |
mkRequest owner repo = | |
applyBasicAuth username password $ req { requestHeaders = [uaHdr] } | |
where | |
url = intercalate "/" ["https://api.github.com/repos", owner, repo, "pulls"] | |
req = fromJust $ parseUrl url | |
username = "rnons" | |
password = "personal-access-token" | |
uaHdr = (hUserAgent, "haskell client") | |
main :: IO () | |
main = void $ withManager $ \manager -> do | |
counts <- forM requests $ \req -> countPr <$> httpLbs req manager | |
when (sum counts > 0) $ do | |
let text = "放学别走,还有 " ++ show (sum counts) ++ " 个 PR 没合呢" | |
attachmentText = intercalate "\n" $ zipWith prInfo repos counts | |
req' <- parseUrl botUrl | |
let payload = object [ "text" .= text | |
, "attachments" .= [object [ "text" .= attachmentText]] | |
] | |
query = [("payload", L.toStrict $ encode payload)] | |
req = urlEncodedBody query req' | |
void $ httpLbs req manager | |
where | |
owner = "bearyinnovative" | |
repos = ["harry", "hermione", "ronald"] | |
requests = map (mkRequest owner) repos | |
countPr resp = length $ fromMaybe [] (decode $ responseBody resp :: Maybe [Value]) | |
repoPrUrl repo = intercalate "/" ["https://github.com", owner, repo, "pulls"] | |
prInfo _ 0 = "" | |
prInfo repo count = repo ++ ":" ++ show count ++ " 个 [PR](" ++ repoPrUrl repo ++ ")" | |
botUrl = "https://hook.bearychat.com/incoming-bot-token" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment