You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

93 lines
1.9 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "codeberg.org/Rinma/vcs-profile-html-generator/types"
  6. "encoding/json"
  7. "flag"
  8. "fmt"
  9. "html/template"
  10. "io/ioutil"
  11. "net/http"
  12. "os"
  13. )
  14. func createJSONQueryString(queryString string) string {
  15. return fmt.Sprintf("{\"query\": \"%s\"}", queryString)
  16. }
  17. func closeFile(file *os.File) {
  18. err := file.Close()
  19. if err != nil {
  20. panic(err)
  21. }
  22. }
  23. func loadGitHubQuery(queryPath string) (string, error) {
  24. file, err := os.Open(queryPath)
  25. if err != nil {
  26. return "", err
  27. }
  28. defer closeFile(file)
  29. var lines string
  30. scanner := bufio.NewScanner(file)
  31. for scanner.Scan() {
  32. lines += scanner.Text()
  33. }
  34. lines = createJSONQueryString(lines)
  35. fmt.Println("Query:", lines)
  36. return lines, scanner.Err()
  37. }
  38. func bodyClose(resp *http.Response) {
  39. err := resp.Body.Close()
  40. if err != nil {
  41. panic(err)
  42. }
  43. }
  44. func callApi(githubToken *string) ([]byte, error) {
  45. var c = &http.Client{}
  46. var githubApiUrl = "https://api.github.com/graphql"
  47. query, _ := loadGitHubQuery("data/github-query.graphql")
  48. req, _ := http.NewRequest("POST", githubApiUrl, bytes.NewBuffer([]byte(query)))
  49. var tokenHeader = fmt.Sprintf("bearer %s", *githubToken)
  50. req.Header.Set("Authorization", tokenHeader)
  51. resp, _ := c.Do(req)
  52. defer bodyClose(resp)
  53. body, err := ioutil.ReadAll(resp.Body)
  54. fmt.Println("response Status:", resp.Status)
  55. fmt.Println("response Headers:", resp.Header)
  56. fmt.Println("response Body:", string(body))
  57. return body, err
  58. }
  59. func main() {
  60. var githubToken = flag.String("github_api_token", "123456789", "Token to connect to the GitHup Api")
  61. flag.Parse()
  62. body, _ := callApi(githubToken)
  63. var dat = types.GitHubData{}
  64. if err := json.Unmarshal(body, &dat); err != nil {
  65. panic(err)
  66. }
  67. f, _ := os.Create("vcs-repos.html")
  68. tmpl, _ := template.ParseFiles("templates/github.html")
  69. err := tmpl.Execute(f, dat.Data.Viewer)
  70. if err != nil {
  71. panic(err)
  72. }
  73. defer closeFile(f)
  74. }