...
1 package milestonereport
2
3 import (
4 "fmt"
5
6 "github.com/google/go-github/v47/github"
7
8
9 "edge-infra.dev/pkg/f8n/devinfra/database/psql"
10
11 "edge-infra.dev/pkg/f8n/devinfra/jack/constants"
12 "edge-infra.dev/pkg/f8n/devinfra/reports"
13
14 "edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
15 )
16
17 const (
18 dbUser = "jack-bot@ret-edge-dev-infra.iam"
19 )
20
21 func init() {
22 plugin.RegisterIssueHandler(constants.PluginMilestoneReport, handleIssue)
23 }
24
25 func handleIssue(hp plugin.HandlerParams, ce github.IssuesEvent) {
26 hp.Log.WithName(constants.PluginMilestoneReport)
27
28 action := ce.GetAction()
29 switch {
30 case action == "milestoned" || action == "labeled" || action == "unlabeled" ||
31 action == "edited":
32 err := updateIssue(hp, ce)
33 if err != nil {
34 hp.Log.Error(err, "Failed to update issue")
35 }
36 }
37 }
38
39 func updateIssue(hp plugin.HandlerParams, ce github.IssuesEvent) error {
40 log := hp.Log
41 issue := ce.GetIssue()
42 log.Info("trying to connect to the db")
43 psql, err := psql.New(psql.WithUser(dbUser))
44 if err != nil {
45 log.Error(err, "failed to open connection")
46 return err
47 }
48 insertion := reports.CreateInsertion(issue)
49
50 issueScan, err := insertion.GetOrAddIssue(psql)
51 if err != nil {
52 log.Error(err, "failed to add or update issue")
53 return err
54 }
55 stmt, err := psql.DB.Prepare("update issues set milestone_number=$1, title=$2, size=$4, kind=$5, area=$6 where number=$3")
56 if err != nil {
57 return err
58 }
59 _, err = stmt.Exec(insertion.Milestones.Number, insertion.Issues.Title, insertion.Issues.Number,
60 insertion.Issues.Size, insertion.Issues.Kind, insertion.Issues.Area)
61 if err != nil {
62 return err
63 }
64 log.Info(fmt.Sprintf("Updated issue %d to milestone %v in table", issueScan.Number, insertion.Milestones.Title))
65 return nil
66 }
67
View as plain text