Simplify Go Endpoints + Add Tests (#120)

This MR represents a major refactor of the Go codebase, as well as introducing tests for the handlers. The MR also introduces an endpoint to shutdown or restart the Go server, which may be useful for clients who want to refresh the state of the plugin after checking out branches. Finally, this MR adds a contributing document for users who want to make feature changes.
This commit is contained in:
Harrison (Harry) Cramer
2023-12-04 10:15:07 -05:00
committed by GitHub
parent 10b0b596ae
commit 93fe3e8bd6
41 changed files with 1745 additions and 728 deletions

View File

@@ -12,9 +12,14 @@ type ProjectMembersResponse struct {
ProjectMembers []*gitlab.ProjectMember
}
func ProjectMembersHandler(w http.ResponseWriter, r *http.Request) {
c := r.Context().Value("client").(Client)
/* projectMembersHandler returns all members of the current Gitlab project */
func (a *api) projectMembersHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodGet {
w.Header().Set("Access-Control-Allow-Methods", http.MethodGet)
handleError(w, InvalidRequestError{}, "Expected GET", http.StatusMethodNotAllowed)
return
}
projectMemberOptions := gitlab.ListProjectMembersOptions{
ListOptions: gitlab.ListOptions{
@@ -22,9 +27,16 @@ func ProjectMembersHandler(w http.ResponseWriter, r *http.Request) {
},
}
projectMembers, res, err := c.git.ProjectMembers.ListAllProjectMembers(c.projectId, &projectMemberOptions)
projectMembers, res, err := a.client.ListAllProjectMembers(a.projectInfo.ProjectId, &projectMemberOptions)
if err != nil {
c.handleError(w, err, "Could not fetch project users", res.StatusCode)
handleError(w, err, "Could not retrieve project members", http.StatusInternalServerError)
return
}
if res.StatusCode >= 300 {
handleError(w, GenericError{endpoint: "/project/members"}, "Could not retrieve project members", res.StatusCode)
return
}
w.WriteHeader(http.StatusOK)
@@ -32,13 +44,13 @@ func ProjectMembersHandler(w http.ResponseWriter, r *http.Request) {
response := ProjectMembersResponse{
SuccessResponse: SuccessResponse{
Status: http.StatusOK,
Message: "Project users fetched successfully",
Message: "Project members retrieved",
},
ProjectMembers: projectMembers,
}
err = json.NewEncoder(w).Encode(response)
if err != nil {
c.handleError(w, err, "Could not encode response", http.StatusInternalServerError)
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
}
}