refactor: refactor GitHub Actions API Routing

- Refactor API routing for actions, secrets, variables, and runners into a new `actionsGroup` function
- Introduce a new `actionAPI` struct to encapsulate action-related API functions

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2024-04-23 15:59:40 +08:00
parent 40d720f574
commit b05f712cbc
No known key found for this signature in database
2 changed files with 72 additions and 43 deletions

View File

@ -1073,27 +1073,21 @@ func Routes() *web.Route {
m.Post("/accept", repo.AcceptTransfer) m.Post("/accept", repo.AcceptTransfer)
m.Post("/reject", repo.RejectTransfer) m.Post("/reject", repo.RejectTransfer)
}, reqToken()) }, reqToken())
m.Group("/actions", func() { actionsGroup(
m.Group("/secrets", func() { m,
m.Get("", reqToken(), reqOwner(), repo.ListActionsSecrets) reqOwner(),
m.Combo("/{secretname}"). actionAPI{
Put(reqToken(), reqOwner(), bind(api.CreateOrUpdateSecretOption{}), repo.CreateOrUpdateSecret). repo.ListActionsSecrets,
Delete(reqToken(), reqOwner(), repo.DeleteSecret) repo.CreateOrUpdateSecret,
}) repo.DeleteSecret,
repo.ListVariables,
m.Group("/variables", func() { repo.GetVariable,
m.Get("", reqToken(), reqOwner(), repo.ListVariables) repo.DeleteVariable,
m.Combo("/{variablename}"). repo.CreateVariable,
Get(reqToken(), reqOwner(), repo.GetVariable). repo.UpdateVariable,
Delete(reqToken(), reqOwner(), repo.DeleteVariable). repo.GetRegistrationToken,
Post(reqToken(), reqOwner(), bind(api.CreateVariableOption{}), repo.CreateVariable). },
Put(reqToken(), reqOwner(), bind(api.UpdateVariableOption{}), repo.UpdateVariable) )
})
m.Group("/runners", func() {
m.Get("/registration-token", reqToken(), reqOwner(), repo.GetRegistrationToken)
})
})
m.Group("/hooks/git", func() { m.Group("/hooks/git", func() {
m.Combo("").Get(repo.ListGitHooks) m.Combo("").Get(repo.ListGitHooks)
m.Group("/{id}", func() { m.Group("/{id}", func() {
@ -1461,27 +1455,21 @@ func Routes() *web.Route {
m.Combo("/{username}").Get(reqToken(), org.IsMember). m.Combo("/{username}").Get(reqToken(), org.IsMember).
Delete(reqToken(), reqOrgOwnership(), org.DeleteMember) Delete(reqToken(), reqOrgOwnership(), org.DeleteMember)
}) })
m.Group("/actions", func() { actionsGroup(
m.Group("/secrets", func() { m,
m.Get("", reqToken(), reqOrgOwnership(), org.ListActionsSecrets) reqOrgOwnership(),
m.Combo("/{secretname}"). actionAPI{
Put(reqToken(), reqOrgOwnership(), bind(api.CreateOrUpdateSecretOption{}), org.CreateOrUpdateSecret). org.ListActionsSecrets,
Delete(reqToken(), reqOrgOwnership(), org.DeleteSecret) org.CreateOrUpdateSecret,
}) org.DeleteSecret,
org.ListVariables,
m.Group("/variables", func() { org.GetVariable,
m.Get("", reqToken(), reqOrgOwnership(), org.ListVariables) org.DeleteVariable,
m.Combo("/{variablename}"). org.CreateVariable,
Get(reqToken(), reqOrgOwnership(), org.GetVariable). org.UpdateVariable,
Delete(reqToken(), reqOrgOwnership(), org.DeleteVariable). org.GetRegistrationToken,
Post(reqToken(), reqOrgOwnership(), bind(api.CreateVariableOption{}), org.CreateVariable). },
Put(reqToken(), reqOrgOwnership(), bind(api.UpdateVariableOption{}), org.UpdateVariable) )
})
m.Group("/runners", func() {
m.Get("/registration-token", reqToken(), reqOrgOwnership(), org.GetRegistrationToken)
})
})
m.Group("/public_members", func() { m.Group("/public_members", func() {
m.Get("", org.ListPublicMembers) m.Get("", org.ListPublicMembers)
m.Combo("/{username}").Get(org.IsPublicMember). m.Combo("/{username}").Get(org.IsPublicMember).
@ -1597,6 +1585,47 @@ func Routes() *web.Route {
return m return m
} }
// actionAPI is a struct that holds the actions API
type actionAPI struct {
ListActionsSecrets func(ctx *context.APIContext)
CreateOrUpdateSecret func(ctx *context.APIContext)
DeleteSecret func(ctx *context.APIContext)
ListVariables func(ctx *context.APIContext)
GetVariable func(ctx *context.APIContext)
DeleteVariable func(ctx *context.APIContext)
CreateVariable func(ctx *context.APIContext)
UpdateVariable func(ctx *context.APIContext)
GetRegistrationToken func(ctx *context.APIContext)
}
func actionsGroup(
m *web.Route,
reqChecker func(ctx *context.APIContext),
act actionAPI,
) {
m.Group("/actions", func() {
m.Group("/secrets", func() {
m.Get("", reqToken(), reqChecker, act.ListActionsSecrets)
m.Combo("/{secretname}").
Put(reqToken(), reqChecker, bind(api.CreateOrUpdateSecretOption{}), act.CreateOrUpdateSecret).
Delete(reqToken(), reqChecker, act.DeleteSecret)
})
m.Group("/variables", func() {
m.Get("", reqToken(), reqChecker, act.ListVariables)
m.Combo("/{variablename}").
Get(reqToken(), reqChecker, act.GetVariable).
Delete(reqToken(), reqChecker, act.DeleteVariable).
Post(reqToken(), reqChecker, bind(api.CreateVariableOption{}), act.CreateVariable).
Put(reqToken(), reqChecker, bind(api.UpdateVariableOption{}), act.UpdateVariable)
})
m.Group("/runners", func() {
m.Get("/registration-token", reqToken(), reqChecker, act.GetRegistrationToken)
})
})
}
func securityHeaders() func(http.Handler) http.Handler { func securityHeaders() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {

View File

@ -25679,4 +25679,4 @@
"TOTPHeader": [] "TOTPHeader": []
} }
] ]
} }