github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/release/winbuild/winbuild.go (about)

     1  package winbuild
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"net/http"
     9  	"net/url"
    10  )
    11  
    12  const (
    13  	buildNumAPIUrl = "https://keybase.io/_/api/1.0/pkg/build_number.json"
    14  )
    15  
    16  type buildNumberResponse struct {
    17  	Status struct {
    18  		Code int    `json:"code"`
    19  		Name string `json:"name"`
    20  	} `json:"status"`
    21  	BuildNumber int `json:"build_number"`
    22  }
    23  
    24  func GetNextBuildNumber(keybaseToken string, version string, botID string, platform string) error {
    25  
    26  	form := url.Values{}
    27  	form.Set("version", version)
    28  	form.Add("bot_id", botID)
    29  	form.Add("platform", platform)
    30  	req, err := http.NewRequest("POST", buildNumAPIUrl, bytes.NewBufferString(form.Encode()))
    31  	if err != nil {
    32  		return fmt.Errorf("newrequest failed, %v", err)
    33  	}
    34  	req.Header.Add("X-keybase-admin-token", keybaseToken)
    35  	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
    36  	client := &http.Client{}
    37  	resp, err := client.Do(req)
    38  	if err != nil {
    39  		return fmt.Errorf("request failed, %v", err)
    40  	}
    41  	defer resp.Body.Close()
    42  
    43  	body, err := io.ReadAll(resp.Body)
    44  	if err != nil {
    45  		return fmt.Errorf("body err, %v", err)
    46  	}
    47  
    48  	var reply buildNumberResponse
    49  	if err := json.Unmarshal(body, &reply); err != nil {
    50  		return fmt.Errorf("json reply err, %v", err)
    51  	}
    52  
    53  	if reply.Status.Code != 0 {
    54  		return fmt.Errorf("Server returned failure, %s", body)
    55  	}
    56  
    57  	fmt.Printf("%d\n", reply.BuildNumber)
    58  	return nil
    59  }