-
Notifications
You must be signed in to change notification settings - Fork 28
/
get_id.go
117 lines (95 loc) · 2.93 KB
/
get_id.go
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
// SPDX-License-Identifier: Apache-2.0
package build
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)
// swagger:operation GET /api/v1/search/builds/{id} builds GetBuildByID
//
// Get a single build by its id in the configured backend
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: id
// description: build id
// required: true
// type: number
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved build
// schema:
// "$ref": "#/definitions/Build"
// '400':
// description: Unable to retrieve the build
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to retrieve the build
// schema:
// "$ref": "#/definitions/Error"
// GetBuildByID represents the API handler to capture a
// build by its id from the configured backend.
func GetBuildByID(c *gin.Context) {
// Variables that will hold the library types of the build and repo
var (
b *library.Build
r *library.Repo
)
// Capture user from middleware
u := user.Retrieve(c)
ctx := c.Request.Context()
// Parse build ID from path
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
retErr := fmt.Errorf("unable to parse build id: %w", err)
util.HandleError(c, http.StatusBadRequest, retErr)
return
}
// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"build": id,
"user": u.GetName(),
}).Infof("reading build %d", id)
// Get build from database
b, err = database.FromContext(c).GetBuild(ctx, id)
if err != nil {
retErr := fmt.Errorf("unable to get build: %w", err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
// Get repo from database using repo ID field from build
r, err = database.FromContext(c).GetRepo(ctx, b.GetRepoID())
if err != nil {
retErr := fmt.Errorf("unable to get repo: %w", err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
// Capture user access from SCM. We do this in order to ensure user has access and is not
// just retrieving any build using a random id number.
perm, err := scm.FromContext(c).RepoAccess(ctx, u.GetName(), u.GetToken(), r.GetOrg(), r.GetName())
if err != nil {
logrus.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName())
}
// Ensure that user has at least read access to repo to return the build
if perm == "none" && !u.GetAdmin() {
retErr := fmt.Errorf("unable to retrieve build %d: user does not have read access to repo", id)
util.HandleError(c, http.StatusUnauthorized, retErr)
return
}
c.JSON(http.StatusOK, b)
}