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

feat: github orgs for /bake #41

Open
wants to merge 19 commits into
base: beta
Choose a base branch
from
Open
Changes from 1 commit
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
81 changes: 79 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"database/sql"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"

"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -54,8 +57,46 @@ func (p PizzaOvenServer) Run(serverPort string) {
}

type reqData struct {
URL string `json:"url"`
Wait bool `json:"wait,omitempty"`
URL string `json:"url,omitempty"`
Wait bool `json:"wait,omitempty"`
Org string `json:"org,omitempty"`
Archives bool `json:"archives,omitempty"`
}
mtfoley marked this conversation as resolved.
Show resolved Hide resolved

type orgRepo struct {
URL string `json:"html_url"`
Archived bool `json:"archived"`
}
type orgRepoList []orgRepo

func (p PizzaOvenServer) processOrg(orgUrlString string, processArchived bool) (orgRepoList, error) {
repoList := make(orgRepoList, 0)
orgUrl, err := url.Parse(orgUrlString)
if err != nil {
return repoList, err
}
if orgUrl.Hostname() == "github.com" {
orgApiUrlString := fmt.Sprintf("https://api.github.com/orgs%s/repos", orgUrl.Path)
res, err := http.Get(orgApiUrlString)
mtfoley marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return repoList, fmt.Errorf("Unable to request repo list from %s", orgApiUrlString)
}
body, err := io.ReadAll(res.Body)
res.Body.Close()
json.Unmarshal(body, &repoList)
if processArchived {
return repoList, nil
}
filteredRepoList := make(orgRepoList, 0)
for _, repo := range repoList {
if !repo.Archived {
filteredRepoList = append(filteredRepoList, repo)
}
}
return filteredRepoList, nil
} else {
return repoList, fmt.Errorf("Cannot parse organizations from %s", orgUrl.Hostname())
}
}

func (p PizzaOvenServer) handleRequest(w http.ResponseWriter, r *http.Request) {
Expand All @@ -75,13 +116,49 @@ func (p PizzaOvenServer) handleRequest(w http.ResponseWriter, r *http.Request) {

w.WriteHeader(http.StatusAccepted)
if data.Wait {
if data.Org != "" {
repoList, err := p.processOrg(data.Org, data.Archives)
if err != nil {
p.Logger.Errorf("Could not process org input: %v with error: %v", r.Body, err)
http.Error(w, "Could not process input", http.StatusInternalServerError)
return
}
for _, repo := range repoList {
err = p.processRepository(repo.URL)
}
return
}
err = p.processRepository(data.URL)
if err != nil {
p.Logger.Errorf("Could not process repository input: %v with error: %v", r.Body, err)
http.Error(w, "Could not process input", http.StatusInternalServerError)
return
}
} else {
if data.Org != "" {
repoList, err := p.processOrg(data.Org, data.Archives)
if err != nil {
p.Logger.Errorf("Could not process org input: %v with error: %v", r.Body, err)
http.Error(w, "Could not process input", http.StatusInternalServerError)
return
}
errors := make([]string, 0)
for _, repo := range repoList {
go func(repo orgRepo) {
err = p.processRepository(repo.URL)
if err != nil {
errors = append(errors, fmt.Sprintf("Could not process repo: %v with error: %v", repo, err))
}

}(repo)
}
if len(errors) > 0 {
errorString := strings.Join(errors, "\n")
p.Logger.Error(errorString)
http.Error(w, "Could not process input", http.StatusInternalServerError)
}
return
}
go func() {
err = p.processRepository(data.URL)
if err != nil {
Expand Down