Skip to content

Commit 80b8e69

Browse files
Add create/update/delete example.
1 parent 8a851fe commit 80b8e69

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

samples/Repos/Contents.hs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ module Main where
55

66
import Common hiding
77
(getContents, intercalate, take, truncate, unlines)
8+
import qualified Data.ByteString.Base64 as Base64
89
import Data.Text
9-
(Text, intercalate, pack, take, unlines)
10+
(Text, intercalate, take, unlines)
11+
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
1012
import Data.Text.IO (putStrLn)
1113
import qualified Data.Vector as Vector
1214
import qualified GitHub.Data as GitHub
@@ -22,6 +24,8 @@ main = do
2224
putStrLn "======="
2325
getContents "LICENSE"
2426

27+
createUpdateDeleteSampleFile
28+
2529
getContents :: Text -> IO ()
2630
getContents 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
5358
formatItem item =
5459
"type: " <> tshow (GitHub.contentItemType item) <> "\n" <>
5560
formatContentInfo (GitHub.contentItemInfo item)
5661

57-
62+
truncate :: Text -> Text
5863
truncate 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

samples/github-samples.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ executable github-repos-contents-example
198198
build-depends:
199199
base
200200
, base-compat
201+
, base64-bytestring
201202
, github
202203
, github-samples
203204
, text

0 commit comments

Comments
 (0)