From 8611e7eba157171db38e9dfb9aa479e24d55c121 Mon Sep 17 00:00:00 2001 From: Nicola Zangrandi Date: Tue, 14 Jan 2025 16:20:53 +0100 Subject: [PATCH] Initial Forgejo API interaction --- .gitignore | 2 +- fj.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ go.sum | 0 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 fj.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index 5b90e79..432502c 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,4 @@ go.work.sum # env file .env - +.envrc diff --git a/fj.go b/fj.go new file mode 100644 index 0000000..289c263 --- /dev/null +++ b/fj.go @@ -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) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ec45cb2 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module tehga.me/forgejo/wasp/fj + +go 1.23.4 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29