github.com/wtfutil/wtf@v0.43.0/modules/buildkite/client.go (about)

     1  package buildkite
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/wtfutil/wtf/utils"
     8  )
     9  
    10  type Pipeline struct {
    11  	Slug string `json:"slug"`
    12  }
    13  
    14  type Build struct {
    15  	State    string   `json:"state"`
    16  	Pipeline Pipeline `json:"pipeline"`
    17  	Branch   string   `json:"branch"`
    18  	WebUrl   string   `json:"web_url"`
    19  }
    20  
    21  func (widget *Widget) getBuilds() ([]Build, error) {
    22  	builds := []Build{}
    23  
    24  	for _, pipeline := range widget.settings.pipelines {
    25  		buildsForPipeline, err := widget.recentBuilds(pipeline)
    26  
    27  		if err != nil {
    28  			return nil, err
    29  		}
    30  
    31  		mostRecent := mostRecentBuildForBranches(buildsForPipeline, pipeline.branches)
    32  		builds = append(builds, mostRecent...)
    33  	}
    34  
    35  	return builds, nil
    36  }
    37  
    38  func (widget *Widget) recentBuilds(pipeline PipelineSettings) ([]Build, error) {
    39  	url := fmt.Sprintf(
    40  		"https://api.buildkite.com/v2/organizations/%s/pipelines/%s/builds%s",
    41  		widget.settings.orgSlug,
    42  		pipeline.slug,
    43  		branchesQuery(pipeline.branches),
    44  	)
    45  
    46  	req, err := http.NewRequest("GET", url, http.NoBody)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", widget.settings.apiKey))
    51  
    52  	httpClient := &http.Client{Transport: &http.Transport{
    53  		Proxy: http.ProxyFromEnvironment,
    54  	}}
    55  
    56  	resp, err := httpClient.Do(req)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	defer func() { _ = resp.Body.Close() }()
    61  
    62  	if resp.StatusCode < 200 || resp.StatusCode > 299 {
    63  		return nil, fmt.Errorf(resp.Status)
    64  	}
    65  
    66  	builds := []Build{}
    67  	err = utils.ParseJSON(&builds, resp.Body)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	return builds, nil
    73  }
    74  
    75  func branchesQuery(branches []string) string {
    76  	if len(branches) == 0 {
    77  		return ""
    78  	}
    79  
    80  	if len(branches) == 1 {
    81  		return fmt.Sprintf("?branch=%s", branches[0])
    82  	}
    83  
    84  	queryString := fmt.Sprintf("?branch[]=%s", branches[0])
    85  	for _, branch := range branches[1:] {
    86  		queryString += fmt.Sprintf("&branch[]=%s", branch)
    87  	}
    88  
    89  	return queryString
    90  }
    91  
    92  func mostRecentBuildForBranches(builds []Build, branches []string) []Build {
    93  	recentBuilds := []Build{}
    94  
    95  	haveMostRecentBuildForBranch := map[string]bool{}
    96  	for _, branch := range branches {
    97  		haveMostRecentBuildForBranch[branch] = false
    98  	}
    99  
   100  	for _, build := range builds {
   101  		if !haveMostRecentBuildForBranch[build.Branch] {
   102  			haveMostRecentBuildForBranch[build.Branch] = true
   103  			recentBuilds = append(recentBuilds, build)
   104  		}
   105  	}
   106  
   107  	return recentBuilds
   108  }