Skip to content
Snippets Groups Projects
Commit 995612ab authored by Kenneth Tran's avatar Kenneth Tran
Browse files

Update strucs and improved handlerCommits etc

parent 2a6af9fc
No related branches found
No related tags found
No related merge requests found
main 0 → 100755
File added
package main
import (
"net/http"
A "oblig2/packs"
"log"
"net/http"
"os"
A"oblig2/packs"
)
func main(){
port := os.Getenv("PORT")
port = "8080"
......@@ -15,7 +17,5 @@ func main(){
}
A.GetCommits()
log.Fatal(http.ListenAndServe(":"+port, nil))
// B.GetIssues()
// C.GetLanguages()
}
......@@ -9,76 +9,56 @@ import (
"strconv"
)
const URL = "https://gitlab.example.com/api/v4/projects"
const APIROOT= "https://git.gvk.idi.ntnu.no/api/v4/projects/"
const Version = "v1"
type Repo1 struct { //struct repo with most commits
// struct with repodata
type Repo1 struct {
Id int `json: "id"`
MostCommits string `json:"path_with_namespace"`
Counter Counter
// Counter1 //convert to Jsonformat
}
Name string `json: "name_with_namespace"`
Path string `json: "path_with_namespace"`
Counter int `json:"commits"`
type Repo2 struct { // struct repo with second most commits
SecondCommits string `json:"path_with_namespace"`
// Counter2 int //convert to Jsonformat
}
// struct with string of id
type Counter struct {
Counter int `json:"counter"`
}
type MergeRep struct {
Repo1 Repo1
Repo2 Repo2
Id string `json:"id"`
}
func GetCommits(){
http.HandleFunc("/repocheck/v1/commits", handlerCommits)
}
func countMost(w http.ResponseWriter, r *http.Request, pointer* []Repo1 , pointer2* []Counter){
for j := 0; j <= len(*pointer); j++ {
spesificPro := strconv.Itoa((*pointer)[j].Id)
url := URL + spesificPro + "/repository/commits"
response, err := http.Get(url)
if err != nil { //error handler
fmt.Println("Error: we have a problem", err)
http.HandleFunc("/repocheck/v1/commits/", handlerCommits)
}
// create an array of id`s
var buffer []Counter
err = json.NewDecoder(response.Body).Decode(&buffer)
counter := len(buffer)
(*pointer2)[j].Counter = counter
func countCommits(w http.ResponseWriter, r *http.Request, Repos* []Repo1) {
for i := 0; i < len(*Repos); i++ {
proId := strconv.Itoa((*Repos)[i].Id) // convert to string
url := APIROOT + proId + "/repository/commits/"
response, _ := http.Get(url)
var content []Counter
json.NewDecoder(response.Body).Decode(&content)
count := len(content)
(*Repos)[i].Counter = count //update variable Counter in Repo1 struct
}
}
func handlerCommits(w http.ResponseWriter, r *http.Request) {
response, err := http.Get(URL)
if err != nil { //error handler
response, err := http.Get(APIROOT)
// url = apiroot + "?per_page" + limit
if err != nil {
fmt.Println("Error: we have a problem", err)
return
}
var content Repo1
var content2 Repo2
var merged MergeRep
countMost(w,r,&content, &Repo2)
var content []Repo1
err = json.NewDecoder(response.Body).Decode(&content)
countCommits(w,r,&content)
merged.Repo1 = content
merged.Repo2 = content2
var buffer = new(bytes.Buffer)
encode := json.NewEncoder(buffer)
encode.Encode(merged)
encode.Encode(content)
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusAccepted)
io.Copy(w, buffer)
}
package packs
package packs
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment