зеркало из https://github.com/Azure/spec-sla-bot.git
db.go seeds database with prs and their assignees from the specs repo
This commit is contained in:
Родитель
c8aef793e7
Коммит
d139ab0c8e
58
grifts/db.go
58
grifts/db.go
|
@ -1,14 +1,70 @@
|
|||
package grifts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/Azure/spec-sla-bot/models"
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/markbates/grift/grift"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
var _ = grift.Namespace("db", func() {
|
||||
|
||||
grift.Desc("seed", "Seeds a database")
|
||||
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,
|
||||
}
|
||||
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)
|
||||
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
|
||||
})
|
||||
|
||||
|
|
|
@ -3,16 +3,16 @@ create_table("pullrequests", func(t) {
|
|||
t.Column("url", "string", {})
|
||||
t.Column("html_url", "string", {})
|
||||
t.Column("issue_url", "string", {})
|
||||
t.Column("number", "string", {})
|
||||
t.Column("number", "int", {})
|
||||
t.Column("state", "string", {})
|
||||
t.Column("locked", "string", {})
|
||||
t.Column("title", "string", {})
|
||||
t.Column("body", "string", {})
|
||||
t.Column("request_created_at", "string", {})
|
||||
t.Column("request_updated_at", "string", {})
|
||||
t.Column("request_merged_at", "string", {})
|
||||
t.Column("request_closed_at", "string", {})
|
||||
t.Column("commits_url", "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("commits_url", "int", {})
|
||||
t.Column("status_url", "string", {})
|
||||
t.Column("expire_time", "string", {})
|
||||
t.Column("expire_time", "models.ValidTime", {})
|
||||
})
|
|
@ -10,6 +10,11 @@ import (
|
|||
"github.com/gobuffalo/validate/validators"
|
||||
)
|
||||
|
||||
type ValidTime struct {
|
||||
Time time.Time
|
||||
Valid bool
|
||||
}
|
||||
|
||||
type Pullrequest struct {
|
||||
ID uuid.UUID `json:"id" db:"id"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
|
@ -17,18 +22,18 @@ type Pullrequest struct {
|
|||
URL string `json:"url" db:"url"`
|
||||
HtmlUrl string `json:"html_url" db:"html_url"`
|
||||
IssueUrl string `json:"issue_url" db:"issue_url"`
|
||||
Number string `json:"number" db:"number"`
|
||||
Number int `json:"number" db:"number"`
|
||||
State string `json:"state" db:"state"`
|
||||
Locked string `json:"locked" db:"locked"`
|
||||
Title string `json:"title" db:"title"`
|
||||
Body string `json:"body" db:"body"`
|
||||
RequestCreatedAt string `json:"request_created_at" db:"request_created_at"`
|
||||
RequestUpdatedAt string `json:"request_updated_at" db:"request_updated_at"`
|
||||
RequestMergedAt string `json:"request_merged_at" db:"request_merged_at"`
|
||||
RequestClosedAt string `json:"request_closed_at" db:"request_closed_at"`
|
||||
CommitsUrl string `json:"commits_url" db:"commits_url"`
|
||||
RequestCreatedAt time.Time `json:"request_created_at" db:"request_created_at"`
|
||||
RequestUpdatedAt time.Time `json:"request_updated_at" db:"request_updated_at"`
|
||||
RequestMergedAt time.Time `json:"request_merged_at" db:"request_merged_at"`
|
||||
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 string `json:"expire_time" db:"expire_time"`
|
||||
ExpireTime ValidTime `json:"expire_time" db:"expire_time"`
|
||||
}
|
||||
|
||||
// String is not required by pop and may be deleted
|
||||
|
@ -53,18 +58,18 @@ func (p *Pullrequest) Validate(tx *pop.Connection) (*validate.Errors, error) {
|
|||
&validators.StringIsPresent{Field: p.URL, Name: "URL"},
|
||||
&validators.StringIsPresent{Field: p.HtmlUrl, Name: "HtmlUrl"},
|
||||
&validators.StringIsPresent{Field: p.IssueUrl, Name: "IssueUrl"},
|
||||
&validators.StringIsPresent{Field: p.Number, Name: "Number"},
|
||||
&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.Title, Name: "Title"},
|
||||
&validators.StringIsPresent{Field: p.Body, Name: "Body"},
|
||||
&validators.StringIsPresent{Field: p.RequestCreatedAt, Name: "RequestCreatedAt"},
|
||||
&validators.StringIsPresent{Field: p.RequestUpdatedAt, Name: "RequestUpdatedAt"},
|
||||
&validators.StringIsPresent{Field: p.RequestMergedAt, Name: "RequestMergedAt"},
|
||||
&validators.StringIsPresent{Field: p.RequestClosedAt, Name: "RequestClosedAt"},
|
||||
&validators.StringIsPresent{Field: p.CommitsUrl, Name: "CommitsUrl"},
|
||||
//&validators.StringIsPresent{Field: p.RequestCreatedAt, Name: "RequestCreatedAt"},
|
||||
//&validators.StringIsPresent{Field: p.RequestUpdatedAt, Name: "RequestUpdatedAt"},
|
||||
//&validators.StringIsPresent{Field: p.RequestMergedAt, Name: "RequestMergedAt"},
|
||||
//&validators.StringIsPresent{Field: p.RequestClosedAt, Name: "RequestClosedAt"},
|
||||
&validators.IntIsPresent{Field: p.CommitsUrl, Name: "CommitsUrl"},
|
||||
&validators.StringIsPresent{Field: p.StatusUrl, Name: "StatusUrl"},
|
||||
&validators.StringIsPresent{Field: p.ExpireTime, Name: "ExpireTime"},
|
||||
//&validators.StringIsPresent{Field: p.ExpireTime, Name: "ExpireTime"},
|
||||
), nil
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче