Repository navigation
Expand file tree
/
Copy pathcommit_statuses.go
More file actions
83 lines (75 loc) · 2.14 KB
/
Copy pathcommit_statuses.go
File metadata and controls
83 lines (75 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package github
import (
"context"
forge "github.com/git-pkgs/forge"
"net/http"
"github.com/google/go-github/v82/github"
)
type gitHubCommitStatusService struct {
client *github.Client
}
func (f *gitHubForge) CommitStatuses() forge.CommitStatusService {
return &gitHubCommitStatusService{client: f.client}
}
func (s *gitHubCommitStatusService) List(ctx context.Context, owner, repo, sha string) ([]forge.CommitStatus, error) {
var all []forge.CommitStatus
opts := &github.ListOptions{PerPage: defaultPageSize}
for {
statuses, resp, err := s.client.Repositories.ListStatuses(ctx, owner, repo, sha, opts)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, forge.ErrNotFound
}
return nil, err
}
for _, st := range statuses {
cs := forge.CommitStatus{
State: forge.NormalizeCommitStatusState(st.GetState()),
Context: st.GetContext(),
Description: st.GetDescription(),
TargetURL: st.GetTargetURL(),
}
if st.Creator != nil {
cs.Creator = st.Creator.GetLogin()
}
if st.CreatedAt != nil {
cs.CreatedAt = st.CreatedAt.Time
}
all = append(all, cs)
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return all, nil
}
func (s *gitHubCommitStatusService) Set(ctx context.Context, owner, repo, sha string, opts forge.SetCommitStatusOpts) (*forge.CommitStatus, error) {
state := string(opts.State)
status := github.RepoStatus{
State: &state,
Context: &opts.Context,
Description: &opts.Description,
TargetURL: &opts.TargetURL,
}
result, resp, err := s.client.Repositories.CreateStatus(ctx, owner, repo, sha, status)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, forge.ErrNotFound
}
return nil, err
}
cs := &forge.CommitStatus{
State: forge.NormalizeCommitStatusState(result.GetState()),
Context: result.GetContext(),
Description: result.GetDescription(),
TargetURL: result.GetTargetURL(),
}
if result.Creator != nil {
cs.Creator = result.Creator.GetLogin()
}
if result.CreatedAt != nil {
cs.CreatedAt = result.CreatedAt.Time
}
return cs, nil
}