• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nightconcept / almandine-go / 14898940517

08 May 2025 04:55AM UTC coverage: 37.306%. First build
14898940517

Pull #1

github

nightconcept
fix tests
Pull Request #1: first files

288 of 772 new or added lines in 11 files covered. (37.31%)

288 of 772 relevant lines covered (37.31%)

1.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/internal/core/source/github_api.go
1
package source
2

3
import (
4
        "encoding/json"
5
        "fmt"
6
        "io"
7
        "net/http"
8
        "time"
9
)
10

11
// GithubAPIBaseURL allows overriding for tests. It is an exported variable.
12
var GithubAPIBaseURL = "https://api.github.com"
13

14
// GitHubCommitInfo minimal structure to parse the commit SHA.
15
type GitHubCommitInfo struct {
16
        SHA    string `json:"sha"`
17
        Commit struct {
18
                Committer struct {
19
                        Date time.Time `json:"date"`
20
                } `json:"committer"`
21
        } `json:"commit"`
22
        // We only need the SHA, but including date for potential future use/sorting.
23
}
24

25
// GetLatestCommitSHAForFile fetches the latest commit SHA for a specific file on a given branch/ref from GitHub.
26
// owner: repository owner
27
// repo: repository name
28
// pathInRepo: path to the file within the repository
29
// ref: branch name, tag name, or commit SHA
NEW
30
func GetLatestCommitSHAForFile(owner, repo, pathInRepo, ref string) (string, error) {
×
NEW
31
        // Construct the API URL
×
NEW
32
        // See: https://docs.github.com/en/rest/commits/commits#list-commits
×
NEW
33
        // We ask for commits for a specific file on a specific branch/ref. The first result is the latest.
×
NEW
34
        apiURL := fmt.Sprintf("%s/repos/%s/%s/commits?path=%s&sha=%s&per_page=1", GithubAPIBaseURL, owner, repo, pathInRepo, ref)
×
NEW
35

×
NEW
36
        httpClient := &http.Client{Timeout: 10 * time.Second}
×
NEW
37
        req, err := http.NewRequest("GET", apiURL, nil)
×
NEW
38
        if err != nil {
×
NEW
39
                return "", fmt.Errorf("failed to create request to GitHub API: %w", err)
×
NEW
40
        }
×
41
        // GitHub API recommends setting an Accept header.
NEW
42
        req.Header.Set("Accept", "application/vnd.github.v3+json")
×
NEW
43
        // Consider adding a User-Agent header for more robust requests.
×
NEW
44
        // req.Header.Set("User-Agent", "almandine-go-cli")
×
NEW
45

×
NEW
46
        resp, err := httpClient.Do(req)
×
NEW
47
        if err != nil {
×
NEW
48
                return "", fmt.Errorf("failed to call GitHub API (%s): %w", apiURL, err)
×
NEW
49
        }
×
NEW
50
        defer func() { _ = resp.Body.Close() }()
×
51

NEW
52
        if resp.StatusCode != http.StatusOK {
×
NEW
53
                bodyBytes, _ := io.ReadAll(resp.Body)
×
NEW
54
                return "", fmt.Errorf("GitHub API request failed with status %s (%s): %s", resp.Status, apiURL, string(bodyBytes))
×
NEW
55
        }
×
56

NEW
57
        body, err := io.ReadAll(resp.Body)
×
NEW
58
        if err != nil {
×
NEW
59
                return "", fmt.Errorf("failed to read response body from GitHub API (%s): %w", apiURL, err)
×
NEW
60
        }
×
61

NEW
62
        var commits []GitHubCommitInfo
×
NEW
63
        if err := json.Unmarshal(body, &commits); err != nil {
×
NEW
64
                return "", fmt.Errorf("failed to unmarshal GitHub API response (%s): %w. Body: %s", apiURL, err, string(body))
×
NEW
65
        }
×
66

NEW
67
        if len(commits) == 0 {
×
NEW
68
                // This can happen if the path is incorrect for the given ref, or the ref itself doesn't exist.
×
NEW
69
                // Or if the ref *is* a commit SHA, and the file wasn't modified in that specific commit (the API returns history).
×
NEW
70
                // If ref is already a SHA, we should ideally use it directly. This function assumes ref might be a branch.
×
NEW
71
                // If no commits are returned for a file on a branch, it implies the file might not exist on that branch or path is wrong.
×
NEW
72
                return "", fmt.Errorf("no commits found for path '%s' at ref '%s' in repo '%s/%s'. The file might not exist at this path/ref, or the ref might be a specific commit SHA where this file was not modified", pathInRepo, ref, owner, repo)
×
NEW
73
        }
×
74

NEW
75
        return commits[0].SHA, nil
×
76
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc