]> khamidou — GitHub mirrors - prguy.git/commitdiff
add more files
authorKarim Hamidou <karim@khamidou.com>
Wed, 26 Jun 2024 04:19:39 +0000 (21:19 -0700)
committerKarim Hamidou <karim@khamidou.com>
Wed, 26 Jun 2024 04:19:39 +0000 (21:19 -0700)
cmd/prguy/config.go [new file with mode: 0644]
cmd/prguy/repos.go [new file with mode: 0644]

diff --git a/cmd/prguy/config.go b/cmd/prguy/config.go
new file mode 100644 (file)
index 0000000..46566ef
--- /dev/null
@@ -0,0 +1,68 @@
+package main
+
+import (
+       "encoding/json"
+       "io/ioutil"
+       "os"
+       "path/filepath"
+       "time"
+)
+
+type Config struct {
+       OAuthToken      string
+       Scope           string
+       LastUpdatedTime string
+}
+
+func (c *Config) getConfigPath() (string, error) {
+       dirname, err := os.UserHomeDir()
+       if err != nil {
+               return "", err
+       }
+
+       return filepath.Join(dirname, ".prguy"), nil
+}
+
+func (c *Config) exists() bool {
+       configPath, err := c.getConfigPath()
+       _, err = os.Stat(configPath)
+       return err == nil
+}
+
+func (c *Config) save() error {
+       configPath, err := c.getConfigPath()
+       file, err := os.Create(configPath)
+       if err != nil {
+               return err
+       }
+
+       defer file.Close()
+       c.LastUpdatedTime = time.Now().String()
+       jsonData, err := json.MarshalIndent(c, "", "  ")
+       if err != nil {
+               return err
+       }
+
+       _, err = file.Write(jsonData)
+       if err != nil {
+               return err
+       }
+
+       return nil
+}
+
+func (c *Config) load() error {
+       configPath, err := c.getConfigPath()
+       file, err := os.Open(configPath)
+       if err != nil {
+               return err
+       }
+
+       defer file.Close()
+       jsonData, err := ioutil.ReadAll(file)
+       if err != nil {
+               return err
+       }
+
+       return json.Unmarshal(jsonData, c)
+}
diff --git a/cmd/prguy/repos.go b/cmd/prguy/repos.go
new file mode 100644 (file)
index 0000000..1d1f15e
--- /dev/null
@@ -0,0 +1,134 @@
+package main
+
+import (
+       "encoding/json"
+       "errors"
+       "fmt"
+       "io/ioutil"
+       "net/http"
+)
+
+type buildStatus int
+
+const (
+       buildPending = iota
+       buildSuccess
+       buildFailure
+       buildCanceled
+)
+
+type pullRequest struct {
+       url    string
+       title  string
+    mergeable bool
+}
+
+func listUserPRs(token string) ([]pullRequest, []pullRequest, error) {
+       githubUrl := "https://api.github.com/notifications?participating=true&all=true"
+       req, err := http.NewRequest("GET", githubUrl, nil)
+       if err != nil {
+               return nil, nil, err
+       }
+
+       req.Header.Set("Authorization", "Bearer "+token)
+       client := &http.Client{}
+       resp, err := client.Do(req)
+       if err != nil {
+               return nil, nil, err
+       }
+
+       if resp.StatusCode != 200 {
+               msg := fmt.Sprintf("Got a '%s'error from the Github API. Please retry in a bit.",
+                       resp.Status)
+               return nil, nil, errors.New(msg)
+       }
+
+       defer resp.Body.Close()
+
+       body, err := ioutil.ReadAll(resp.Body)
+       if err != nil {
+               return nil, nil, err
+       }
+
+       var myPRs []pullRequest
+    var otherPRs []pullRequest
+
+       var jsonData []map[string]interface{}
+    var PRsSeen = make(map[string]bool)
+
+       err = json.Unmarshal(body, &jsonData)
+       if err != nil {
+               return nil, nil, err
+       }
+
+       for _, pr := range jsonData {
+               reason := pr["reason"].(string)
+               if reason != "author" || reason != "review_requested" {
+                       continue
+               }
+
+        fmt.Println(pr)
+
+               subject := pr["subject"].(map[string]interface{})
+        prAPIUrl := subject["url"].(string)
+        prData, err := fetchOneRESTObject(prAPIUrl, token)
+        if err != nil {
+            return nil, nil, err
+        }
+
+        prUrl := prData["_links"].(map[string]interface{})["html"].(map[string]interface{})["href"].(string)
+        if _, ok := PRsSeen[prUrl]; ok {
+            continue
+        }
+
+        PRsSeen[prUrl] = true
+        if reason == "author" {
+            myPRs = append(myPRs, pullRequest{
+                url:   prUrl,
+                title: subject["title"].(string),
+                mergeable: prData["mergeable"].(bool),
+            })
+        } else {
+            otherPRs = append(otherPRs, pullRequest{
+                url:   prUrl,
+                title: subject["title"].(string),
+                mergeable: prData["mergeable"].(bool),
+            })
+        }
+       }
+
+       return myPRs, otherPRs, nil
+}
+
+func fetchOneRESTObject(url string, token string) (map[string]interface{}, error) {
+    req, err := http.NewRequest("GET", url, nil)
+    if err != nil {
+        return nil, err
+    }
+
+    req.Header.Set("Authorization", "Bearer "+token)
+    client := &http.Client{}
+    resp, err := client.Do(req)
+    if err != nil {
+        return nil, err
+    }
+
+    if resp.StatusCode != 200 {
+        return nil, errors.New(fmt.Sprintf("Got a '%s'error from the Github API. Please retry in a bit.", resp.Status))
+    }
+
+    defer resp.Body.Close()
+
+    body, err := ioutil.ReadAll(resp.Body)
+    if err != nil {
+        return nil, err
+    }
+
+    var jsonData map[string]interface{}
+    err = json.Unmarshal(body, &jsonData)
+    if err != nil {
+        return nil, err
+    }
+
+    return jsonData, nil
+}