woodpecker/server/forge/gitea/fixtures/handler.go

211 lines
5.1 KiB
Go
Raw Normal View History

2018-02-19 22:24:10 +00:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2017-05-01 10:33:06 +00:00
package fixtures
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Handler returns an http.Handler that is capable of handling a variety of mock
2017-05-01 13:48:18 +00:00
// Gitea requests and returning mock responses.
2017-05-01 10:33:06 +00:00
func Handler() http.Handler {
gin.SetMode(gin.TestMode)
e := gin.New()
e.GET("/api/v1/repos/:owner/:name", getRepo)
e.GET("/api/v1/repositories/:id", getRepoByID)
e.GET("/api/v1/repos/:owner/:name/raw/:file", getRepoFile)
2017-05-01 10:33:06 +00:00
e.POST("/api/v1/repos/:owner/:name/hooks", createRepoHook)
e.GET("/api/v1/repos/:owner/:name/hooks", listRepoHooks)
e.DELETE("/api/v1/repos/:owner/:name/hooks/:id", deleteRepoHook)
2017-05-01 12:51:44 +00:00
e.POST("/api/v1/repos/:owner/:name/statuses/:commit", createRepoCommitStatus)
e.GET("/api/v1/repos/:owner/:name/pulls/:index/files", getPRFiles)
2017-05-01 10:33:06 +00:00
e.GET("/api/v1/user/repos", getUserRepos)
2020-09-30 11:27:34 +00:00
e.GET("/api/v1/version", getVersion)
2017-05-01 10:33:06 +00:00
return e
}
func listRepoHooks(c *gin.Context) {
page := c.Query("page")
if page != "" && page != "1" {
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, "[]")
} else {
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, listRepoHookPayloads)
}
}
2017-05-01 10:33:06 +00:00
func getRepo(c *gin.Context) {
switch c.Param("name") {
case "repo_not_found":
2024-03-15 17:00:25 +00:00
c.String(http.StatusNotFound, "")
2017-05-01 10:33:06 +00:00
default:
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, repoPayload)
2017-05-01 10:33:06 +00:00
}
}
func getRepoByID(c *gin.Context) {
switch c.Param("id") {
case "repo_not_found":
2024-03-15 17:00:25 +00:00
c.String(http.StatusNotFound, "")
default:
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, repoPayload)
}
}
2017-05-01 12:51:44 +00:00
func createRepoCommitStatus(c *gin.Context) {
if c.Param("commit") == "v1.0.0" || c.Param("commit") == "9ecad50" {
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, repoPayload)
2017-05-01 12:51:44 +00:00
}
2024-03-15 17:00:25 +00:00
c.String(http.StatusNotFound, "")
2017-05-01 12:51:44 +00:00
}
2017-05-01 10:33:06 +00:00
func getRepoFile(c *gin.Context) {
file := c.Param("file")
ref := c.Query("ref")
if file == "file_not_found" {
2024-03-15 17:00:25 +00:00
c.String(http.StatusNotFound, "")
2017-05-01 10:33:06 +00:00
}
if ref == "v1.0.0" || ref == "9ecad50" {
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, repoFilePayload)
2017-05-01 10:33:06 +00:00
}
2024-03-15 17:00:25 +00:00
c.String(http.StatusNotFound, "")
2017-05-01 10:33:06 +00:00
}
func createRepoHook(c *gin.Context) {
in := struct {
Type string `json:"type"`
Conf struct {
Type string `json:"content_type"`
URL string `json:"url"`
} `json:"config"`
}{}
_ = c.BindJSON(&in)
2017-05-01 14:43:23 +00:00
if in.Type != "gitea" ||
2017-05-01 10:33:06 +00:00
in.Conf.Type != "json" ||
in.Conf.URL != "http://localhost" {
2024-03-15 17:00:25 +00:00
c.String(http.StatusInternalServerError, "")
2017-05-01 10:33:06 +00:00
return
}
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, "{}")
2017-05-01 10:33:06 +00:00
}
func deleteRepoHook(c *gin.Context) {
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, "{}")
}
2017-05-01 10:33:06 +00:00
func getUserRepos(c *gin.Context) {
switch c.Request.Header.Get("Authorization") {
case "token repos_not_found":
2024-03-15 17:00:25 +00:00
c.String(http.StatusNotFound, "")
2017-05-01 10:33:06 +00:00
default:
page := c.Query("page")
if page != "" && page != "1" {
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, "[]")
} else {
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, userRepoPayload)
}
2017-05-01 10:33:06 +00:00
}
}
2020-09-30 11:27:34 +00:00
func getVersion(c *gin.Context) {
2024-03-15 17:00:25 +00:00
c.JSON(http.StatusOK, map[string]any{"version": "1.18.0"})
}
func getPRFiles(c *gin.Context) {
page := c.Query("page")
if page == "1" {
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, prFilesPayload)
} else {
2024-03-15 17:00:25 +00:00
c.String(http.StatusOK, "[]")
}
2020-09-30 11:27:34 +00:00
}
const listRepoHookPayloads = `
[
{
"id": 1,
"type": "gitea",
"config": {
"content_type": "json",
"url": "http:\/\/localhost\/hook?access_token=1234567890"
}
}
]
`
2017-05-01 10:33:06 +00:00
const repoPayload = `
{
"id": 5,
2017-05-01 10:33:06 +00:00
"owner": {
2017-05-01 12:51:44 +00:00
"login": "test_name",
2017-05-01 10:33:06 +00:00
"email": "octocat@github.com",
"avatar_url": "https:\/\/secure.gravatar.com\/avatar\/8c58a0be77ee441bb8f8595b7f1b4e87"
},
"full_name": "test_name\/repo_name",
"private": true,
"html_url": "http:\/\/localhost\/test_name\/repo_name",
"clone_url": "http:\/\/localhost\/test_name\/repo_name.git",
"permissions": {
"admin": true,
"push": true,
"pull": true
}
}
`
const repoFilePayload = `{ platform: linux/amd64 }`
const userRepoPayload = `
[
{
"id": 5,
2017-05-01 10:33:06 +00:00
"owner": {
2017-05-01 12:51:44 +00:00
"login": "test_name",
2017-05-01 10:33:06 +00:00
"email": "octocat@github.com",
"avatar_url": "https:\/\/secure.gravatar.com\/avatar\/8c58a0be77ee441bb8f8595b7f1b4e87"
},
"full_name": "test_name\/repo_name",
"private": true,
"html_url": "http:\/\/localhost\/test_name\/repo_name",
"clone_url": "http:\/\/localhost\/test_name\/repo_name.git",
"permissions": {
"admin": true,
"push": true,
"pull": true
}
}
]
`
const prFilesPayload = `
[
{
"filename": "README.md",
"status": "changed",
"additions": 2,
"deletions": 0,
"changes": 2,
"html_url": "http://localhost/username/repo/src/commit/e79e4b0e8d9dd6f72b70e776c3317db7c19ca0fd/README.md",
"contents_url": "http://localhost:3000/api/v1/repos/username/repo/contents/README.md?ref=e79e4b0e8d9dd6f72b70e776c3317db7c19ca0fd",
"raw_url": "http://localhost/username/repo/raw/commit/e79e4b0e8d9dd6f72b70e776c3317db7c19ca0fd/README.md"
}
]
`