1
0
mirror of https://github.com/pinpox/gitea-matrix-bot synced 2024-11-22 10:21:58 +01:00

cleaned up postdata struct

This commit is contained in:
Pablo Ovelleiro Corral 2019-04-13 20:21:58 +02:00
parent 313c3e10de
commit 349ef28ba7
No known key found for this signature in database
GPG Key ID: F7C1D57C8464E825
2 changed files with 206 additions and 181 deletions

@ -43,13 +43,11 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
json.Unmarshal(body, &postData) json.Unmarshal(body, &postData)
// fmt.Println(string(body))
fmt.Printf("%+v\n", postData) fmt.Printf("%+v\n", postData)
fmt.Println("=================================================") fmt.Println("=================================================")
spew.Dump(postData) spew.Dump(postData)
fmt.Println("=================================================") fmt.Println("=================================================")
fmt.Println(string(body))
fmt.Println("=================================================") fmt.Println("=================================================")
fmt.Println(generateMessage(postData, r.Header.Get("X-Gitea-Event"))) fmt.Println(generateMessage(postData, r.Header.Get("X-Gitea-Event")))
fmt.Println("=================================================") fmt.Println("=================================================")
@ -80,7 +78,7 @@ func generateMessage(data GiteaPostData, eventHeader string) string {
case "issues": case "issues":
switch data.Action { switch data.Action {
case "assigned": case "assigned":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} assigned issue #{{.Issue.Number}} {{.Issue.Title}} to TODO ") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} assigned issue #{{.Issue.Number}} {{.Issue.Title}} to {{}} ")
case "closed": case "closed":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} closed issue #{{.Issue.Number}} {{.Issue.Title}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} closed issue #{{.Issue.Number}} {{.Issue.Title}}")
case "demilestoned": case "demilestoned":
@ -104,47 +102,40 @@ func generateMessage(data GiteaPostData, eventHeader string) string {
} }
case "fork": case "fork":
// switch data.Action {
// case "created":
templ.Parse("{{.Sender.FullName}} forked repository {{.Repository.Parent.FullName}} to {{.Repository.FullName}}") templ.Parse("{{.Sender.FullName}} forked repository {{.Repository.Parent.FullName}} to {{.Repository.FullName}}")
// case "edited":
// templ.Parse("{{.Sender.FullName}} edited repository fork {{}}")
// case "deleted":
// templ.Parse("{{.Sender.FullName}} deleted repository fork {{}}")
// }
case "pull_request": case "pull_request":
switch data.Action { switch data.Action {
case "assigned": case "assigned":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} assigned pull-request {{}} {{}} to {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} assigned pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\" to {{.PullRequest.Assignee.FullName}}")
case "closed": case "closed":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} closed pull-request {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} closed pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
case "demilestoned": case "demilestoned":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed milestone TODO from pull-request {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed milestone TODO from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
case "edited": case "edited":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} edited pull-request {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} edited pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
case "label_cleared": case "label_cleared":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed labels from pull-request {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed labels from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
case "label_updated": case "label_updated":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated labels from pull-request {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated labels from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
case "milestoned": case "milestoned":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} added pull-request {{}} {{}} to milestone TODO") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} added pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\" to milestone TODO")
case "opened": case "opened":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} opened pull-request {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} opened pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
case "reopened": case "reopened":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} re-opened pull-request {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} re-opened pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
case "synchronized": case "synchronized":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} synchronized pull-request {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} synchronized pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
case "unassigned": case "unassigned":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed assinee {{}} from pull-request {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed assinee from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
} }
case "issue_comment": case "issue_comment":
switch data.Action { switch data.Action {
case "created": case "created":
templ.Parse("{{.Sender.FullName}} commented issue #{{.Issue.Number}} {{.Issue.Title}}: {{.Comment.Body}}") templ.Parse("{{.Sender.FullName}} commented on #{{.Issue.Number}} {{.Issue.Title}}: {{.Comment.Body}}")
case "deleted": case "deleted":
templ.Parse("{{.Sender.FullName}} deleted commented on issue #{{.Issue.Number}} {{.Issue.Title}}") templ.Parse("{{.Sender.FullName}} deleted commented on #{{.Issue.Number}} {{.Issue.Title}}")
} }
case "repository": case "repository":
@ -166,11 +157,11 @@ func generateMessage(data GiteaPostData, eventHeader string) string {
} }
case "pull_request_approved": case "pull_request_approved":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} approved pull request {{}}{{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} approved pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
case "pull_request_rejected": case "pull_request_rejected":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} rejected pull request {{}}{{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} rejected pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
case "pull_request_comment": case "pull_request_comment":
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} commented on pull request {{}}{{}}: {{.Comment.Body}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} commented on pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\": {{.Comment.Body}}")
default: default:
fmt.Println("Unknown action: " + eventHeader + " " + data.Action) fmt.Println("Unknown action: " + eventHeader + " " + data.Action)

@ -4,165 +4,199 @@ import (
"time" "time"
) )
type GiteaPostData struct { type GiteaUser struct {
Secret string `json:"secret"` ID int `json:"id"`
Action string `json:"action"` Login string `json:"login"`
Number int `json:"number"` FullName string `json:"full_name"`
Ref string `json:"ref"` Name string `json:"name"`
Before string `json:"before"` Email string `json:"email"`
After string `json:"after"` AvatarURL string `json:"avatar_url"`
CompareURL string `json:"compare_url"` Language string `json:"language"`
Issue struct { Username string `json:"username"`
ID int `json:"id"` }
URL string `json:"url"`
Number int `json:"number"` type GiteaRepository struct {
User struct { ID int `json:"id"`
ID int `json:"id"` Owner GiteaUser `json:"owner"`
Login string `json:"login"` Name string `json:"name"`
FullName string `json:"full_name"` FullName string `json:"full_name"`
Email string `json:"email"` Description string `json:"description"`
AvatarURL string `json:"avatar_url"` Empty bool `json:"empty"`
Language string `json:"language"` Private bool `json:"private"`
Username string `json:"username"` Fork bool `json:"fork"`
} `json:"user"` Parent struct {
Title string `json:"title"`
Body string `json:"body"`
Labels []interface{} `json:"labels"`
Milestone interface{} `json:"milestone"`
Assignee interface{} `json:"assignee"`
Assignees interface{} `json:"assignees"`
State string `json:"state"`
Comments int `json:"comments"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt interface{} `json:"closed_at"`
DueDate interface{} `json:"due_date"`
PullRequest interface{} `json:"pull_request"`
} `json:"issue"`
Commits []struct {
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"committer"`
Verification interface{} `json:"verification"`
Timestamp time.Time `json:"timestamp"`
} `json:"commits"`
Comment struct {
ID int `json:"id"`
HTMLURL string `json:"html_url"`
PullRequestURL string `json:"pull_request_url"`
IssueURL string `json:"issue_url"`
User struct {
ID int `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Language string `json:"language"`
Username string `json:"username"`
} `json:"user"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
} `json:"comment"`
Repository struct {
ID int `json:"id"`
Owner struct {
ID int `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Language string `json:"language"`
Username string `json:"username"`
} `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
Empty bool `json:"empty"`
Private bool `json:"private"`
Fork bool `json:"fork"`
Parent struct {
CreatedAt time.Time `json:"created_at"`
Empty bool `json:"empty"`
Fork bool `json:"fork"`
Private bool `json:"private"`
Archived bool `json:"archived"`
FullName string `json:"full_name"`
ForksCount int `json:"forks_count"`
ID int `json:"id"`
OpenIssuesCount int `json:"open_issues_count"`
Size int `json:"size"`
StarsCount int `json:"stars_count"`
Name string `json:"name"`
CloneURL string `json:"clone_url"`
DefaultBranch string `json:"default_branch"`
SSHURL string `json:"ssh_url"`
WatchersCount int `json:"watchers_count"`
Description string `json:"description"`
UpdatedAt time.Time `json:"updated_at"`
Website string `json:"website"`
HTMLURL string `json:"html_url"`
Owner struct {
ID int `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Language string `json:"language"`
Username string `json:"username"`
} `json:"owner"`
Permissions struct {
Admin bool `json:"admin"`
Push bool `json:"push"`
Pull bool `json:"pull"`
} `json:"permissions"`
} `json:"parent"`
Mirror bool `json:"mirror"`
Size int `json:"size"`
HTMLURL string `json:"html_url"`
SSHURL string `json:"ssh_url"`
CloneURL string `json:"clone_url"`
Website string `json:"website"`
StarsCount int `json:"stars_count"`
ForksCount int `json:"forks_count"`
WatchersCount int `json:"watchers_count"`
OpenIssuesCount int `json:"open_issues_count"`
DefaultBranch string `json:"default_branch"`
Archived bool `json:"archived"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
Empty bool `json:"empty"`
Fork bool `json:"fork"`
Private bool `json:"private"`
Archived bool `json:"archived"`
FullName string `json:"full_name"`
ForksCount int `json:"forks_count"`
ID int `json:"id"`
OpenIssuesCount int `json:"open_issues_count"`
Size int `json:"size"`
StarsCount int `json:"stars_count"`
Name string `json:"name"`
CloneURL string `json:"clone_url"`
DefaultBranch string `json:"default_branch"`
SSHURL string `json:"ssh_url"`
WatchersCount int `json:"watchers_count"`
Description string `json:"description"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
Website string `json:"website"`
HTMLURL string `json:"html_url"`
Owner GiteaUser `json:"owner"`
Permissions struct { Permissions struct {
Admin bool `json:"admin"` Admin bool `json:"admin"`
Push bool `json:"push"` Push bool `json:"push"`
Pull bool `json:"pull"` Pull bool `json:"pull"`
} `json:"permissions"` } `json:"permissions"`
} `json:"repository"` } `json:"parent"`
Pusher struct { Mirror bool `json:"mirror"`
ID int `json:"id"` Size int `json:"size"`
Login string `json:"login"` HTMLURL string `json:"html_url"`
FullName string `json:"full_name"` SSHURL string `json:"ssh_url"`
Email string `json:"email"` CloneURL string `json:"clone_url"`
AvatarURL string `json:"avatar_url"` Website string `json:"website"`
Language string `json:"language"` StarsCount int `json:"stars_count"`
Username string `json:"username"` ForksCount int `json:"forks_count"`
} `json:"pusher"` WatchersCount int `json:"watchers_count"`
Sender struct { OpenIssuesCount int `json:"open_issues_count"`
ID int `json:"id"` DefaultBranch string `json:"default_branch"`
Login string `json:"login"` Archived bool `json:"archived"`
FullName string `json:"full_name"` CreatedAt time.Time `json:"created_at"`
Email string `json:"email"` UpdatedAt time.Time `json:"updated_at"`
AvatarURL string `json:"avatar_url"` Permissions struct {
Language string `json:"language"` Admin bool `json:"admin"`
Username string `json:"username"` Push bool `json:"push"`
} `json:"sender"` Pull bool `json:"pull"`
} `json:"permissions"`
}
type GiteaRelease struct {
ID int `json:"id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Body string `json:"body"`
URL string `json:"url"`
TarballURL string `json:"tarball_url"`
ZipballURL string `json:"zipball_url"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
Author GiteaUser `json:"author"`
Assets []interface{} `json:"assets"`
}
type GiteaPullRequest struct {
ID int `json:"id"`
URL string `json:"url"`
Number int `json:"number"`
User GiteaUser `json:"user"`
Title string `json:"title"`
Body string `json:"body"`
Labels []interface{} `json:"labels"`
Milestone GiteaMilestone `json:"milestone"`
Assignee GiteaUser `json:"assignee"`
Assignees []GiteaUser `json:"assignees"`
State string `json:"state"`
Comments int `json:"comments"`
HTMLURL string `json:"html_url"`
DiffURL string `json:"diff_url"`
PatchURL string `json:"patch_url"`
Mergeable bool `json:"mergeable"`
Merged bool `json:"merged"`
MergedAt time.Time `json:"merged_at"`
MergeCommitSha interface{} `json:"merge_commit_sha"`
MergedBy interface{} `json:"merged_by"`
Base struct {
Label string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
RepoID int `json:"repo_id"`
Repo GiteaRepository `json:"repo"`
} `json:"base"`
Head struct {
Label string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
RepoID int `json:"repo_id"`
Repo GiteaRepository `json:"repo"`
} `json:"head"`
MergeBase string `json:"merge_base"`
DueDate time.Time `json:"due_date"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt time.Time `json:"closed_at"`
}
type GiteaMilestone struct {
ID int `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
State string `json:"state"`
OpenIssues int `json:"open_issues"`
ClosedIssues int `json:"closed_issues"`
ClosedAt time.Time `json:"closed_at"`
DueOn time.Time `json:"due_on"`
}
type GiteaIssue struct {
ID int `json:"id"`
URL string `json:"url"`
Number int `json:"number"`
User GiteaUser `json:"user"`
Title string `json:"title"`
Body string `json:"body"`
Labels []interface{} `json:"labels"`
Milestone GiteaMilestone `json:"milestone"`
Assignee GiteaUser `json:"assignee"`
Assignees []GiteaUser `json:"assignees"`
State string `json:"state"`
Comments int `json:"comments"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt time.Time `json:"closed_at"`
DueDate time.Time `json:"due_date"`
PullRequest time.Time `json:"pull_request"`
}
type GiteaCommit struct {
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
Author GiteaUser `json:"author"`
Committer GiteaUser `json:"committer"`
Verification interface{} `json:"verification"`
Timestamp time.Time `json:"timestamp"`
}
type GiteaComment struct {
ID int `json:"id"`
HTMLURL string `json:"html_url"`
PullRequestURL string `json:"pull_request_url"`
IssueURL string `json:"issue_url"`
User GiteaUser `json:"user"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type GiteaPostData struct {
Secret string `json:"secret"`
Action string `json:"action"`
Number int `json:"number"`
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
CompareURL string `json:"compare_url"`
Release GiteaRelease `json:"release"`
PullRequest GiteaPullRequest `json:"pull_request"`
Issue GiteaIssue `json:"issue"`
Commits []GiteaCommit `json:"commits"`
Comment GiteaComment `json:"comment"`
Repository GiteaRepository `json:"repository"`
Pusher GiteaUser `json:"pusher"`
Sender GiteaUser `json:"sender"`
} }