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

nightconcept / almandine-go / 14922784212

09 May 2025 06:21AM UTC coverage: 67.546%. First build
14922784212

Pull #1

github

nightconcept
more more fixing
Pull Request #1: first files

947 of 1402 new or added lines in 14 files covered. (67.55%)

947 of 1402 relevant lines covered (67.55%)

3.05 hits per line

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

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

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

12
// GithubAPIBaseURL allows overriding for tests. It is an exported variable.
13
var GithubAPIBaseURL = "https://api.github.com"
14
var GithubAPIBaseURLMutex sync.Mutex // Mutex for GithubAPIBaseURL (Exported)
15

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

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

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

6✔
51
        resp, err := httpClient.Do(req)
6✔
52
        if err != nil {
7✔
53
                return "", fmt.Errorf("failed to call GitHub API (%s): %w", apiURL, err)
1✔
54
        }
1✔
55
        defer func() { _ = resp.Body.Close() }()
10✔
56

57
        if resp.StatusCode != http.StatusOK {
6✔
58
                bodyBytes, _ := io.ReadAll(resp.Body)
1✔
59
                return "", fmt.Errorf("GitHub API request failed with status %s (%s): %s", resp.Status, apiURL, string(bodyBytes))
1✔
60
        }
1✔
61

62
        body, err := io.ReadAll(resp.Body)
4✔
63
        if err != nil {
4✔
NEW
64
                return "", fmt.Errorf("failed to read response body from GitHub API (%s): %w", apiURL, err)
×
NEW
65
        }
×
66

67
        var commits []GitHubCommitInfo
4✔
68
        if err := json.Unmarshal(body, &commits); err != nil {
5✔
69
                return "", fmt.Errorf("failed to unmarshal GitHub API response (%s): %w. Body: %s", apiURL, err, string(body))
1✔
70
        }
1✔
71

72
        if len(commits) == 0 {
4✔
73
                // This can happen if the path is incorrect for the given ref, or the ref itself doesn't exist.
1✔
74
                // Or if the ref *is* a commit SHA, and the file wasn't modified in that specific commit (the API returns history).
1✔
75
                // If ref is already a SHA, we should ideally use it directly. This function assumes ref might be a branch.
1✔
76
                // 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.
1✔
77
                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)
1✔
78
        }
1✔
79

80
        return commits[0].SHA, nil
2✔
81
}
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