forked from haskell-github/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContents.hs
More file actions
116 lines (101 loc) · 4.04 KB
/
Contents.hs
File metadata and controls
116 lines (101 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Common hiding
(getContents, intercalate, take, truncate, unlines)
import qualified Data.ByteString.Base64 as Base64
import Data.Text
(Text, intercalate, take, unlines)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import Data.Text.IO (putStrLn)
import qualified Data.Vector as Vector
import qualified GitHub.Data as GitHub
import qualified GitHub.Endpoints.Repos.Contents as GitHub
main :: IO ()
main = do
putStrLn "Root"
putStrLn "===="
getContents ""
putStrLn "LICENSE"
putStrLn "======="
getContents "LICENSE"
createUpdateDeleteSampleFile
getContents :: Text -> IO ()
getContents path = do
contents <- GitHub.contentsFor "mike-burns" "ohlaunch" path Nothing
putStrLn $ either (("Error: " <>) . tshow) formatContents contents
formatContents :: GitHub.Content -> Text
formatContents (GitHub.ContentFile fileData) =
formatContentInfo (GitHub.contentFileInfo fileData) <>
unlines
[ tshow (GitHub.contentFileSize fileData) <> " bytes"
, "encoding: " <> GitHub.contentFileEncoding fileData
, "data: " <> truncate (GitHub.contentFileContent fileData)
]
formatContents (GitHub.ContentDirectory items) =
intercalate "\n\n" . map formatItem . Vector.toList $ items
formatContentInfo :: GitHub.ContentInfo -> Text
formatContentInfo contentInfo =
unlines
[ "name: " <> GitHub.contentName contentInfo
, "path: " <> GitHub.contentPath contentInfo
, "sha: " <> GitHub.contentSha contentInfo
, "url: " <> (GitHub.getUrl . GitHub.contentUrl) contentInfo
, "git url: " <> (GitHub.getUrl . GitHub.contentGitUrl) contentInfo
, "html url: " <> (GitHub.getUrl . GitHub.contentHtmlUrl) contentInfo
]
formatItem :: GitHub.ContentItem -> Text
formatItem item =
"type: " <> tshow (GitHub.contentItemType item) <> "\n" <>
formatContentInfo (GitHub.contentItemInfo item)
truncate :: Text -> Text
truncate str = take 40 str <> "... (truncated)"
createUpdateDeleteSampleFile :: IO ()
createUpdateDeleteSampleFile = do
let
auth = GitHub.OAuth "oauthtoken"
owner = "repoOwner"
repo = "repoName"
author = GitHub.Author
{ GitHub.authorName = "John Doe"
, GitHub.authorEmail = "johndoe@example.com"
}
defaultBranch = Nothing
base64Encode = decodeUtf8 . Base64.encode . encodeUtf8
createResult <- failOnError $ GitHub.createFile auth owner repo
GitHub.CreateFile
{ GitHub.createFilePath = "sample.txt"
, GitHub.createFileMessage = "Add sample.txt"
, GitHub.createFileContent = base64Encode "Hello"
, GitHub.createFileBranch = defaultBranch
, GitHub.createFileAuthor = Just author
, GitHub.createFileCommitter = Just author
}
let getResultSHA = GitHub.contentSha . GitHub.contentResultInfo . GitHub.contentResultContent
let createFileSHA = getResultSHA createResult
updateResult <- failOnError $ GitHub.updateFile auth owner repo
GitHub.UpdateFile
{ GitHub.updateFilePath = "sample.txt"
, GitHub.updateFileMessage = "Update sample.txt"
, GitHub.updateFileContent = base64Encode "Hello world!"
, GitHub.updateFileSHA = createFileSHA
, GitHub.updateFileBranch = defaultBranch
, GitHub.updateFileAuthor = Just author
, GitHub.updateFileCommitter = Just author
}
let updateFileSHA = getResultSHA updateResult
failOnError $ GitHub.deleteFile auth owner repo
GitHub.DeleteFile
{ GitHub.deleteFilePath = "sample.txt"
, GitHub.deleteFileMessage = "Delete sample.txt"
, GitHub.deleteFileSHA = updateFileSHA
, GitHub.deleteFileBranch = defaultBranch
, GitHub.deleteFileAuthor = Just author
, GitHub.deleteFileCommitter = Just author
}
failOnError :: IO (Either GitHub.Error a) -> IO a
failOnError c = c >>= go
where
go r = case r of
Left err -> fail . show $ err
Right x -> return x