зеркало из https://github.com/Azure/spec-sla-bot.git
Updated db grift task to seed database
This commit is contained in:
Родитель
d139ab0c8e
Коммит
a1bb29a114
|
@ -2,6 +2,7 @@ package actions
|
|||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/gobuffalo/buffalo"
|
||||
"github.com/gobuffalo/buffalo/middleware"
|
||||
|
@ -64,7 +65,7 @@ func App() *buffalo.App {
|
|||
eventgrid.RegisterSubscriber(app, "/specgithub", NewSpecgithubSubscriber(&eventgrid.BaseSubscriber{}))
|
||||
|
||||
//Create AMQP Listener
|
||||
messages.ReceiveFromQueue(context.Background())
|
||||
messages.ReceiveFromQueue(context.Background(), os.Getenv("CUSTOMCONNSTR_SERVICEBUS_CONNECTION_STRING"))
|
||||
|
||||
app.Resource("/assignees", AssigneesResource{})
|
||||
app.Resource("/events", EventsResource{})
|
||||
|
|
176
grifts/db.go
176
grifts/db.go
|
@ -2,70 +2,148 @@ package grifts
|
|||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/Azure/spec-sla-bot/messages"
|
||||
"github.com/Azure/spec-sla-bot/models"
|
||||
"github.com/gobuffalo/pop"
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/markbates/grift/grift"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
var _ = grift.Namespace("db", func() {
|
||||
|
||||
grift.Desc("seed", "Seed the database with PRs in the specs repo")
|
||||
grift.Add("seed", func(c *grift.Context) error {
|
||||
// Add DB seeding stuff here
|
||||
var client *github.Client
|
||||
var opt *github.PullRequestListOptions
|
||||
pullRequestList, _, err := client.PullRequests.List(ctx, "t-jaelli", "azure-service-bus-go", opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//var pr models.Pullrequest
|
||||
//var a models.Assignee
|
||||
if pullRequestList != nil {
|
||||
for _, pullRequest := range pullRequestList {
|
||||
expireTime := models.ValidTime{
|
||||
Time: time.Now(),
|
||||
Valid: false,
|
||||
var _ = grift.Add("seed:without:connection", func(c *grift.Context) error {
|
||||
var client *github.Client
|
||||
var opt *github.PullRequestListOptions
|
||||
pullRequestList, _, err := client.PullRequests.List(ctx, "t-jaelli", "azure-service-bus-go", opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if pullRequestList != nil {
|
||||
for _, pullRequest := range pullRequestList {
|
||||
pr := &models.Pullrequest{
|
||||
GitPRID: *pullRequest.ID,
|
||||
URL: *pullRequest.URL,
|
||||
HtmlUrl: *pullRequest.HTMLURL,
|
||||
IssueUrl: *pullRequest.IssueURL,
|
||||
Number: *pullRequest.Number,
|
||||
State: *pullRequest.State,
|
||||
ValidTime: false,
|
||||
Title: *pullRequest.Title,
|
||||
Body: *pullRequest.Body,
|
||||
RequestCreatedAt: *pullRequest.CreatedAt,
|
||||
RequestUpdatedAt: *pullRequest.UpdatedAt,
|
||||
RequestMergedAt: messages.NullCheckTime(pullRequest.MergedAt),
|
||||
RequestClosedAt: messages.NullCheckTime(pullRequest.ClosedAt),
|
||||
CommitsUrl: messages.NullCheckInt(pullRequest.Commits),
|
||||
StatusUrl: *pullRequest.StatusesURL, // consider changing name of column to match statuses
|
||||
ExpireTime: time.Time{},
|
||||
}
|
||||
err := models.DB.Create(pr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if pullRequest.Assignee != nil {
|
||||
a := &models.Assignee{
|
||||
Login: *pullRequest.Assignee.Login,
|
||||
Type: *pullRequest.Assignee.Type,
|
||||
HtmlUrl: *pullRequest.Assignee.HTMLURL,
|
||||
}
|
||||
pr := &models.Pullrequest{
|
||||
URL: *pullRequest.URL,
|
||||
HtmlUrl: *pullRequest.HTMLURL,
|
||||
IssueUrl: *pullRequest.IssueURL,
|
||||
Number: *pullRequest.Number,
|
||||
State: *pullRequest.State,
|
||||
Locked: *pullRequest.Title, //not correct. May need a new column for ID or get rid of locked
|
||||
Title: *pullRequest.Title,
|
||||
Body: *pullRequest.Body,
|
||||
RequestCreatedAt: *pullRequest.CreatedAt,
|
||||
RequestUpdatedAt: *pullRequest.UpdatedAt,
|
||||
RequestMergedAt: *pullRequest.MergedAt,
|
||||
RequestClosedAt: *pullRequest.ClosedAt,
|
||||
CommitsUrl: *pullRequest.Commits, // may need a null check to get the CommitsURL
|
||||
StatusUrl: *pullRequest.StatusesURL, // consider changing name of column to match statuses
|
||||
ExpireTime: expireTime,
|
||||
}
|
||||
err := models.DB.Create(pr)
|
||||
err := models.DB.Create(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if pullRequest.Assignee != nil {
|
||||
//add assignee info to the database
|
||||
a := &models.Assignee{
|
||||
Login: *pullRequest.Assignee.Login,
|
||||
Type: *pullRequest.Assignee.Type,
|
||||
HtmlUrl: *pullRequest.Assignee.HTMLURL,
|
||||
}
|
||||
err := models.DB.Create(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
var _ = grift.Add("db:seed:truncateAll", func(c *grift.Context) error {
|
||||
return models.DB.Transaction(func(tx *pop.Connection) error {
|
||||
err := tx.TruncateAll()
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
c.Set("tx", tx)
|
||||
err = grift.Run("db:seed:without:connection", c)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
})
|
||||
|
||||
var _ = grift.Add("db:seed:with:connection", func(c *grift.Context) error {
|
||||
var client *github.Client = github.NewClient(nil)
|
||||
var opt *github.PullRequestListOptions
|
||||
db := models.DB
|
||||
if tx := c.Value("tx"); tx != nil {
|
||||
log.Printf("Made connection")
|
||||
db = tx.(*pop.Connection)
|
||||
}
|
||||
pullRequestList, _, err := client.PullRequests.List(ctx, "t-jaelli", "azure-rest-api-specs", opt)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get prlist from github")
|
||||
return err
|
||||
}
|
||||
if pullRequestList != nil {
|
||||
log.Printf("list is not nil")
|
||||
log.Printf("length: %d", len(pullRequestList))
|
||||
for _, pullRequest := range pullRequestList {
|
||||
//log.Printf(pullRequest.String())
|
||||
pr := &models.Pullrequest{
|
||||
GitPRID: *pullRequest.ID,
|
||||
URL: *pullRequest.URL,
|
||||
HtmlUrl: *pullRequest.HTMLURL,
|
||||
IssueUrl: *pullRequest.IssueURL,
|
||||
Number: *pullRequest.Number,
|
||||
State: *pullRequest.State,
|
||||
ValidTime: false,
|
||||
Title: *pullRequest.Title,
|
||||
Body: *pullRequest.Title,
|
||||
RequestCreatedAt: *pullRequest.CreatedAt,
|
||||
RequestUpdatedAt: *pullRequest.UpdatedAt,
|
||||
RequestMergedAt: messages.NullCheckTime(pullRequest.MergedAt),
|
||||
RequestClosedAt: messages.NullCheckTime(pullRequest.ClosedAt),
|
||||
CommitsUrl: messages.NullCheckInt(pullRequest.Commits), // may need a null check to get the CommitsURL
|
||||
StatusUrl: *pullRequest.StatusesURL, // consider changing name of column to match statuses
|
||||
ExpireTime: time.Time{},
|
||||
}
|
||||
log.Printf("Made it here")
|
||||
log.Printf(pr.String())
|
||||
err = db.Create(pr)
|
||||
if err != nil {
|
||||
log.Printf("Could not create the pr in the database")
|
||||
return err
|
||||
}
|
||||
if pullRequest.Assignee != nil {
|
||||
a := &models.Assignee{
|
||||
Login: *pullRequest.Assignee.Login,
|
||||
Type: *pullRequest.Assignee.Type,
|
||||
HtmlUrl: *pullRequest.Assignee.HTMLURL,
|
||||
}
|
||||
err := db.Create(a)
|
||||
if err != nil {
|
||||
log.Printf("Could not create the assignee in the database")
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
var _ = grift.Add("db:seed", func(c *grift.Context) error {
|
||||
return models.DB.Transaction(func(tx *pop.Connection) error {
|
||||
c.Set("tx", tx)
|
||||
err := grift.Run("db:seed:with:connection", c)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
})
|
||||
|
|
|
@ -4,77 +4,153 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Azure/spec-sla-bot/models"
|
||||
"github.com/gobuffalo/pop"
|
||||
"github.com/google/go-github/github"
|
||||
)
|
||||
|
||||
//type AcknowledgmentStatus unint32
|
||||
//var tx *pop.Connection
|
||||
var currentExpireTime time.Time
|
||||
|
||||
func CheckAcknowledgement(event github.PullRequestEvent) {
|
||||
//Check if PR is in the database
|
||||
//Add if not (now or in another function)
|
||||
log.Print("CONNECT TO DEVELOPEMENT DB")
|
||||
tx, err := pop.Connect("developement")
|
||||
if err != nil {
|
||||
log.Printf("Could not conntect to the developement database")
|
||||
return
|
||||
}
|
||||
log.Print("MADE IT HERE")
|
||||
if checkClosed(event) || checkUnassigned(event) || (event.PullRequest.Assignee == nil && checkOpened(event)) {
|
||||
if checkClosed(event, tx) || checkUnassigned(event, tx) || (event.PullRequest.Assignee == nil && checkOpened(event, tx)) {
|
||||
//update event in DB to show the PR is no longer open and no more messages will be accepted for that PR ID
|
||||
//don't send a message
|
||||
} else if event.PullRequest.Assignee != nil && (checkAssigned(event) || checkReviewed(event) || checkEdited(event) || checkLabeled(event) || checkOpened(event)) {
|
||||
} else if event.PullRequest.Assignee != nil && (checkAssigned(event, tx) || checkReviewed(event, tx) || checkEdited(event, tx) || checkLabeled(event, tx) || checkOpened(event, tx)) {
|
||||
//send a message with PR id
|
||||
//Format string with PR ID
|
||||
log.Printf("Close: %s, Unassigned: %s, Opened: %s, Assigned: %s, Reviewed: %s, Edited: %s, Labeled: %s", checkClosed(event), checkUnassigned(event), checkOpened(event),
|
||||
checkAssigned(event), checkReviewed(event), checkEdited(event), checkLabeled(event))
|
||||
log.Printf("Close: %s, Unassigned: %s, Opened: %s, Assigned: %s, Reviewed: %s, Edited: %s, Labeled: %s", checkClosed(event, tx), checkUnassigned(event, tx), checkOpened(event, tx),
|
||||
checkAssigned(event, tx), checkReviewed(event, tx), checkEdited(event, tx), checkLabeled(event, tx))
|
||||
log.Print("MADE IT HERE")
|
||||
message := fmt.Sprintf("PR id, %d, URL, %s, Assignee, %s", *event.PullRequest.ID, *event.PullRequest.URL, *event.PullRequest.Assignee.Login)
|
||||
message := fmt.Sprintf("PR id, %d, URL, %s, Assignee, %s", *event.PullRequest.Number, *event.PullRequest.URL, *event.PullRequest.Assignee.Login)
|
||||
log.Print(message)
|
||||
err := SendToQueue(message)
|
||||
err = SendToQueue(message, currentExpireTime)
|
||||
log.Print("SENT TO QUEUE")
|
||||
if err != nil {
|
||||
log.Printf("Message for event %d not delivered", *event.PullRequest.ID)
|
||||
}
|
||||
}
|
||||
//error
|
||||
}
|
||||
|
||||
func CheckAcknowledgementComment(event github.IssueCommentEvent) {
|
||||
if checkCommented(event) {
|
||||
log.Print("CONNECT TO DEVELOPEMENT DB")
|
||||
tx, err := pop.Connect("developement")
|
||||
if err != nil {
|
||||
log.Printf("Could not conntect to the developement database")
|
||||
return
|
||||
}
|
||||
if event.Issue.IsPullRequest() && checkCommented(event, tx) {
|
||||
message := fmt.Sprintf("PR id, %d, URL, %s, Assignee, %s", *event.Issue.ID, *event.Issue.URL, *event.Issue.Assignee.Login)
|
||||
log.Print(message)
|
||||
err := SendToQueue(message)
|
||||
err = SendToQueue(message, currentExpireTime)
|
||||
log.Print("SENT TO QUEUE")
|
||||
if err != nil {
|
||||
log.Printf("Message for event %d not delivered", *event.Issue.ID)
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Printf("Comment event was not on a pull request issue")
|
||||
}
|
||||
|
||||
func checkCommented(event github.IssueCommentEvent) bool {
|
||||
func updateTime() time.Time {
|
||||
currentTime := time.Now().Local()
|
||||
if strings.Compare(currentTime.Weekday().String(), "Friday") == 0 {
|
||||
currentExpireTime := currentTime.Add(time.Hour * time.Duration(48))
|
||||
return currentExpireTime
|
||||
}
|
||||
currentExpireTime := currentTime.Add(time.Hour * time.Duration(24))
|
||||
return currentExpireTime
|
||||
}
|
||||
|
||||
func checkCommented(event github.IssueCommentEvent, tx *pop.Connection) bool {
|
||||
//check that the issue is not nil and that the issue id is a pr id in the db
|
||||
if event.Issue != nil && event.Issue.Assignee != nil {
|
||||
//check in the DB that the repo is not
|
||||
//query := tx.Where("issue_url = ?", event.Comment.IssueURL)
|
||||
/*expireTime := updateTime()
|
||||
time := models.ValidTime{
|
||||
Time: expireTime,
|
||||
Valid: true,
|
||||
}
|
||||
if event.Issue.PullRequestLinks!= nil && event.PullRequest.Assignee == nil && event.Number != nil {
|
||||
err := tx.RawQuery("UPDATE pullrequests SET expire_time=? WHERE number=?", time, *event.Number)
|
||||
if err != nil {
|
||||
log.Print("Unable to update event number %d", *event.Number)
|
||||
return false
|
||||
}
|
||||
}*/
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func checkAssigned(event github.PullRequestEvent) bool {
|
||||
func checkAssigned(event github.PullRequestEvent, tx *pop.Connection) bool {
|
||||
if strings.Compare(*event.Action, "assigned") == 0 {
|
||||
//Update PR in DB to accept messages
|
||||
expireTime := updateTime()
|
||||
err := tx.RawQuery("UPDATE pullrequests SET expire_time=? WHERE number=?", expireTime, event.Number)
|
||||
if err != nil {
|
||||
log.Print("Unable to update event number %d", *event.Number)
|
||||
return false
|
||||
}
|
||||
if event.PullRequest != nil && event.PullRequest.Assignees != nil {
|
||||
//if *event.PullRequest != nil && *event.PullRequest.Assignees != nil {
|
||||
assignees := event.PullRequest.Assignees
|
||||
for _, assignee := range assignees {
|
||||
err = tx.RawQuery("INSERT INTO assignees (login, type, html_url) SELECT ?,?,? WHERE NOT EXISTS (SELECT ? FROM assignees WHERE login = ?)", assignee.Login, assignee.Type, assignee.HTMLURL, assignee.Login)
|
||||
if err != nil {
|
||||
log.Print("Unable to add assignee %s", *event.PullRequest.Assignee)
|
||||
return false
|
||||
}
|
||||
}
|
||||
//}
|
||||
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func checkUnassigned(event github.PullRequestEvent) bool {
|
||||
func checkUnassigned(event github.PullRequestEvent, tx *pop.Connection) bool {
|
||||
if strings.Compare(*event.Action, "unassigned") == 0 {
|
||||
//Update PR in DB to no longer accept messages until assigned
|
||||
//if the assignee is nil update the expire time to be invalid
|
||||
expireTime := updateTime()
|
||||
if event.PullRequest != nil && event.PullRequest.Assignee == nil && event.Number != nil {
|
||||
err := tx.RawQuery("UPDATE pullrequests SET expire_time=? WHERE number=?", expireTime, *event.Number)
|
||||
if err != nil {
|
||||
log.Print("Unable to update event number %d", *event.Number)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func checkReviewed(event github.PullRequestEvent) bool {
|
||||
func checkReviewed(event github.PullRequestEvent, tx *pop.Connection) bool {
|
||||
if strings.Compare(*event.Action, "review_requested") == 0 {
|
||||
if event.PullRequest.Assignee != nil && event.Sender != nil {
|
||||
if strings.Compare(*event.PullRequest.Assignee.Name, *event.Sender.Name) == 0 {
|
||||
//Update DB
|
||||
expireTime := updateTime()
|
||||
if event.PullRequest != nil && event.PullRequest.Assignee == nil && event.Number != nil {
|
||||
err := tx.RawQuery("UPDATE pullrequests SET expire_time=? WHERE number=?", expireTime, *event.Number)
|
||||
if err != nil {
|
||||
log.Print("Unable to update event number %d", *event.Number)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -82,11 +158,19 @@ func checkReviewed(event github.PullRequestEvent) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func checkLabeled(event github.PullRequestEvent) bool {
|
||||
func checkLabeled(event github.PullRequestEvent, tx *pop.Connection) bool {
|
||||
if strings.Compare(*event.Action, "labeled") == 0 {
|
||||
if event.PullRequest.Assignee != nil && event.Sender != nil {
|
||||
//if strings.Compare(*event.PullRequest.Assignee.Name, *event.Sender.Name) == 0 {
|
||||
//Update DB
|
||||
expireTime := updateTime()
|
||||
if event.PullRequest != nil && event.PullRequest.Assignee == nil && event.Number != nil {
|
||||
err := tx.RawQuery("UPDATE pullrequests SET expire_time=? WHERE number=?", expireTime, *event.Number)
|
||||
if err != nil {
|
||||
log.Print("Unable to update event number %d", *event.Number)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
//}
|
||||
}
|
||||
|
@ -94,37 +178,94 @@ func checkLabeled(event github.PullRequestEvent) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func checkClosed(event github.PullRequestEvent) bool {
|
||||
func checkClosed(event github.PullRequestEvent, tx *pop.Connection) bool {
|
||||
if strings.Compare(*event.Action, "closed") == 0 {
|
||||
//Update DB to not accept messages
|
||||
return true
|
||||
//expireTime := updateTime()
|
||||
if event.PullRequest != nil && event.PullRequest.Assignee == nil && event.Number != nil {
|
||||
err := tx.RawQuery("UPDATE pullrequests SET expire_time=? WHERE number=?", time.Time{}, *event.Number)
|
||||
if err != nil {
|
||||
log.Print("Unable to update event number %d", *event.Number)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func checkOpened(event github.PullRequestEvent) bool {
|
||||
func checkOpened(event github.PullRequestEvent, tx *pop.Connection) bool {
|
||||
if strings.Compare(*event.Action, "opened") == 0 {
|
||||
//expireTime := updateTime()
|
||||
//check if PR ID is in the DB
|
||||
//Create new entry with assignee if not
|
||||
if event.PullRequest.Assignee == nil {
|
||||
//PR id cannot accept messages (not assigned yet)
|
||||
pr := &models.Pullrequest{
|
||||
GitPRID: *event.PullRequest.ID,
|
||||
URL: *event.PullRequest.URL,
|
||||
HtmlUrl: *event.PullRequest.HTMLURL,
|
||||
IssueUrl: *event.PullRequest.IssueURL,
|
||||
Number: *event.PullRequest.Number,
|
||||
State: *event.PullRequest.State,
|
||||
ValidTime: false,
|
||||
Title: *event.PullRequest.Title,
|
||||
Body: *event.PullRequest.Body,
|
||||
RequestCreatedAt: *event.PullRequest.CreatedAt,
|
||||
RequestUpdatedAt: NullCheckTime(event.PullRequest.UpdatedAt),
|
||||
RequestMergedAt: NullCheckTime(event.PullRequest.MergedAt),
|
||||
RequestClosedAt: NullCheckTime(event.PullRequest.ClosedAt),
|
||||
CommitsUrl: NullCheckInt(event.PullRequest.Commits), // may need a null check to get the CommitsURL
|
||||
StatusUrl: *event.PullRequest.StatusesURL, // consider changing name of column to match statuses
|
||||
ExpireTime: time.Time{},
|
||||
}
|
||||
err := models.DB.Create(pr)
|
||||
if err != nil {
|
||||
log.Printf("Could not create pr entry for checkopened, pr number %d", *event.Number)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
//time.Valid = true
|
||||
|
||||
//update DB for PR to accept messages
|
||||
//Case when PR closed with an assignee but then reopenned
|
||||
|
||||
//Not sure what to do here
|
||||
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func checkEdited(event github.PullRequestEvent) bool {
|
||||
func checkEdited(event github.PullRequestEvent, tx *pop.Connection) bool {
|
||||
if strings.Compare(*event.Action, "edited") == 0 {
|
||||
if event.PullRequest.Assignee != nil && event.Sender != nil {
|
||||
//if strings.Compare(*event.PullRequest.Assignee.Name, *event.Sender.) == 0 {
|
||||
//Update DB to accept messages
|
||||
expireTime := updateTime()
|
||||
if event.PullRequest != nil && event.PullRequest.Assignee == nil && event.Number != nil {
|
||||
err := tx.RawQuery("UPDATE pullrequests SET expire_time=? WHERE number=?", expireTime, *event.Number)
|
||||
if err != nil {
|
||||
log.Print("Unable to update event number %d", *event.Number)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
//}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func NullCheckTime(x *time.Time) time.Time {
|
||||
if x == nil {
|
||||
return time.Time{}
|
||||
}
|
||||
return *x
|
||||
}
|
||||
|
||||
func NullCheckInt(x *int) int {
|
||||
if x == nil {
|
||||
return 0
|
||||
}
|
||||
return *x
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -17,9 +16,7 @@ type Message struct {
|
|||
Assignee string
|
||||
}
|
||||
|
||||
func ReceiveFromQueue(ctx context.Context) (*servicebus.ListenerHandle, error) {
|
||||
connStr := mustGetenv("CUSTOMCONNSTR_SERVICEBUS_CONNECTION_STRING")
|
||||
//connStr :=
|
||||
func ReceiveFromQueue(ctx context.Context, connStr string) (*servicebus.ListenerHandle, error) {
|
||||
ns, err := servicebus.NewNamespace(servicebus.NamespaceWithConnectionString(connStr))
|
||||
log.Print("new namespace created")
|
||||
if err != nil {
|
||||
|
@ -46,25 +43,22 @@ func ReceiveFromQueue(ctx context.Context) (*servicebus.ListenerHandle, error) {
|
|||
log.Print("parsed message")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
//os.Exit(1)
|
||||
return message.DeadLetter(err)
|
||||
}
|
||||
//determine if the email should be sent
|
||||
//if shouldSend(message) {
|
||||
err = SendEmailToAssignee(messageStruct)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
return message.DeadLetter(err)
|
||||
}
|
||||
log.Print("sent email")
|
||||
//}
|
||||
return message.Complete()
|
||||
})
|
||||
|
||||
//Not sure if this should stay
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Println("I am listening...")
|
||||
return listenHandle, nil
|
||||
}
|
||||
|
||||
|
@ -77,29 +71,18 @@ func getQueueToReceive(ns *servicebus.Namespace, queueName string) (*servicebus.
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if qe == nil {
|
||||
_, err := qm.Put(ctx, queueName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
q, err := ns.NewQueue(ctx, queueName)
|
||||
return q, err
|
||||
}
|
||||
|
||||
func mustGetenv(key string) string {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
panic("Environment variable '" + key + "' required for integration tests.")
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func parseMessage(data []byte) (*Message, error) {
|
||||
str := string(data[:])
|
||||
log.Print(str)
|
||||
if len(str) != 0 {
|
||||
strSplit := strings.FieldsFunc(str, Split)
|
||||
for i, v := range strSplit {
|
||||
|
@ -114,3 +97,7 @@ func parseMessage(data []byte) (*Message, error) {
|
|||
func Split(r rune) bool {
|
||||
return r == ','
|
||||
}
|
||||
|
||||
//func shouldSend(messageStruct *Message) bool {
|
||||
|
||||
//}
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/Azure/azure-service-bus-go"
|
||||
)
|
||||
|
||||
func SendToQueue(message string) error {
|
||||
func SendToQueue(message string, exprireTime time.Time) error {
|
||||
connStr := os.Getenv("CUSTOMCONNSTR_SERVICEBUS_CONNECTION_STRING")
|
||||
ns, err := servicebus.NewNamespace(servicebus.NamespaceWithConnectionString(connStr))
|
||||
if err != nil {
|
||||
|
@ -24,10 +24,11 @@ func SendToQueue(message string) error {
|
|||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
future := time.Now().UTC().Add(1 * time.Minute)
|
||||
//future := time.Now().UTC().Add(1 * time.Minute)
|
||||
msg := servicebus.NewMessageFromString(message)
|
||||
msg.SystemProperties = &servicebus.SystemProperties{
|
||||
ScheduledEnqueueTime: &future,
|
||||
//ScheduledEnqueueTime: &future,
|
||||
ScheduledEnqueueTime: &exprireTime,
|
||||
}
|
||||
log.Print(message)
|
||||
q.Send(ctx, msg)
|
||||
|
|
|
@ -3,4 +3,6 @@ create_table("assignees", func(t) {
|
|||
t.Column("login", "string", {})
|
||||
t.Column("type", "string", {})
|
||||
t.Column("html_url", "string", {})
|
||||
})
|
||||
})
|
||||
|
||||
add_index("assignees", "login", {"unique":true})
|
|
@ -1,18 +1,21 @@
|
|||
create_table("pullrequests", func(t) {
|
||||
t.Column("id", "uuid", {"primary": true})
|
||||
t.Column("git_prid", "int", {"unique": true})
|
||||
t.Column("url", "string", {})
|
||||
t.Column("html_url", "string", {})
|
||||
t.Column("issue_url", "string", {})
|
||||
t.Column("issue_url", "string", {"unique": true})
|
||||
t.Column("number", "int", {})
|
||||
t.Column("state", "string", {})
|
||||
t.Column("locked", "string", {})
|
||||
t.Column("valid_time", "bool", {})
|
||||
t.Column("title", "string", {})
|
||||
t.Column("body", "string", {})
|
||||
t.Column("request_created_at", "time.Time", {})
|
||||
t.Column("request_updated_at", "time.Time", {})
|
||||
t.Column("request_merged_at", "time.Time", {})
|
||||
t.Column("request_closed_at", "time.Time", {})
|
||||
t.Column("request_created_at", "timestamp", {})
|
||||
t.Column("request_updated_at", "timestamp", {})
|
||||
t.Column("request_merged_at", "timestamp", {})
|
||||
t.Column("request_closed_at", "timestamp", {})
|
||||
t.Column("commits_url", "int", {})
|
||||
t.Column("status_url", "string", {})
|
||||
t.Column("expire_time", "models.ValidTime", {})
|
||||
})
|
||||
t.Column("expire_time", "timestamp", {})
|
||||
})
|
||||
|
||||
add_index("pullrequests", "git_prid", {"unique": true})
|
|
@ -11,12 +11,15 @@ import (
|
|||
)
|
||||
|
||||
type Assignee struct {
|
||||
ID uuid.UUID `json:"id" db:"id"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
Login string `json:"login" db:"login"`
|
||||
Type string `json:"type" db:"type"`
|
||||
HtmlUrl string `json:"html_url" db:"html_url"`
|
||||
ID uuid.UUID `json:"id" db:"id"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
Login string `json:"login" db:"login"`
|
||||
Type string `json:"type" db:"type"`
|
||||
HtmlUrl string `json:"html_url" db:"html_url"`
|
||||
Pullrequests Pullrequests `many_to_many:"pullrequest_assignees" db:"-"`
|
||||
//Pullrequests *Pullrequest `many_to_many:"pullrequest_assignee"`
|
||||
//Emails *Email `many_to_many:"email_assignee"`
|
||||
}
|
||||
|
||||
// String is not required by pop and may be deleted
|
||||
|
|
|
@ -16,6 +16,7 @@ type Email struct {
|
|||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
PullrequestID uuid.UUID `json:"pullrequest_id" db:"pullrequest_id"`
|
||||
TimeSent string `json:"time_sent" db:"time_sent"`
|
||||
Assignees *Assignee `many_to_many:"email_assignee"`
|
||||
}
|
||||
|
||||
// String is not required by pop and may be deleted
|
||||
|
|
|
@ -10,12 +10,13 @@ import (
|
|||
"github.com/gobuffalo/validate/validators"
|
||||
)
|
||||
|
||||
type ValidTime struct {
|
||||
/*type ValidTime struct {
|
||||
Time time.Time
|
||||
Valid bool
|
||||
}
|
||||
}*/
|
||||
|
||||
type Pullrequest struct {
|
||||
GitPRID int64 `json:"git_prid" db:"git_prid"`
|
||||
ID uuid.UUID `json:"id" db:"id"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
|
@ -24,7 +25,7 @@ type Pullrequest struct {
|
|||
IssueUrl string `json:"issue_url" db:"issue_url"`
|
||||
Number int `json:"number" db:"number"`
|
||||
State string `json:"state" db:"state"`
|
||||
Locked string `json:"locked" db:"locked"`
|
||||
ValidTime bool `json:"valid_time" db:"valid_time"`
|
||||
Title string `json:"title" db:"title"`
|
||||
Body string `json:"body" db:"body"`
|
||||
RequestCreatedAt time.Time `json:"request_created_at" db:"request_created_at"`
|
||||
|
@ -33,7 +34,8 @@ type Pullrequest struct {
|
|||
RequestClosedAt time.Time `json:"request_closed_at" db:"request_closed_at"`
|
||||
CommitsUrl int `json:"commits_url" db:"commits_url"`
|
||||
StatusUrl string `json:"status_url" db:"status_url"`
|
||||
ExpireTime ValidTime `json:"expire_time" db:"expire_time"`
|
||||
ExpireTime time.Time `json:"expire_time" db:"expire_time"`
|
||||
Assignees Assignees `many_to_many:"pullrequest_assignees" db:"-"`
|
||||
}
|
||||
|
||||
// String is not required by pop and may be deleted
|
||||
|
@ -60,7 +62,7 @@ func (p *Pullrequest) Validate(tx *pop.Connection) (*validate.Errors, error) {
|
|||
&validators.StringIsPresent{Field: p.IssueUrl, Name: "IssueUrl"},
|
||||
&validators.IntIsPresent{Field: p.Number, Name: "Number"},
|
||||
&validators.StringIsPresent{Field: p.State, Name: "State"},
|
||||
&validators.StringIsPresent{Field: p.Locked, Name: "Locked"},
|
||||
//&validators.StringIsPresent{Field: p.ValidTime, Name: "ValidTime"},
|
||||
&validators.StringIsPresent{Field: p.Title, Name: "Title"},
|
||||
&validators.StringIsPresent{Field: p.Body, Name: "Body"},
|
||||
//&validators.StringIsPresent{Field: p.RequestCreatedAt, Name: "RequestCreatedAt"},
|
||||
|
|
|
@ -10,11 +10,13 @@ import (
|
|||
)
|
||||
|
||||
type PullrequestAssignee struct {
|
||||
ID uuid.UUID `json:"id" db:"id"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
PullrequestID uuid.UUID `json:"pullrequest_id" db:"pullrequest_id"`
|
||||
AssigneeID uuid.UUID `json:"assignee_id" db:"assignee_id"`
|
||||
ID uuid.UUID `json:"id" db:"id"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
PullrequestID uuid.UUID `json:"pullrequest_id" db:"pullrequest_id"`
|
||||
AssigneeID uuid.UUID `json:"assignee_id" db:"assignee_id"`
|
||||
Assignee Assignee `belongs_to:"assignees" db:"-"`
|
||||
Pullrequest Pullrequest `belongs_to:"pullrequests" db:"-"`
|
||||
}
|
||||
|
||||
// String is not required by pop and may be deleted
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
<%= f.InputTag("GitPRID") %>
|
||||
<%= f.InputTag("URL") %>
|
||||
<%= f.InputTag("HtmlUrl") %>
|
||||
<%= f.InputTag("IssueUrl") %>
|
||||
<%= f.InputTag("Number") %>
|
||||
<%= f.InputTag("State") %>
|
||||
<%= f.InputTag("Locked") %>
|
||||
<%= f.InputTag("ValidTime") %>
|
||||
<%= f.InputTag("Title") %>
|
||||
<%= f.InputTag("Body") %>
|
||||
<%= f.InputTag("RequestCreatedAt") %>
|
||||
|
|
|
@ -7,12 +7,13 @@
|
|||
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<th>URL</th>
|
||||
<th>GitPRID</th>
|
||||
<th>URL</th>
|
||||
<th>HtmlUrl</th>
|
||||
<th>IssueUrl</th>
|
||||
<th>Number</th>
|
||||
<th>State</th>
|
||||
<th>Locked</th>
|
||||
<th>ValidTime</th>
|
||||
<th>Title</th>
|
||||
<th>Body</th>
|
||||
<th>RequestCreatedAt</th>
|
||||
|
@ -27,12 +28,13 @@
|
|||
<tbody>
|
||||
<%= for (pullrequest) in pullrequests { %>
|
||||
<tr>
|
||||
<td><%= pullrequest.URL %></td>
|
||||
<td><%= pullrequest.GitPRID %></td>
|
||||
<td><%= pullrequest.URL %></td>
|
||||
<td><%= pullrequest.HtmlUrl %></td>
|
||||
<td><%= pullrequest.IssueUrl %></td>
|
||||
<td><%= pullrequest.Number %></td>
|
||||
<td><%= pullrequest.State %></td>
|
||||
<td><%= pullrequest.Locked %></td>
|
||||
<td><%= pullrequest.ValidTime %></td>
|
||||
<td><%= pullrequest.Title %></td>
|
||||
<td><%= pullrequest.Body %></td>
|
||||
<td><%= pullrequest.RequestCreatedAt %></td>
|
||||
|
|
|
@ -8,6 +8,9 @@
|
|||
<li><a href="<%= pullrequestPath({ pullrequest_id: pullrequest.ID })%>" data-method="DELETE" data-confirm="Are you sure?" class="btn btn-danger">Destroy</a>
|
||||
</ul>
|
||||
|
||||
<p>
|
||||
<strong>GitPRID</strong>: <%= pullrequest.GitPRID %>
|
||||
</p>
|
||||
<p>
|
||||
<strong>URL</strong>: <%= pullrequest.URL %>
|
||||
</p>
|
||||
|
@ -24,7 +27,7 @@
|
|||
<strong>State</strong>: <%= pullrequest.State %>
|
||||
</p>
|
||||
<p>
|
||||
<strong>Locked</strong>: <%= pullrequest.Locked %>
|
||||
<strong>ValidTime</strong>: <%= pullrequest.ValidTime %>
|
||||
</p>
|
||||
<p>
|
||||
<strong>Title</strong>: <%= pullrequest.Title %>
|
||||
|
|
Загрузка…
Ссылка в новой задаче