|
| 1 | +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by the LICENSE file in this repository. |
| 4 | + |
| 5 | +package worker |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/gin-gonic/gin" |
| 14 | + "github.com/go-vela/types" |
| 15 | + "github.com/go-vela/types/library" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // BuildResp represents a JSON return for a single build. |
| 20 | + BuildResp = `{ |
| 21 | + "id": 1, |
| 22 | + "repo_id": 1, |
| 23 | + "number": 1, |
| 24 | + "parent": 1, |
| 25 | + "event": "push", |
| 26 | + "status": "created", |
| 27 | + "error": "", |
| 28 | + "enqueued": 1563474077, |
| 29 | + "created": 1563474076, |
| 30 | + "started": 1563474077, |
| 31 | + "finished": 0, |
| 32 | + "deploy": "", |
| 33 | + "clone": "https://github.com/github/octocat.git", |
| 34 | + "source": "https://github.com/github/octocat/commit/48afb5bdc41ad69bf22588491333f7cf71135163", |
| 35 | + "title": "push received from https://github.com/github/octocat", |
| 36 | + "message": "First commit...", |
| 37 | + "commit": "48afb5bdc41ad69bf22588491333f7cf71135163", |
| 38 | + "sender": "OctoKitty", |
| 39 | + "author": "OctoKitty", |
| 40 | + |
| 41 | + "link": "https://vela.example.company.com/github/octocat/1", |
| 42 | + "branch": "master", |
| 43 | + "ref": "refs/heads/master", |
| 44 | + "base_ref": "", |
| 45 | + "host": "example.company.com", |
| 46 | + "runtime": "docker", |
| 47 | + "distribution": "linux" |
| 48 | +}` |
| 49 | +) |
| 50 | + |
| 51 | +// getBuild has a param :build returns mock JSON for a http GET. |
| 52 | +func getBuild(c *gin.Context) { |
| 53 | + b := c.Param("build") |
| 54 | + |
| 55 | + if strings.EqualFold(b, "0") { |
| 56 | + msg := fmt.Sprintf("Build %s does not exist", b) |
| 57 | + |
| 58 | + c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg}) |
| 59 | + |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + data := []byte(BuildResp) |
| 64 | + |
| 65 | + var body library.Build |
| 66 | + _ = json.Unmarshal(data, &body) |
| 67 | + |
| 68 | + c.JSON(http.StatusOK, body) |
| 69 | +} |
| 70 | + |
| 71 | +// cancelBuild has a param :build returns mock JSON for a http DELETE. |
| 72 | +// |
| 73 | +// Pass "0" to :build to test receiving a http 404 response. |
| 74 | +func cancelBuild(c *gin.Context) { |
| 75 | + b := c.Param("build") |
| 76 | + |
| 77 | + if strings.EqualFold(b, "0") { |
| 78 | + msg := fmt.Sprintf("Build %s does not exist", b) |
| 79 | + |
| 80 | + c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg}) |
| 81 | + |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + c.JSON(http.StatusOK, BuildResp) |
| 86 | +} |
0 commit comments