Added deployments overview
This commit is contained in:
@@ -2,6 +2,7 @@ package github
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
@@ -11,7 +12,67 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func FetchAndDeployBranch(repoOwner, repoName, branch, pat, destDir string) error {
|
||||
// DeployResult holds metadata about the deployed commit.
|
||||
type DeployResult struct {
|
||||
CommitHash string `json:"commit_hash"`
|
||||
CommitMessage string `json:"commit_message"`
|
||||
}
|
||||
|
||||
func FetchAndDeployBranch(repoOwner, repoName, branch, pat, destDir string) (*DeployResult, error) {
|
||||
result, err := fetchBranchHead(repoOwner, repoName, branch, pat)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetching branch head: %w", err)
|
||||
}
|
||||
|
||||
if err = downloadAndExtract(repoOwner, repoName, branch, pat, destDir); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// fetchBranchHead returns the SHA and commit message for the tip of the given branch.
|
||||
func fetchBranchHead(owner, repo, branch, pat string) (*DeployResult, error) {
|
||||
url := fmt.Sprintf(
|
||||
"https://api.github.com/repos/%s/%s/commits/%s",
|
||||
owner, repo, branch,
|
||||
)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+pat)
|
||||
req.Header.Set("Accept", "application/vnd.github+json")
|
||||
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetching commit: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("GitHub returned %s for %s", resp.Status, url)
|
||||
}
|
||||
|
||||
var payload struct {
|
||||
SHA string `json:"sha"`
|
||||
Commit struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"commit"`
|
||||
}
|
||||
if err = json.NewDecoder(resp.Body).Decode(&payload); err != nil {
|
||||
return nil, fmt.Errorf("decoding commit response: %w", err)
|
||||
}
|
||||
|
||||
return &DeployResult{
|
||||
CommitHash: payload.SHA,
|
||||
CommitMessage: strings.SplitN(payload.Commit.Message, "\n", 2)[0],
|
||||
}, nil
|
||||
}
|
||||
|
||||
func downloadAndExtract(repoOwner, repoName, branch, pat, destDir string) error {
|
||||
archiveURL := fmt.Sprintf(
|
||||
"https://api.github.com/repos/%s/%s/zipball/%s",
|
||||
repoOwner, repoName, branch,
|
||||
@@ -59,7 +120,7 @@ func FetchAndDeployBranch(repoOwner, repoName, branch, pat, destDir string) erro
|
||||
return fmt.Errorf("unzipping: %w", err)
|
||||
}
|
||||
|
||||
// github wraps everything in one top-level folder ({owner}-{repo}-{sha}/).
|
||||
// GitHub wraps everything in one top-level folder ({owner}-{repo}-{sha}/).
|
||||
entries, err := os.ReadDir(tmpDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading temp dir: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user