diff --git a/go.mod b/go.mod index 31dc5f5..4d1b66f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,30 @@ module git.aweoo.com/action/gotify go 1.23.3 -require github.com/sethvargo/go-githubactions v1.3.0 // indirect +require ( + github.com/gotify/go-api-client/v2 v2.0.4 + github.com/sethvargo/go-githubactions v1.3.0 +) + +require ( + github.com/PuerkitoBio/purell v1.1.0 // indirect + github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect + github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf // indirect + github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb // indirect + github.com/go-openapi/analysis v0.17.0 // indirect + github.com/go-openapi/errors v0.17.2 // indirect + github.com/go-openapi/jsonpointer v0.17.0 // indirect + github.com/go-openapi/jsonreference v0.17.0 // indirect + github.com/go-openapi/loads v0.17.0 // indirect + github.com/go-openapi/runtime v0.17.2 // indirect + github.com/go-openapi/spec v0.17.0 // indirect + github.com/go-openapi/strfmt v0.17.0 // indirect + github.com/go-openapi/swag v0.17.0 // indirect + github.com/go-openapi/validate v0.17.0 // indirect + github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 // indirect + github.com/mitchellh/mapstructure v1.1.2 // indirect + github.com/spf13/cast v1.7.0 // indirect + golang.org/x/net v0.0.0-20181005035420-146acd28ed58 // indirect + golang.org/x/text v0.3.0 // indirect + gopkg.in/yaml.v2 v2.2.1 // indirect +) diff --git a/go.sum b/go.sum index 3418b69..48dfc29 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,60 @@ +github.com/PuerkitoBio/purell v1.1.0 h1:rmGxhojJlM0tuKtfdvliR84CFHljx9ag64t2xmVkjK4= +github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= +github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf h1:eg0MeVzsP1G42dRafH3vf+al2vQIJU0YHX+1Tw87oco= +github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb h1:D4uzjWwKYQ5XnAvUbuvHW93esHg7F8N/OYeBBcJoTr0= +github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= +github.com/go-openapi/analysis v0.0.0-20180825180245-b006789cd277/go.mod h1:k70tL6pCuVxPJOHXQ+wIac1FUrvNkHolPie/cLEU6hI= +github.com/go-openapi/analysis v0.17.0 h1:8JV+dzJJiK46XqGLqqLav8ZfEiJECp8jlOFhpiCdZ+0= +github.com/go-openapi/analysis v0.17.0/go.mod h1:IowGgpVeD0vNm45So8nr+IcQ3pxVtpRoBWb8PVZO0ik= +github.com/go-openapi/errors v0.17.0/go.mod h1:LcZQpmvG4wyF5j4IhA73wkLFQg+QJXOQHVjmcZxhka0= +github.com/go-openapi/errors v0.17.2 h1:azEQ8Fnx0jmtFF2fxsnmd6I0x6rsweUF63qqSO1NmKk= +github.com/go-openapi/errors v0.17.2/go.mod h1:LcZQpmvG4wyF5j4IhA73wkLFQg+QJXOQHVjmcZxhka0= +github.com/go-openapi/jsonpointer v0.17.0 h1:nH6xp8XdXHx8dqveo0ZuJBluCO2qGrPbDNZ0dwoRHP0= +github.com/go-openapi/jsonpointer v0.17.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M= +github.com/go-openapi/jsonreference v0.17.0 h1:yJW3HCkTHg7NOA+gZ83IPHzUSnUzGXhGmsdiCcMexbA= +github.com/go-openapi/jsonreference v0.17.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I= +github.com/go-openapi/loads v0.17.0 h1:H22nMs3GDQk4SwAaFQ+jLNw+0xoFeCueawhZlv8MBYs= +github.com/go-openapi/loads v0.17.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU= +github.com/go-openapi/runtime v0.0.0-20180920151709-4f900dc2ade9/go.mod h1:6v9a6LTXWQCdL8k1AO3cvqx5OtZY/Y9wKTgaoP6YRfA= +github.com/go-openapi/runtime v0.17.2 h1:/ZK67ikFhQAMFFH/aPu2MaGH7QjP4wHBvHYOVIzDAw0= +github.com/go-openapi/runtime v0.17.2/go.mod h1:QO936ZXeisByFmZEO1IS1Dqhtf4QV1sYYFtIq6Ld86Q= +github.com/go-openapi/spec v0.17.0 h1:XNvrt8FlSVP8T1WuhbAFF6QDhJc0zsoWzX4wXARhhpE= +github.com/go-openapi/spec v0.17.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsdfssdxcBI= +github.com/go-openapi/strfmt v0.17.0 h1:1isAxYf//QDTnVzbLAMrUK++0k1EjeLJU/gTOR0o3Mc= +github.com/go-openapi/strfmt v0.17.0/go.mod h1:P82hnJI0CXkErkXi8IKjPbNBM6lV6+5pLP5l494TcyU= +github.com/go-openapi/swag v0.17.0 h1:iqrgMg7Q7SvtbWLlltPrkMs0UBJI6oTSs79JFRUi880= +github.com/go-openapi/swag v0.17.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg= +github.com/go-openapi/validate v0.17.0 h1:pqoViQz3YLOGIhAmD0N4Lt6pa/3Gnj3ymKqQwq8iS6U= +github.com/go-openapi/validate v0.17.0/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4= +github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA= +github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gotify/go-api-client/v2 v2.0.4 h1:0w8skCr8aLBDKaQDg31LKKHUGF7rt7zdRpR+6cqIAlE= +github.com/gotify/go-api-client/v2 v2.0.4/go.mod h1:VKiah/UK20bXsr0JObE1eBVLW44zbBouzjuri9iwjFU= +github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic= +github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g= +github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sethvargo/go-githubactions v1.3.0 h1:Kg633LIUV2IrJsqy2MfveiED/Ouo+H2P0itWS0eLh8A= github.com/sethvargo/go-githubactions v1.3.0/go.mod h1:7/4WeHgYfSz9U5vwuToCK9KPnELVHAhGtRwLREOQV80= +github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w= +github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/net v0.0.0-20181005035420-146acd28ed58 h1:otZG8yDCO4LVps5+9bxOeNiCvgmOyt96J3roHTYs7oE= +golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index e8d6c9c..6a287a9 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,23 @@ package main import ( "encoding/json" "fmt" + "github.com/gotify/go-api-client/v2/auth" + "github.com/gotify/go-api-client/v2/client/message" + "github.com/gotify/go-api-client/v2/gotify" + "github.com/gotify/go-api-client/v2/models" "github.com/sethvargo/go-githubactions" + "github.com/spf13/cast" + "io/fs" + "net/http" + "net/url" + "os" + "path/filepath" "time" ) func main() { + //doPush() + //return ctx, err := githubactions.Context() if err != nil { @@ -23,5 +35,298 @@ func main() { username := githubactions.GetInput("username") fmt.Printf("username is %s\n", username) + dir, _ := os.Getwd() + root := filepath.Join(dir, "../") + filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if d.IsDir() { + return nil + } + file := filepath.Join(path, d.Name()) + body, _ = os.ReadFile(file) + fmt.Printf("path: %s\n%s\n\n", file, string(body)) + return nil + }) + githubactions.SetOutput("time", time.Now().Format("2006-01-02 15:04:05")) } + +func doPush() { + + priority := githubactions.GetInput("priority") + title := githubactions.GetInput("title") + token := githubactions.GetInput("token") + contentType := githubactions.GetInput("contentType") + clickUrl := githubactions.GetInput("clickUrl") + bigImageUrl := githubactions.GetInput("bigImageUrl") + msgText := githubactions.GetInput("msgText") + stringURL := githubactions.GetInput("url") + + msg := &models.MessageExternal{ + Message: msgText, + Title: title, + Priority: cast.ToInt(priority), + } + + msg.Extras = map[string]interface{}{} + + if contentType != "" { + msg.Extras["client::display"] = map[string]interface{}{ + "contentType": contentType, + } + } + + clientNotification := map[string]interface{}{} + if clickUrl != "" { + clientNotification["click"] = map[string]string{"url": clickUrl} + } + if bigImageUrl != "" { + clientNotification["bigImageUrl"] = bigImageUrl + } + if len(clientNotification) > 0 { + msg.Extras["client::notification"] = clientNotification + } + + parsedURL, err := url.Parse(stringURL) + if err != nil { + panic(err) + } + + pushMessage(parsedURL, token, msg) +} + +func pushMessage(parsedURL *url.URL, token string, msg *models.MessageExternal) { + + client := gotify.NewClient(parsedURL, &http.Client{}) + + params := message.NewCreateMessageParams() + params.SetBody(msg) + res, err := client.Message.CreateMessage(params, auth.TokenAuth(token)) + if err == nil { + // todo成功 + response, _ := json.Marshal(res) + fmt.Println(string(response)) + } else { + panic(err) + // todo 错误处理 + } +} + +type GithubEnv struct { + Event struct { + After string `json:"after"` + Before string `json:"before"` + Commits []struct { + Added []interface{} `json:"added"` + Author struct { + Email string `json:"email"` + Name string `json:"name"` + Username string `json:"username"` + } `json:"author"` + Committer struct { + Email string `json:"email"` + Name string `json:"name"` + Username string `json:"username"` + } `json:"committer"` + Id string `json:"id"` + Message string `json:"message"` + Modified []string `json:"modified"` + Removed []interface{} `json:"removed"` + Timestamp time.Time `json:"timestamp"` + Url string `json:"url"` + Verification interface{} `json:"verification"` + } `json:"commits"` + CompareUrl string `json:"compare_url"` + HeadCommit struct { + Added []interface{} `json:"added"` + Author struct { + Email string `json:"email"` + Name string `json:"name"` + Username string `json:"username"` + } `json:"author"` + Committer struct { + Email string `json:"email"` + Name string `json:"name"` + Username string `json:"username"` + } `json:"committer"` + Id string `json:"id"` + Message string `json:"message"` + Modified []string `json:"modified"` + Removed []interface{} `json:"removed"` + Timestamp time.Time `json:"timestamp"` + Url string `json:"url"` + Verification interface{} `json:"verification"` + } `json:"head_commit"` + Pusher struct { + Active bool `json:"active"` + AvatarUrl string `json:"avatar_url"` + Created time.Time `json:"created"` + Description string `json:"description"` + Email string `json:"email"` + FollowersCount int `json:"followers_count"` + FollowingCount int `json:"following_count"` + FullName string `json:"full_name"` + HtmlUrl string `json:"html_url"` + Id int `json:"id"` + IsAdmin bool `json:"is_admin"` + Language string `json:"language"` + LastLogin time.Time `json:"last_login"` + Location string `json:"location"` + Login string `json:"login"` + LoginName string `json:"login_name"` + ProhibitLogin bool `json:"prohibit_login"` + Restricted bool `json:"restricted"` + SourceId int `json:"source_id"` + StarredReposCount int `json:"starred_repos_count"` + Username string `json:"username"` + Visibility string `json:"visibility"` + Website string `json:"website"` + } `json:"pusher"` + Ref string `json:"ref"` + Repository struct { + AllowFastForwardOnlyMerge bool `json:"allow_fast_forward_only_merge"` + AllowMergeCommits bool `json:"allow_merge_commits"` + AllowRebase bool `json:"allow_rebase"` + AllowRebaseExplicit bool `json:"allow_rebase_explicit"` + AllowRebaseUpdate bool `json:"allow_rebase_update"` + AllowSquashMerge bool `json:"allow_squash_merge"` + Archived bool `json:"archived"` + ArchivedAt time.Time `json:"archived_at"` + AvatarUrl string `json:"avatar_url"` + CloneUrl string `json:"clone_url"` + CreatedAt time.Time `json:"created_at"` + DefaultAllowMaintainerEdit bool `json:"default_allow_maintainer_edit"` + DefaultBranch string `json:"default_branch"` + DefaultDeleteBranchAfterMerge bool `json:"default_delete_branch_after_merge"` + DefaultMergeStyle string `json:"default_merge_style"` + Description string `json:"description"` + Empty bool `json:"empty"` + Fork bool `json:"fork"` + ForksCount int `json:"forks_count"` + FullName string `json:"full_name"` + HasActions bool `json:"has_actions"` + HasIssues bool `json:"has_issues"` + HasPackages bool `json:"has_packages"` + HasProjects bool `json:"has_projects"` + HasPullRequests bool `json:"has_pull_requests"` + HasReleases bool `json:"has_releases"` + HasWiki bool `json:"has_wiki"` + HtmlUrl string `json:"html_url"` + Id int `json:"id"` + IgnoreWhitespaceConflicts bool `json:"ignore_whitespace_conflicts"` + Internal bool `json:"internal"` + InternalTracker struct { + AllowOnlyContributorsToTrackTime bool `json:"allow_only_contributors_to_track_time"` + EnableIssueDependencies bool `json:"enable_issue_dependencies"` + EnableTimeTracker bool `json:"enable_time_tracker"` + } `json:"internal_tracker"` + Language string `json:"language"` + LanguagesUrl string `json:"languages_url"` + Link string `json:"link"` + Mirror bool `json:"mirror"` + MirrorInterval string `json:"mirror_interval"` + MirrorUpdated time.Time `json:"mirror_updated"` + Name string `json:"name"` + ObjectFormatName string `json:"object_format_name"` + OpenIssuesCount int `json:"open_issues_count"` + OpenPrCounter int `json:"open_pr_counter"` + OriginalUrl string `json:"original_url"` + Owner struct { + Active bool `json:"active"` + AvatarUrl string `json:"avatar_url"` + Created time.Time `json:"created"` + Description string `json:"description"` + Email string `json:"email"` + FollowersCount int `json:"followers_count"` + FollowingCount int `json:"following_count"` + FullName string `json:"full_name"` + HtmlUrl string `json:"html_url"` + Id int `json:"id"` + IsAdmin bool `json:"is_admin"` + Language string `json:"language"` + LastLogin time.Time `json:"last_login"` + Location string `json:"location"` + Login string `json:"login"` + LoginName string `json:"login_name"` + ProhibitLogin bool `json:"prohibit_login"` + Restricted bool `json:"restricted"` + SourceId int `json:"source_id"` + StarredReposCount int `json:"starred_repos_count"` + Username string `json:"username"` + Visibility string `json:"visibility"` + Website string `json:"website"` + } `json:"owner"` + Parent interface{} `json:"parent"` + Permissions struct { + Admin bool `json:"admin"` + Pull bool `json:"pull"` + Push bool `json:"push"` + } `json:"permissions"` + Private bool `json:"private"` + ProjectsMode string `json:"projects_mode"` + ReleaseCounter int `json:"release_counter"` + RepoTransfer interface{} `json:"repo_transfer"` + Size int `json:"size"` + SshUrl string `json:"ssh_url"` + StarsCount int `json:"stars_count"` + Template bool `json:"template"` + UpdatedAt time.Time `json:"updated_at"` + Url string `json:"url"` + WatchersCount int `json:"watchers_count"` + Website string `json:"website"` + } `json:"repository"` + Sender struct { + Active bool `json:"active"` + AvatarUrl string `json:"avatar_url"` + Created time.Time `json:"created"` + Description string `json:"description"` + Email string `json:"email"` + FollowersCount int `json:"followers_count"` + FollowingCount int `json:"following_count"` + FullName string `json:"full_name"` + HtmlUrl string `json:"html_url"` + Id int `json:"id"` + IsAdmin bool `json:"is_admin"` + Language string `json:"language"` + LastLogin time.Time `json:"last_login"` + Location string `json:"location"` + Login string `json:"login"` + LoginName string `json:"login_name"` + ProhibitLogin bool `json:"prohibit_login"` + Restricted bool `json:"restricted"` + SourceId int `json:"source_id"` + StarredReposCount int `json:"starred_repos_count"` + Username string `json:"username"` + Visibility string `json:"visibility"` + Website string `json:"website"` + } `json:"sender"` + TotalCommits int `json:"total_commits"` + } `json:"event"` + EventPath string `json:"event_path"` + Workflow string `json:"workflow"` + RunId string `json:"run_id"` + RunNumber string `json:"run_number"` + Actor string `json:"actor"` + Repository string `json:"repository"` + EventName string `json:"event_name"` + Sha string `json:"sha"` + Ref string `json:"ref"` + RefName string `json:"ref_name"` + RefType string `json:"ref_type"` + HeadRef string `json:"head_ref"` + BaseRef string `json:"base_ref"` + Token string `json:"token"` + Workspace string `json:"workspace"` + Action string `json:"action"` + ActionPath string `json:"action_path"` + ActionRef string `json:"action_ref"` + ActionRepository string `json:"action_repository"` + Job string `json:"job"` + JobName string `json:"job_name"` + RepositoryOwner string `json:"repository_owner"` + RetentionDays string `json:"retention_days"` + RunnerPerflog string `json:"runner_perflog"` + RunnerTrackingId string `json:"runner_tracking_id"` + ServerUrl string `json:"server_url"` + ApiUrl string `json:"api_url"` + GraphqlUrl string `json:"graphql_url"` +}