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

heathcliff26 / gh-utility / 29820966146

21 Jul 2026 10:06AM UTC coverage: 81.234% (+0.9%) from 80.293%
29820966146

Pull #35

github

web-flow
Merge 8f6a2d8c1 into 127bbe241
Pull Request #35: pr: Add support for labels

26 of 30 new or added lines in 2 files covered. (86.67%)

632 of 778 relevant lines covered (81.23%)

0.9 hits per line

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

60.56
/pkg/pullrequest/pr.go
1
package pullrequest
2

3
import (
4
        "fmt"
5

6
        "github.com/heathcliff26/gh-utility/pkg/client"
7
        "github.com/heathcliff26/gh-utility/pkg/git"
8
)
9

10
// Commit the current changes to the given branch.
11
// Will force push the branch if needed.
12
// Returns the hashes of the created tree and commit
13
func Commit(c *client.Client, dir, msg, branch, token string) (string, string, error) {
1✔
14
        repo, err := git.OpenRepository(dir)
1✔
15
        if err != nil {
1✔
16
                return "", "", err
×
17
        }
×
18
        treeHash, err := repo.GetTreeHash()
1✔
19
        if err != nil {
1✔
20
                return "", "", err
×
21
        }
×
22
        changedFiles, err := repo.GetChangedFiles()
1✔
23
        if err != nil {
1✔
24
                return "", "", err
×
25
        }
×
26

27
        if len(changedFiles) == 0 {
1✔
28
                return "", "", nil
×
29
        }
×
30

31
        remote, err := repo.GetRemote()
1✔
32
        if err != nil {
1✔
33
                return "", "", err
×
34
        }
×
35

36
        treeHash, err = c.CreateTree(token, remote, changedFiles, treeHash, dir)
1✔
37
        if err != nil {
1✔
38
                return "", "", fmt.Errorf("failed to create tree: %w", err)
×
39
        }
×
40

41
        commitHash, err := repo.GetCommitHash()
1✔
42
        if err != nil {
1✔
43
                return treeHash, "", err
×
44
        }
×
45

46
        commitHash, err = c.CreateCommit(token, remote, msg, treeHash, []string{commitHash})
1✔
47
        if err != nil {
1✔
48
                return treeHash, "", fmt.Errorf("failed to create commit: %w", err)
×
49
        }
×
50

51
        err = c.CreateOrUpdateBranch(token, remote, branch, commitHash)
1✔
52
        if err != nil {
1✔
53
                return treeHash, commitHash, fmt.Errorf("failed to create/update branch: %w", err)
×
54
        }
×
55

56
        return treeHash, commitHash, nil
1✔
57
}
58

59
// Check if a pull request already exists.
60
// Create if not exists, update if exists.
61
// Will add the given labels to the PR if they are not already present.
62
// Returns a url to the pull request.
63
func PullRequest(c *client.Client, dir, branch, title, body, token string, labels []string) (string, error) {
1✔
64
        repo, err := git.OpenRepository(dir)
1✔
65
        if err != nil {
1✔
66
                return "", err
×
67
        }
×
68
        remote, err := repo.GetRemote()
1✔
69
        if err != nil {
1✔
70
                return "", err
×
71
        }
×
72
        base, err := repo.CurrentBranch()
1✔
73
        if err != nil {
1✔
74
                return "", err
×
75
        }
×
76

77
        pr, err := c.CreateOrUpdatePullRequest(token, remote, title, body, branch, base)
1✔
78
        if err != nil {
1✔
79
                return "", err
×
80
        }
×
81

82
        if hasMissingLabels(pr.Labels, labels) {
2✔
83
                err = c.AddLabels(token, remote, pr.Number, labels)
1✔
84
                if err != nil {
1✔
NEW
85
                        return pr.HtmlUrl, fmt.Errorf("failed to add labels: %w", err)
×
NEW
86
                }
×
87
        }
88

89
        return pr.HtmlUrl, nil
1✔
90
}
91

92
func hasMissingLabels(existing []client.Label, requested []string) bool {
1✔
93
        for _, label := range requested {
2✔
94
                found := false
1✔
95
                for _, existingLabel := range existing {
2✔
96
                        if existingLabel.Name == label {
2✔
97
                                found = true
1✔
98
                                break
1✔
99
                        }
100
                }
101
                if !found {
2✔
102
                        return true
1✔
103
                }
1✔
104
        }
105
        return false
1✔
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc