Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/helxplatform/gitea-assist
Browse files Browse the repository at this point in the history
…into list-commits
  • Loading branch information
frostyfan109 committed Aug 12, 2024
2 parents af5ce0a + 166f0a2 commit 10e3cf3
Show file tree
Hide file tree
Showing 4 changed files with 466 additions and 440 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ The application reads Gitea credentials from the following files:
- `/etc/assist-secret/gitea-username` for the username.
- `/etc/assist-secret/gitea-password` for the password.

Ensure these files are present and contain the necessary credentials for accessing Gitea repositories.
The application expects a Bearer Token for use of gitea functionality which is supplied by:
- `/etc/assist-secret/assist-token` for the assist auth token.

Ensure these files are present and contain the necessary credentials for accessing Gitea repositories. These can be created by using the mk_passwd.py script

## Endpoints

- `/onPush`: Endpoint to receive Gitea push webhooks.
- `/readiness`: A readiness endpoint that returns 200 OK, indicating the service is ready to handle requests.
- `/liveness`: A liveness endpoint that returns 200 OK, indicating the service is alive and healthy.
- `/users`: Endpoint to handle users in Gitea instance
- `/users/ssh`: Endpoint to manage ssh keys for Gitea
- `/repos`: Endpoint to manage repo list

An example curl command needed to hit any of the endpoints is:
`curl -H "Authorization: Bearer <assist-token_from_secret>" -X GET "http://<server_ip>:9000/users?username=<user_name>"`

## Running the Application

Expand All @@ -49,7 +58,7 @@ However, you can specify custom values for both.

### Usage

- **Default (random password, `gitea_admin` username):**
- **Default (random password, `gitea_admin` username and `assist-token`):**
```
./mk_passwd.py
```
Expand All @@ -64,9 +73,9 @@ However, you can specify custom values for both.
./mk_passwd.py --username <your_predefined_username>
```

- **Specify both password and username:**
- **Specify both password, username and token:**
```
./mk_passwd.py --password <your_predefined_password> --username <your_predefined_username>
./mk_passwd.py --password <your_predefined_password> --username <your_predefined_username> --token <your_predefined_token>
```

### Behavior
Expand Down
61 changes: 61 additions & 0 deletions error/errorapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package errorapi

import (
"errors"
"fmt"
"log"
"net/http"
"strings"
)

type APIError struct {
error
status int
}

func (e APIError) Error() string {
msg := e.error.Error()
// The join method while wrapping concatenates error messages with newlines. Replace newlines with spaces
return strings.Replace(msg, "\n", " ", -1)
}

func (e APIError) Compare(err *APIError) bool {
return errors.Is(e.error, err.error)
}

var (
// A list of general error of type APIError
ErrBadRequest = &APIError{error: errors.New("bad request"), status: http.StatusBadRequest}
ErrNotFound = &APIError{error: errors.New("not found"), status: http.StatusNotFound}
ErrInternalServerError = &APIError{error: errors.New("internal server error"), status: http.StatusInternalServerError}
ErrRequestReadError = &APIError{error: errors.New("error reading request body"), status: http.StatusBadRequest}
ErrResponseReadError = &APIError{error: errors.New("error reading response body"), status: http.StatusBadRequest}
ErrRequestParseError = &APIError{error: errors.New("request parse error"), status: http.StatusBadRequest}
ErrMethodNotAllowed = &APIError{error: errors.New("method not allowed"), status: http.StatusMethodNotAllowed}
ErrGiteaConnectError = &APIError{error: errors.New("error connecting to gitea"), status: http.StatusBadRequest}
ErrUnauthorized = &APIError{error: errors.New("unauthorized attempt to login"), status: http.StatusUnauthorized}
// Making a slice for all predefined errors for ease of comparison in HandleError below
allErrors = []*APIError{ErrBadRequest, ErrNotFound, ErrInternalServerError, ErrRequestReadError, ErrResponseReadError, ErrMethodNotAllowed, ErrRequestParseError, ErrMethodNotAllowed, ErrGiteaConnectError, ErrUnauthorized}
)

// This function provides capability to "modify" the message of an existing error
// but still keep the original type when comparing
// For example:
// repoError := WrapError(ErrNotFound, "Repo") :: Creates a new error specific to repo
// But now, repoError.Compare(ErrNotFound) will return true because of the Join method of errors package
func WrapError(e *APIError, msg string) *APIError {
err := errors.Join(fmt.Errorf("%v", msg), e.error)
return &APIError{error: err, status: e.status}
}

func HandleError(w http.ResponseWriter, err *APIError) {
for _, sperr := range allErrors {
if err.Compare(sperr) {
http.Error(w, err.Error(), err.status)
log.Printf("ERROR: %s STATUS: %d\n", err.Error(), err.status)
return
}
}
http.Error(w, ErrInternalServerError.Error(), ErrInternalServerError.status)
log.Printf("Unknown error occured %v", err)
}
Loading

0 comments on commit 10e3cf3

Please sign in to comment.