123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- /*
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
- */
- package main
- import (
- "context"
- "crypto/tls"
- "encoding/json"
- "fmt"
- "io"
- "log"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "time"
- dp "idio.link/go/depager/v2"
- )
- // Per https://docs.gitlab.com/ee/api/rest/#offset-based-pagination
- const maxPageSize = 100
- func NewGitLabClient(
- ctx context.Context,
- accessToken string,
- ) *GitLabClient {
- t := &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
- TLSHandshakeTimeout: 15 * time.Second,
- IdleConnTimeout: 30 * time.Minute,
- ResponseHeaderTimeout: 180 * time.Second,
- ExpectContinueTimeout: 10 * time.Second,
- }
- c := &http.Client{
- Transport: t,
- Timeout: 30 * time.Minute,
- }
- baseURI, _ := url.Parse("https://gitlab.com/api/v4/")
- return &GitLabClient{
- baseURI: baseURI,
- ctx: ctx,
- httpClient: c,
- retries: 3,
- retryWait: 10 * time.Second,
- accessToken: accessToken,
- }
- }
- type GitLabClient struct {
- ctx context.Context
- httpClient *http.Client
- baseURI *url.URL
- blockReqsBefore time.Time
- retries int
- retryWait time.Duration
- accessToken string
- }
- func (c *GitLabClient) expandResource(
- resource *url.URL,
- ) *url.URL {
- ex, err :=
- url.JoinPath(c.baseURI.String(), resource.EscapedPath())
- if err != nil {
- panic(fmt.Sprintf("BUG: gitlab client: expand resource: failed to join path: '%s' + '%s': %v", c.baseURI, resource.RequestURI(), err))
- }
- ex += "?" + resource.RawQuery
- next, err := url.Parse(ex)
- if err != nil {
- panic(fmt.Sprintf("BUG: gitlab client: expand resource: failed to parse expanded resource '%s': %v", ex, err))
- }
- return next
- }
- // retry when server rate limits are exceeded
- // See https://docs.gitlab.com/ee/api/rest/#status-codes
- func (c *GitLabClient) sendRequestWithRetry(
- req *http.Request,
- ) (resp *http.Response, err error) {
- for i := 0; i < c.retries+1; i++ {
- resp, err = c.httpClient.Do(req)
- if err != nil {
- err = fmt.Errorf("send request with retry: %w", err)
- return
- }
- if resp.StatusCode != http.StatusTooManyRequests {
- break
- }
- log.Printf("info: request throttled: %s '%s'", req.Method, req.RequestURI)
- // TODO Offer an async option in future, but this is
- // acceptable, for now.
- time.Sleep(c.retryWait / time.Millisecond)
- }
- if resp == nil {
- err = fmt.Errorf("send request with retry: unknown failure")
- return
- }
- return
- }
- func (c *GitLabClient) request(
- method string,
- resource *url.URL,
- body io.Reader,
- ) (respHead http.Header, respBody []byte, err error) {
- req, err := http.NewRequestWithContext(
- c.ctx,
- method,
- resource.String(),
- body,
- )
- if err != nil {
- err = fmt.Errorf("request %v: %w", resource, err)
- return
- }
- // Per https://docs.gitlab.com/ee/api/rest/#personalprojectgroup-access-tokens
- req.Header.Add(
- "Authorization",
- fmt.Sprintf("Bearer %s", c.accessToken),
- )
- resp, err := c.sendRequestWithRetry(req)
- if err != nil {
- err = fmt.Errorf("request '%s': %+v: %w", req.URL, req, err)
- return
- }
- defer resp.Body.Close()
- respHead = resp.Header
- respBody, err = io.ReadAll(resp.Body)
- if err != nil {
- err = fmt.Errorf("request %v: failed to read response body: %w", resource, err)
- return
- }
- err = c.ratelimitRequests(resp)
- if err != nil {
- err = fmt.Errorf("request %v: %w", resource, err)
- return
- }
- // Success response
- if 200 <= resp.StatusCode && resp.StatusCode <= 299 {
- return
- }
- err = fmt.Errorf("request %v: %s", resource, http.StatusText(resp.StatusCode))
- return
- }
- /*
- Throttle requests. See
- * https://gitlab.com/gitlab-com/runbooks/-/tree/master/docs/rate-limiting
- * https://docs.gitlab.com/ee/user/gitlab_com/index.html#gitlabcom-specific-rate-limits
- We postpone subsequent requests by at least win/rem
- */
- func (c *GitLabClient) ratelimitRequests(
- resp *http.Response,
- ) error {
- window := 60 * time.Second // window is not sent in headers
- remStr := resp.Header.Get("Rate-Limit-Remaining")
- remaining, err := strconv.ParseInt(remStr, 10, 64)
- if remStr != "" && err != nil {
- return fmt.Errorf("throttle requests: failed to parse header Rate-Limit-Remaining: %w", err)
- }
- if remaining != 0 && window != 0 {
- msDelay := int64(window/time.Millisecond) / remaining
- delay := time.Duration(msDelay) * time.Millisecond
- c.blockReqsBefore = time.Now().Add(delay)
- }
- return nil
- }
- func (c *GitLabClient) get(
- resource *url.URL,
- ) (http.Header, []byte, error) {
- return c.request(http.MethodGet, resource, nil)
- }
- func newSubclient[T any](
- c *GitLabClient,
- resource *url.URL,
- ) *Subclient[T] {
- expanded := c.expandResource(resource)
- return &Subclient[T]{
- GitLabClient: *c,
- uri: expanded,
- }
- }
- type Subclient[T any] struct {
- GitLabClient
- uri *url.URL
- }
- func (c *Subclient[T]) NextPage(
- offset uint64,
- ) (page dp.Page[T], cnt uint64, err error) {
- // TODO So slow... Need to adjust depager to buffer these
- // in order without waiting for each one. Keyset-based
- // pagination will ultimately make it impossible to do
- // this, but for now, we shouldn't suck this badly. Why
- // does everyone insist on making this horrible?
- aggr := make([]T, 0, 32)
- head, body, err := c.get(c.uri)
- if err != nil {
- err = fmt.Errorf("gitlab client: next page: %w", err)
- return
- }
- cnt, err = strconv.ParseUint(head.Get("x-total"), 10, 64)
- if err != nil {
- err = fmt.Errorf("gitlab client: next page: parse header 'x-total': %w", err)
- return
- }
- err = json.Unmarshal(body, &aggr)
- if err != nil {
- err = fmt.Errorf("gitlab client: next page: unmarshal response: %w", err)
- return
- }
- if next := getLinkNext(head.Get("link")); next != "" {
- c.uri, err = url.Parse(next)
- if err != nil {
- err = fmt.Errorf("gitlab client: next page: unable to parse next link '%s': %w", next, err)
- return
- }
- }
- page = GitLabAggregate[T](aggr)
- return
- }
- func getLinkNext(link string) (next string) {
- // this could be made faster, but doesn't seem necessary
- // See https://www.w3.org/wiki/LinkHeader for details.
- // basic format: `<meta.rdf>; rel=meta, ...`
- before, _, found := strings.Cut(link, `rel="next"`)
- if !found {
- return
- }
- idx := strings.LastIndex(before, "<")
- if idx == -1 {
- return
- }
- next = before[idx+1:]
- parts := strings.Split(next, ">")
- if len(parts) != 2 {
- return
- }
- next = parts[0]
- return
- }
- type GitLabAggregate[T any] []T
- func (a GitLabAggregate[T]) Elems() []T {
- return a
- }
- type GitLabProject struct {
- PathWithNamespace string `json:"path_with_namespace"`
- HTTPURLToRepo string `json:"http_url_to_repo"`
- WebURL string `json:"web_url"`
- DefaultBranch string `json:"default_branch"`
- Visibility string `json:"visibility"`
- }
- func FetchGitLabProjects[T *GitLabProject](
- c *GitLabClient,
- ) dp.Pager[T] {
- resStrFmt := "/projects?owned=true&per_page=%d"
- resStr := fmt.Sprintf(resStrFmt, maxPageSize)
- resource, err := url.Parse(resStr)
- if err != nil { // should only occur in case of bugs
- panic(err)
- }
- return dp.NewPager[T](
- newSubclient[T](c, resource),
- maxPageSize,
- )
- }
- func FetchGitLabGroupProjects[T *GitLabProject](
- c *GitLabClient,
- group string,
- ) dp.Pager[T] {
- resStrFmt := "/groups/%s/projects?simple=true&per_page=%d"
- escaped := url.PathEscape(group)
- resStr := fmt.Sprintf(resStrFmt, escaped, maxPageSize)
- resource, err := url.Parse(resStr)
- if err != nil { // should only occur in case of bugs
- panic(err)
- }
- return dp.NewPager[T](
- newSubclient[T](c, resource),
- maxPageSize,
- )
- }
|