-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
199 lines (179 loc) · 6.23 KB
/
main.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/rkoster/github-multi-repo-project-card-sync/config"
"github.com/rkoster/github-multi-repo-project-card-sync/github"
"github.com/shurcooL/githubv4"
)
func main() {
logger := log.Default()
var configFileName string
flag.StringVar(&configFileName, "config", "config.yml", "config file name to load")
flag.Parse()
c, err := config.LoadConfig(configFileName)
if err != nil {
logger.Fatalf("failed to load config: %s", err)
}
token := os.Getenv("GITHUB_TOKEN")
ctx := context.Background()
var gh *github.Client
if token != "" {
gh = github.NewTokenClient(token, ctx)
} else {
private_key := strings.Replace(os.Getenv("GITHUB_PRIVATE_KEY"), `\n`, "\n", -1)
app_id, err := strconv.ParseInt(os.Getenv("GITHUB_APP_ID"), 10, 64)
if err != nil {
logger.Fatalf("failed to parse GITHUB_APP_ID: %s", err)
}
gh, err = github.NewAppClient(c.Project.Organization, app_id, private_key, ctx)
if err != nil {
logger.Fatalf("failed to setup app auth: %s", err)
}
}
project, err := gh.GetOrganizationProject(c.Project.Organization, c.Project.Number, ctx)
if err != nil {
logger.Fatalf("failed to load github project: %s", err)
}
for i, repo := range c.Repositories {
pullRequests, err := gh.ListOpenPullRequests(repo.Name, ctx)
if err != nil {
logger.Fatalf("failed to list pull requests for: %s got: %s", repo.Name, err)
}
issues, err := gh.ListOpenIssues(repo.Name, ctx)
if err != nil {
logger.Fatalf("failed to list issues for: %s got: %s", repo.Name, err)
}
fmt.Printf("\n[%d/%d] %s processing: %d ", i, len(c.Repositories), repo.Name, len(pullRequests)+len(issues))
for _, pullRequest := range pullRequests {
err = processPullRequest(pullRequest, project, repo, gh, ctx)
if err != nil {
logger.Fatalf("failed to process pull request: %s got: %s", pullRequest.URL, err)
}
fmt.Print(".")
}
for _, issue := range issues {
err = processIssue(issue, project, repo, gh, ctx)
if err != nil {
logger.Fatalf("failed to process issue: %s got: %s", issue.URL, err)
}
fmt.Print(".")
}
}
}
func processIssue(issue github.Issue, project github.Project, repo config.Repository, gh *github.Client, ctx context.Context) error {
item, err := gh.AddProjectItem(&project.ID, githubv4.NewID(issue.ID), ctx)
if err != nil {
return fmt.Errorf("failed to add project item got: %s", err)
}
for _, field := range repo.Fields {
f, found := project.Fields.FindByName(field.Name)
if !found {
return fmt.Errorf("Project does not have field with name: %s", field.Name)
}
var input githubv4.ProjectV2FieldValue
switch field.Type {
case "draft":
continue
case "changes":
continue
case "author":
input.Text = &issue.Author.Login
case "last_activity":
input.Date = &issue.TimelineItems.UpdatedAt
case "default_single_select":
currentValue, found := item.FieldValues.Nodes.FindByFieldName(field.Name)
if found && currentValue.ProjectV2ItemFieldSingleSelectValue.OptionID != "" {
continue
}
o, found := f.FindOptionByName(field.Value)
if !found {
return fmt.Errorf("Project field: %s does not have an option: %s", field.Name, field.Value)
}
input.SingleSelectOptionID = &o.ID
case "single_select":
o, found := f.FindOptionByName(field.Value)
if !found {
return fmt.Errorf("Project field: %s does not have an option: %s", field.Name, field.Value)
}
input.SingleSelectOptionID = &o.ID
case "type":
o, found := f.FindOptionByName("Issue")
if !found {
return fmt.Errorf("Project field: %s does not have an option: %s", field.Name, "Issue")
}
input.SingleSelectOptionID = &o.ID
default:
return fmt.Errorf(
"Only 'draft', 'author' and 'single_select' are currently supported values for field.type, given: %v", field.Type)
}
err := gh.UpdateProjectItemField(&project.ID, githubv4.NewID(item.ID), github.GetID(f), input, ctx)
if err != nil {
return fmt.Errorf("Failed to update Project Item Field got: %s", err)
}
}
return nil
}
func processPullRequest(pullRequest github.PullRequest, project github.Project, repo config.Repository, gh *github.Client, ctx context.Context) error {
item, err := gh.AddProjectItem(githubv4.NewID(project.ID), githubv4.NewID(pullRequest.ID), ctx)
if err != nil {
return fmt.Errorf("Failed to add Project Item got: %s", err)
}
for _, field := range repo.Fields {
f, found := project.Fields.FindByName(field.Name)
if !found {
return fmt.Errorf("Project does not have field with name: %s", field.Name)
}
var input githubv4.ProjectV2FieldValue
switch field.Type {
case "draft":
o, found := f.FindOptionByName(strconv.FormatBool(pullRequest.IsDraft))
if !found {
return fmt.Errorf("Project field: %s does not have an option: %s", field.Name, field.Value)
}
input.SingleSelectOptionID = &o.ID
case "changes":
input.Number = pullRequest.Changes()
case "author":
input.Text = &pullRequest.Author.Login
case "last_activity":
input.Date = &pullRequest.TimelineItems.UpdatedAt
case "default_single_select":
currentValue, found := item.FieldValues.Nodes.FindByFieldName(field.Name)
if found && currentValue.ProjectV2ItemFieldSingleSelectValue.OptionID != "" {
continue
}
o, found := f.FindOptionByName(field.Value)
if !found {
return fmt.Errorf("Project field: %s does not have an option: %s", field.Name, field.Value)
}
input.SingleSelectOptionID = &o.ID
case "single_select":
o, found := f.FindOptionByName(field.Value)
if !found {
return fmt.Errorf("Project field: %s does not have an option: %s", field.Name, field.Value)
}
input.SingleSelectOptionID = &o.ID
case "type":
o, found := f.FindOptionByName("Pull Request")
if !found {
return fmt.Errorf("Project field: %s does not have an option: %s", field.Name, "Pull Request")
}
input.SingleSelectOptionID = &o.ID
default:
return fmt.Errorf(
"Only 'draft', 'author' and 'single_select' are currently supported values for field.type, given: %v", field.Type)
}
err := gh.UpdateProjectItemField(githubv4.NewID(project.ID),
githubv4.NewID(item.ID), github.GetID(f), input, ctx)
if err != nil {
return fmt.Errorf("Failed to update Project Item Field got: %s", err)
}
}
return nil
}