github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/repo/sync/http.go (about)

     1  package sync
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/cli/cli/api"
     9  	"github.com/cli/cli/internal/ghrepo"
    10  )
    11  
    12  type commit struct {
    13  	Ref    string `json:"ref"`
    14  	NodeID string `json:"node_id"`
    15  	URL    string `json:"url"`
    16  	Object struct {
    17  		Type string `json:"type"`
    18  		SHA  string `json:"sha"`
    19  		URL  string `json:"url"`
    20  	} `json:"object"`
    21  }
    22  
    23  func latestCommit(client *api.Client, repo ghrepo.Interface, branch string) (commit, error) {
    24  	var response commit
    25  	path := fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", repo.RepoOwner(), repo.RepoName(), branch)
    26  	err := client.REST(repo.RepoHost(), "GET", path, nil, &response)
    27  	return response, err
    28  }
    29  
    30  func syncFork(client *api.Client, repo ghrepo.Interface, branch, SHA string, force bool) error {
    31  	path := fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", repo.RepoOwner(), repo.RepoName(), branch)
    32  	body := map[string]interface{}{
    33  		"sha":   SHA,
    34  		"force": force,
    35  	}
    36  	requestByte, err := json.Marshal(body)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	requestBody := bytes.NewReader(requestByte)
    41  	return client.REST(repo.RepoHost(), "PATCH", path, requestBody, nil)
    42  }