Skip to content

Commit 27297bd

Browse files
committed
feat: support for codeberg host
Close #100
1 parent 990d4a5 commit 27297bd

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

app/Main.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ githubKeyToParse =
4242
)
4343
)
4444

45+
codebergKeyToParse :: Opt.Parser (Maybe CodebergKey)
46+
codebergKeyToParse =
47+
optional
48+
( CodebergKey
49+
<$> Opt.strOption
50+
( Opt.long "issuetracker-codebergkey"
51+
<> Opt.metavar "PERSONAL_CODEBERG_KEY"
52+
<> Opt.help "A codeberg developer key to allow for more API calls or access to private codeberg repo for the IssueTracker checker"
53+
)
54+
)
55+
4556
parseGitlabKey :: Opt.ReadM (GitlabHost, GitlabKey)
4657
parseGitlabKey = Opt.eitherReader $ \(Text.pack -> s) -> case scan [re|^([^=]+)=(.+)$|] s of
4758
[(_, [x, y])] -> Right (GitlabHost x, GitlabKey y)
@@ -80,6 +91,7 @@ optionsParser =
8091
<*> ( KrankConfig
8192
<$> githubKeyToParse
8293
<*> gitlabKeyToParse
94+
<*> codebergKeyToParse
8395
<*> Opt.switch
8496
( Opt.long "dry-run"
8597
<> Opt.help "Perform a dry run. Parse file, but do not execute HTTP requests"

src/Krank/Checkers/IssueTracker.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import qualified Text.Regex.PCRE.Heavy as RE
3434
import Utils.Github (showGithubException)
3535
import Utils.Gitlab (showGitlabException)
3636

37-
data GitServer = Github | Gitlab GitlabHost
37+
data GitServer = Github | Gitlab GitlabHost | Codeberg
3838
deriving (Eq, Show)
3939

4040
data IssueStatus = Open | Closed deriving (Eq, Show)
@@ -58,6 +58,7 @@ serverDomain ::
5858
GitServer ->
5959
Text
6060
serverDomain Github = "github.com"
61+
serverDomain Codeberg = "codeberg.org"
6162
serverDomain (Gitlab (GitlabHost h)) = h
6263

6364
-- | This regex represents a github/gitlab issue URL
@@ -75,6 +76,7 @@ extractIssuesOnALine lineContent = map f (RE.scan gitRepoRe lineContent)
7576
colNo = 1 + ByteString.length (fst $ ByteString.breakSubstring match lineContent)
7677
provider
7778
| domain == "github.com" = Github
79+
| domain == "codeberg.org" = Codeberg
7880
-- TODO: We suppose that all other cases are gitlab
7981
-- The only thing we risk here is a query with the wrong
8082
-- API to an irrelevant host.
@@ -109,6 +111,7 @@ issueUrl ::
109111
Req.Url 'Req.Https
110112
issueUrl issue = case server issue of
111113
Github -> Req.https "api.github.com" Req./: "repos" Req./: owner issue Req./: repo issue Req./: "issues" Req./~ issueNum issue
114+
Codeberg -> Req.https "codeberg.org" Req./: "api" Req./: "v1" Req./: "repos" Req./: owner issue Req./: repo issue Req./: "issues" Req./~ issueNum issue
112115
Gitlab (GitlabHost host) -> Req.https host Req./: "api" Req./: "v4" Req./: "projects" Req./: [fmt|{owner issue}/{repo issue}|] Req./: "issues" Req./~ issueNum issue
113116

114117
-- try Issue can fail, on non-2xx HTTP response
@@ -129,10 +132,14 @@ headersFor ::
129132
headersFor issue = do
130133
mGithubKey <- krankAsks githubKey
131134
mGitlabKeys <- krankAsks gitlabKeys
135+
mCodebergKey <- krankAsks codebergKey
132136
case server issue of
133137
Github -> case mGithubKey of
134138
Just (GithubKey token) -> pure $ Req.oAuth2Token (Text.Encoding.encodeUtf8 token)
135139
Nothing -> pure mempty
140+
Codeberg -> case mCodebergKey of
141+
Just (CodebergKey token) -> pure $ Req.oAuth2Token (Text.Encoding.encodeUtf8 token)
142+
Nothing -> pure mempty
136143
Gitlab host -> case Map.lookup host mGitlabKeys of
137144
Just (GitlabKey token) -> pure $ Req.header "PRIVATE-TOKEN" (Text.Encoding.encodeUtf8 token)
138145
Nothing -> pure mempty
@@ -150,6 +157,7 @@ showGitServerException ::
150157
Req.HttpException ->
151158
Text
152159
showGitServerException Github exc = showGithubException exc
160+
showGitServerException Codeberg exc = showGithubException exc
153161
showGitServerException (Gitlab _) exc = showGitlabException exc
154162

155163
restIssue ::

src/Krank/Types.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module Krank.Types
44
( GithubKey (..),
5+
CodebergKey(..),
56
GitlabHost (..),
67
GitlabKey (..),
78
Violation (..),
@@ -22,6 +23,8 @@ import qualified Network.HTTP.Req as Req
2223

2324
newtype GithubKey = GithubKey Text deriving (Show)
2425

26+
newtype CodebergKey = CodebergKey Text deriving (Show)
27+
2528
newtype GitlabKey = GitlabKey Text deriving (Show)
2629

2730
newtype GitlabHost = GitlabHost Text deriving (Show, Ord, Eq)
@@ -61,6 +64,8 @@ data KrankConfig = KrankConfig
6164
githubKey :: Maybe GithubKey,
6265
-- | The gitlab oAuth token
6366
gitlabKeys :: Map GitlabHost GitlabKey,
67+
-- | The codeberg host token
68+
codebergKey :: Maybe CodebergKey,
6469
-- | If 'True', all IO operations, such as HTTP requests, are ignored
6570
dryRun :: Bool,
6671
-- | Use color for formatting

0 commit comments

Comments
 (0)