@@ -5,8 +5,10 @@ module Main where
55
66import Common hiding
77 (getContents , intercalate , take , truncate , unlines )
8+ import qualified Data.ByteString.Base64 as Base64
89import Data.Text
9- (Text , intercalate , pack , take , unlines )
10+ (Text , intercalate , take , unlines )
11+ import Data.Text.Encoding (decodeUtf8 , encodeUtf8 )
1012import Data.Text.IO (putStrLn )
1113import qualified Data.Vector as Vector
1214import qualified GitHub.Data as GitHub
@@ -22,6 +24,8 @@ main = do
2224 putStrLn " ======="
2325 getContents " LICENSE"
2426
27+ createUpdateDeleteSampleFile
28+
2529getContents :: Text -> IO ()
2630getContents path = do
2731 contents <- GitHub. contentsFor " mike-burns" " ohlaunch" path Nothing
@@ -50,9 +54,63 @@ formatContentInfo contentInfo =
5054 , " html url: " <> (GitHub. getUrl . GitHub. contentHtmlUrl) contentInfo
5155 ]
5256
57+ formatItem :: GitHub. ContentItem -> Text
5358formatItem item =
5459 " type: " <> tshow (GitHub. contentItemType item) <> " \n " <>
5560 formatContentInfo (GitHub. contentItemInfo item)
5661
57-
62+ truncate :: Text -> Text
5863truncate str = take 40 str <> " ... (truncated)"
64+
65+ createUpdateDeleteSampleFile :: IO ()
66+ createUpdateDeleteSampleFile = do
67+ let
68+ auth = GitHub. OAuth " oauthtoken"
69+ owner = " repoOwner"
70+ repo = " repoName"
71+ author = GitHub. Author
72+ { GitHub. authorName = " John Doe"
73+ , GitHub. authorEmail = " johndoe@example.com"
74+ }
75+ defaultBranch = Nothing
76+ base64Encode = decodeUtf8 . Base64. encode . encodeUtf8
77+ createResult <- failOnError $ GitHub. createFile auth owner repo
78+ GitHub. CreateFile
79+ { GitHub. createFilePath = " sample.txt"
80+ , GitHub. createFileMessage = " Add sample.txt"
81+ , GitHub. createFileContent = base64Encode " Hello"
82+ , GitHub. createFileBranch = defaultBranch
83+ , GitHub. createFileAuthor = Just author
84+ , GitHub. createFileCommitter = Just author
85+ }
86+
87+ let getResultSHA = GitHub. contentSha . GitHub. contentResultInfo . GitHub. contentResultContent
88+ let createFileSHA = getResultSHA createResult
89+ updateResult <- failOnError $ GitHub. updateFile auth owner repo
90+ GitHub. UpdateFile
91+ { GitHub. updateFilePath = " sample.txt"
92+ , GitHub. updateFileMessage = " Update sample.txt"
93+ , GitHub. updateFileContent = base64Encode " Hello world!"
94+ , GitHub. updateFileSHA = createFileSHA
95+ , GitHub. updateFileBranch = defaultBranch
96+ , GitHub. updateFileAuthor = Just author
97+ , GitHub. updateFileCommitter = Just author
98+ }
99+
100+ let updateFileSHA = getResultSHA updateResult
101+ failOnError $ GitHub. deleteFile auth owner repo
102+ GitHub. DeleteFile
103+ { GitHub. deleteFilePath = " sample.txt"
104+ , GitHub. deleteFileMessage = " Delete sample.txt"
105+ , GitHub. deleteFileSHA = updateFileSHA
106+ , GitHub. deleteFileBranch = defaultBranch
107+ , GitHub. deleteFileAuthor = Just author
108+ , GitHub. deleteFileCommitter = Just author
109+ }
110+
111+ failOnError :: IO (Either GitHub. Error a ) -> IO a
112+ failOnError c = c >>= go
113+ where
114+ go r = case r of
115+ Left err -> fail . show $ err
116+ Right x -> return x
0 commit comments