Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List commits #11

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,65 @@ func handleDownloadRepo(w http.ResponseWriter, r *http.Request) {
}
}

func listCommitsForUser(giteaBaseURL, adminUsername, adminPassword, owner, repoName, branch string) ([]api.Commit, *errorapi.APIError) {
// Build the Gitea API URL for downloading the repo archive
url := fmt.Sprintf("%s/repos/%s/%s/commits?sha=%s&files=false", giteaBaseURL, owner, repoName, branch)

// Build request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("Error creating request %v", http.StatusInternalServerError)
return nil, errorapi.WrapError(errorapi.ErrBadRequest, err.Error())
}
req.SetBasicAuth(string(adminUsername), string(adminPassword))

// Send request
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("Error querying Gitea %v", http.StatusInternalServerError)
return nil, errorapi.WrapError(errorapi.ErrGiteaConnectError, err.Error())
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Printf("Error listing commits from Gitea %v", resp.StatusCode)
return nil, errorapi.WrapError(errorapi.ErrBadRequest, fmt.Sprintf("listing commits failed with status code: %d", resp.StatusCode))
}

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading Gitea response %v", err)
return nil, errorapi.WrapError(errorapi.ErrRequestReadError, err.Error())
}

var commits []api.Commit
err = json.Unmarshal(bodyBytes, &commits)
if err != nil {
log.Printf("Error reading Gitea response %v", err)
return nil, errorapi.WrapError(errorapi.ErrRequestParseError, err.Error())
}

return commits, nil
}

func handleListCommits(w http.ResponseWriter, r *http.Request) {
repoName := r.URL.Query().Get("name")
owner := r.URL.Query().Get("owner")
branch := r.URL.Query().Get("branch")

if repoName == "" || owner == "" || branch == "" {
http.Error(w, "Repo name, owner, and branch must be provided", http.StatusBadRequest)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of consistency, can you change this to errorapi.HandleError(w, errorapi.ErrBadRequest, "Repo name, owner, and branch must be provided")

return
}
if commits, err := listCommitsForUser(access.URL, access.Username, access.Password, owner, repoName, branch); err == nil {
jsonData, _ := json.Marshal(commits)
w.WriteHeader(http.StatusOK)
w.Write(jsonData)
} else {
errorapi.HandleError(w, errorapi.WrapError(err, "failed to list commits for repo"))
}
}

func deleteRepoForUser(giteaBaseURL, adminUsername, adminPassword, owner, repoName string) *errorapi.APIError {

// Build the Gitea API URL for fetching the repo details
Expand Down Expand Up @@ -2424,6 +2483,7 @@ func main() {
protected.HandleFunc("/repos/hooks", handleRepoHook)
protected.HandleFunc("/repos/modify", handleModifyRepoFiles).Methods("POST")
protected.HandleFunc("/repos/download", handleDownloadRepo).Methods("GET")
protected.HandleFunc("/repos/commits", handleListCommits).Methods("GET")
protected.HandleFunc("/forks", handleFork)
protected.HandleFunc("/orgs", handleOrg)
protected.HandleFunc("/orgs/{orgName}/members", handleGetMembers).Methods("GET")
Expand Down
Loading