Skip to content

Instantly share code, notes, and snippets.

@Elzair
Last active August 29, 2015 14:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Elzair/48a8ca130f3c251eb144 to your computer and use it in GitHub Desktop.
bitbucket-webhook
{-# LANGUAGE OverloadedStrings #-}
module Bitbucket (
parseHook
) where
import Control.Applicative ((<$>))
import Control.Lens
import Data.Aeson.Lens
import Data.List (nub)
import qualified Data.Text as DT
import qualified Data.ByteString.Lazy.Char8 as BS
data HookInfo = HookInfo {
hookProvider :: DT.Text,
hookRepository :: DT.Text,
hookBranch :: DT.Text
} deriving (Show,Eq)
stripSlashes :: DT.Text -> DT.Text
stripSlashes input = DT.reverse $ DT.drop 1 $ DT.reverse $ DT.drop 1 input
extractRepositoryName :: BS.ByteString -> DT.Text
extractRepositoryName str = str ^?! key "repository" . key "absolute_url" . _String
extractBranches :: BS.ByteString -> [DT.Text]
extractBranches str = str ^.. key "commits" . _Array . traverse . to (\o -> (o ^?! key "branch" . _String))
parseHook :: FilePath -> IO [HookInfo]
parseHook file = do
contents <- BS.readFile file
let name = stripSlashes $ extractRepositoryName contents
branches = extractBranches contents
out = nub $ (\b -> HookInfo{hookProvider = "bitbucket", hookRepository = name, hookBranch = b}) <$> branches
return out
{
"canon_url": "https://bitbucket.org",
"commits": [
{
"author": "marcus",
"branch": "master",
"files": [
{
"file": "somefile.py",
"type": "modified"
}
],
"message": "Added some more things to somefile.py\n",
"node": "620ade18607a",
"parents": [
"702c70160afc"
],
"raw_author": "Marcus Bertrand <marcus@somedomain.com>",
"raw_node": "620ade18607ac42d872b568bb92acaa9a28620e9",
"revision": null,
"size": -1,
"timestamp": "2012-05-30 05:58:56",
"utctimestamp": "2012-05-30 03:58:56+00:00"
},
{
"author": "marcus",
"branch": "test",
"files": [
{
"file": "somefile.py",
"type": "modified"
}
],
"message": "Added some more things to somefile.py\n",
"node": "620ade18607a",
"parents": [
"702c70160afc"
],
"raw_author": "Marcus Bertrand <marcus@somedomain.com>",
"raw_node": "620ade18607ac42d872b568bb92acaa9a28620e9",
"revision": null,
"size": -1,
"timestamp": "2012-05-30 05:58:56",
"utctimestamp": "2012-05-30 03:58:56+00:00"
}
],
"repository": {
"absolute_url": "/marcus/project-x/",
"fork": false,
"is_private": true,
"name": "Project X",
"owner": "marcus",
"scm": "git",
"slug": "project-x",
"website": "https://atlassian.com/"
},
"user": "marcus"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment