forked from elastic/integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
167 lines (149 loc) · 3.75 KB
/
github.go
File metadata and controls
167 lines (149 loc) · 3.75 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package testsreporter
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"sort"
"strconv"
"strings"
"time"
"github.com/cli/go-gh/v2"
)
type commandRunner interface {
Exec(ctx context.Context, args ...string) (stdout, stdErr bytes.Buffer, err error)
}
type ghRunner struct {
DryRun bool
}
type githubOptions struct {
DryRun bool
Runner commandRunner
}
func (g *ghRunner) Exec(ctx context.Context, args ...string) (stdout, stdErr bytes.Buffer, err error) {
log.Printf("Running command: %s", strings.Join(args[:4], " "))
if g.DryRun {
if args[0] != "issue" || args[1] != "list" {
log.Printf("DRY-RUN> not run command")
return bytes.Buffer{}, bytes.Buffer{}, nil
}
}
return gh.ExecContext(ctx, args...)
}
type ghCli struct {
runner commandRunner
}
func newGhCli(options githubOptions) *ghCli {
var runner commandRunner
runner = options.Runner
if runner == nil {
runner = &ghRunner{
DryRun: options.DryRun,
}
}
return &ghCli{
runner: runner,
}
}
func (g *ghCli) Exists(ctx context.Context, issue *githubIssue, open bool) (bool, *githubIssue, error) {
stateIssue := "open"
if !open {
stateIssue = "closed"
}
stdout, stderr, err := g.runner.Exec(ctx,
"issue",
"list",
"--json",
"title,body,number,labels,state,url,createdAt,closedAt",
"--repo",
issue.repository,
"--search",
fmt.Sprintf("%s in:title sort:created-desc", issue.title),
"--limit",
"1000",
"--jq",
"map(select((.labels | length) > 0))| map(.labels = (.labels | map(.name)))",
"--state",
stateIssue,
)
if err != nil {
return false, nil, fmt.Errorf("failed to list issues: %w\n%s", err, stderr.String())
}
type responseListIssue struct {
CreatedAt time.Time `json:"createdAt"`
ClosedAt time.Time `json:"closedAt"`
Title string `json:"title"`
Body string `json:"body"`
Number int `json:"number"`
Labels []string `json:"labels"`
State string `json:"state"`
URL string `json:"url"`
}
var list []responseListIssue
err = json.Unmarshal(stdout.Bytes(), &list)
if err != nil {
return false, nil, fmt.Errorf("failed to unmarshal list of issues: %w", err)
}
if !open {
// There is no query available to sort by closing time of issues
sort.Slice(list, func(i, j int) bool {
return list[i].ClosedAt.After(list[j].ClosedAt)
})
}
for _, i := range list {
if i.Title == issue.title {
issueGot := newGithubIssue(githubIssueOptions{
Number: i.Number,
Title: i.Title,
Description: i.Body,
Labels: i.Labels,
State: i.State,
Repository: issue.repository,
URL: i.URL,
})
return true, issueGot, nil
}
}
return false, nil, nil
}
func (g *ghCli) Create(ctx context.Context, issue *githubIssue) error {
params := []string{
"issue",
"create",
"--title",
issue.title,
"--body",
issue.description,
"--repo",
issue.repository,
}
for _, label := range issue.labels {
params = append(params, "--label", label)
}
stdout, stderr, err := g.runner.Exec(ctx, params...)
if err != nil {
return fmt.Errorf("failed to create issue: %w\n%s", err, stderr.String())
}
fmt.Println("Created issue:", stdout.String())
return nil
}
func (g *ghCli) Update(ctx context.Context, issue *githubIssue) error {
params := []string{
"issue",
"edit",
strconv.Itoa(issue.number),
"--body",
issue.description,
"--repo",
issue.repository,
}
_, stderr, err := g.runner.Exec(ctx, params...)
if err != nil {
return fmt.Errorf("failed to update issue: %w\n%s", err, stderr.String())
}
return nil
}