initial commit

This commit is contained in:
Olivier Tremblay 2025-09-13 08:40:40 -04:00
commit c215536745
5 changed files with 298 additions and 0 deletions

80
contributions/gh.go Normal file
View file

@ -0,0 +1,80 @@
package contributions
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
)
var ghc = http.Client{}
var gh_token = os.Getenv("GH_TOKEN")
type SearchResponse struct {
Items []PRResp
}
type PRResp struct {
Body string
Title string
HtmlUrl string `json:"html_url"`
ClosedAt string `json:"closed_at"`
RepositoryUrl string `json:"repository_url"`
}
type PullRequest struct {
Body string
Title string
ClosedAt string
URL string
}
func GetPRs(org, username string, from string, to string) (map[string][]PullRequest, error) {
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf(
"https://api.github.com/search/issues?q=type:pr+org:%s+author:%s+is:closed+merged:%s..%s",
org,
username,
from,
to,
),
nil)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", gh_token))
resp, err := ghc.Do(req)
if err != nil {
return nil, fmt.Errorf("error talking to github: %w", err)
}
if resp.StatusCode > 400 {
b := bytes.NewBuffer(nil)
io.Copy(b, resp.Body)
return nil, fmt.Errorf("error talking to github: %s", b.String())
}
r := &SearchResponse{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&r)
if err != nil {
return nil, fmt.Errorf("error decoding response: %w", err)
}
allthings := map[string][]PullRequest{}
for _, pr := range r.Items {
reponameparts := strings.Split(pr.RepositoryUrl, "/")
reponame := reponameparts[len(reponameparts)-1]
if prs, ok := allthings[reponame]; !ok {
allthings[reponame] = []PullRequest{{pr.Body, pr.Title, pr.ClosedAt, pr.HtmlUrl}}
} else {
allthings[reponame] = append(prs, PullRequest{pr.Body, pr.Title, pr.ClosedAt, pr.HtmlUrl})
}
}
return allthings, nil
}