Initial Forgejo API interaction

This commit is contained in:
Nicola Zangrandi 2025-01-14 16:20:53 +01:00
parent 7089c1de4d
commit d99f2143a4
No known key found for this signature in database
GPG key ID: 3C640B6656B9495F
4 changed files with 65 additions and 1 deletions

2
.gitignore vendored
View file

@ -24,4 +24,4 @@ go.work.sum
# env file # env file
.env .env
.envrc

61
fj.go Normal file
View file

@ -0,0 +1,61 @@
package main
import (
"encoding/json"
"log"
"os"
"net/http"
// "github.com/go-git/go-git/v5"
// "github.com/go-git/go-git/v5/storage/memory"
)
type ForgejoRepository struct {
ID int `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
HTMLURL string `json:"html_url"`
SSHURL string `json:"ssh_url"`
CloneURL string `json:"clone_url"`
}
var API_URL = "/api/v1"
var API_USER_REPOS = "/user/repos"
func buildApiUrl(host string, token string, api string) string {
return host + API_URL + api + "?token=" + token + "&limit=1000"
}
func buildApiUrlUserRepos(host string, token string) string {
return buildApiUrl(host, token, API_USER_REPOS)
}
func main() {
// Get the environment variables for Forgejo URL and api token
FJ_HOST := os.Getenv("FJ_HOST")
FJ_API_TOKEN := os.Getenv("FJ_API_TOKEN")
if FJ_HOST == "" || FJ_API_TOKEN == "" {
log.Fatalln("Please configure the FJ_HOST and FJ_API_TOKEN environment variables to use fj.")
return
}
// Get the master repo list from Forgejo by calling the user/repos endpoint
repos_json, err := http.Get(buildApiUrlUserRepos(FJ_HOST, FJ_API_TOKEN))
if err != nil {
log.Fatalln(err)
return
}
// Deserialize the json into a slice of Repository structs
var repos []ForgejoRepository
err = json.NewDecoder(repos_json.Body).Decode(&repos)
if err != nil {
log.Fatalln(err)
return
}
for _, repo := range repos {
log.Println(repo.Name + " " + repo.CloneURL)
}
}

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module tehga.me/forgejo/wasp/fj
go 1.23.4

0
go.sum Normal file
View file