Repository navigation
Expand file tree
/
Copy pathContents.hs
More file actions
171 lines (150 loc) · 5.45 KB
/
Copy pathContents.hs
File metadata and controls
171 lines (150 loc) · 5.45 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
-----------------------------------------------------------------------------
-- |
-- License : BSD-3-Clause
-- Maintainer : Oleg Grenrus <oleg.grenrus@iki.fi>
--
-- The Github Repo Contents API, as documented at
-- <https://developer.github.com/v3/repos/contents/>
module GitHub.Endpoints.Repos.Contents (
-- * Querying contents
contentsFor,
contentsFor',
contentsForR,
readmeFor,
readmeFor',
readmeForR,
archiveFor,
archiveFor',
archiveForR,
-- ** Create
createFile,
createFileR,
-- ** Update
updateFile,
updateFileR,
-- ** Delete
deleteFile,
deleteFileR,
module GitHub.Data
) where
import GitHub.Data
import GitHub.Internal.Prelude
import GitHub.Request
import Prelude ()
import Data.Maybe (maybeToList)
import qualified Data.Text.Encoding as TE
import Network.URI (URI)
-- | The contents of a file or directory in a repo, given the repo owner, name, and path to the file
--
-- > contentsFor "thoughtbot" "paperclip" "README.md"
contentsFor :: Name Owner -> Name Repo -> Text -> Maybe Text -> IO (Either Error Content)
contentsFor = contentsFor' Nothing
-- | The contents of a file or directory in a repo, given the repo owner, name, and path to the file
-- With Authentication
--
-- > contentsFor' (Just $ BasicAuth "github-username" "github-password") "thoughtbot" "paperclip" "README.md" Nothing
contentsFor' :: Maybe Auth -> Name Owner -> Name Repo -> Text -> Maybe Text -> IO (Either Error Content)
contentsFor' auth user repo path ref =
executeRequestMaybe auth $ contentsForR user repo path ref
contentsForR
:: Name Owner
-> Name Repo
-> Text -- ^ file or directory
-> Maybe Text -- ^ Git commit
-> Request k Content
contentsForR user repo path ref =
query ["repos", toPathPart user, toPathPart repo, "contents", path] qs
where
qs = maybe [] (\r -> [("ref", Just . TE.encodeUtf8 $ r)]) ref
-- | The contents of a README file in a repo, given the repo owner and name
--
-- > readmeFor "thoughtbot" "paperclip"
readmeFor :: Name Owner -> Name Repo -> IO (Either Error Content)
readmeFor = readmeFor' Nothing
-- | The contents of a README file in a repo, given the repo owner and name
-- With Authentication
--
-- > readmeFor' (Just $ BasicAuth "github-username" "github-password") "thoughtbot" "paperclip"
readmeFor' :: Maybe Auth -> Name Owner -> Name Repo -> IO (Either Error Content)
readmeFor' auth user repo =
executeRequestMaybe auth $ readmeForR user repo
readmeForR :: Name Owner -> Name Repo -> Request k Content
readmeForR user repo =
query ["repos", toPathPart user, toPathPart repo, "readme"] []
-- | The archive of a repo, given the repo owner, name, and archive type
--
-- > archiveFor "thoughtbot" "paperclip" ArchiveFormatTarball Nothing
archiveFor :: Name Owner -> Name Repo -> ArchiveFormat -> Maybe Text -> IO (Either Error URI)
archiveFor = archiveFor' Nothing
-- | The archive of a repo, given the repo owner, name, and archive type
-- With Authentication
--
-- > archiveFor' (Just $ BasicAuth "github-username" "github-password") "thoughtbot" "paperclip" ArchiveFormatTarball Nothing
archiveFor' :: Maybe Auth -> Name Owner -> Name Repo -> ArchiveFormat -> Maybe Text -> IO (Either Error URI)
archiveFor' auth user repo path ref =
executeRequestMaybe auth $ archiveForR user repo path ref
-- | Get archive link.
-- See <https://developer.github.com/v3/repos/contents/#get-archive-link>
archiveForR
:: Name Owner
-> Name Repo
-> ArchiveFormat -- ^ The type of archive to retrieve
-> Maybe Text -- ^ Git commit
-> GenRequest 'MtRedirect rw URI
archiveForR user repo format ref = Query path []
where
path = ["repos", toPathPart user, toPathPart repo, toPathPart format] <> maybeToList ref
-- | Create a file.
createFile
:: Auth
-> Name Owner -- ^ owner
-> Name Repo -- ^ repository name
-> CreateFile
-> IO (Either Error ContentResult)
createFile auth user repo body =
executeRequest auth $ createFileR user repo body
-- | Create a file.
-- See <https://developer.github.com/v3/repos/contents/#create-a-file>
createFileR
:: Name Owner
-> Name Repo
-> CreateFile
-> Request 'RW ContentResult
createFileR user repo body =
command Put ["repos", toPathPart user, toPathPart repo, "contents", createFilePath body] (encode body)
-- | Update a file.
updateFile
:: Auth
-> Name Owner -- ^ owner
-> Name Repo -- ^ repository name
-> UpdateFile
-> IO (Either Error ContentResult)
updateFile auth user repo body =
executeRequest auth $ updateFileR user repo body
-- | Update a file.
-- See <https://developer.github.com/v3/repos/contents/#update-a-file>
updateFileR
:: Name Owner
-> Name Repo
-> UpdateFile
-> Request 'RW ContentResult
updateFileR user repo body =
command Put ["repos", toPathPart user, toPathPart repo, "contents", updateFilePath body] (encode body)
-- | Delete a file.
deleteFile
:: Auth
-> Name Owner -- ^ owner
-> Name Repo -- ^ repository name
-> DeleteFile
-> IO (Either Error ())
deleteFile auth user repo body =
executeRequest auth $ deleteFileR user repo body
-- | Delete a file.
-- See <https://developer.github.com/v3/repos/contents/#delete-a-file>
deleteFileR
:: Name Owner
-> Name Repo
-> DeleteFile
-> GenRequest 'MtUnit 'RW ()
deleteFileR user repo body =
Command Delete ["repos", toPathPart user, toPathPart repo, "contents", deleteFilePath body] (encode body)