feat(GitHub): support default message
This commit is contained in:
		
							
								
								
									
										32
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								main.go
									
									
									
									
									
								
							| @@ -200,6 +200,31 @@ func main() { | ||||
| 			Usage:  "Boolean value, indicates the runtime environment is GitHub Action.", | ||||
| 			EnvVar: "PLUGIN_GITHUB,GITHUB", | ||||
| 		}, | ||||
| 		cli.StringFlag{ | ||||
| 			Name:   "github.workflow", | ||||
| 			Usage:  "The name of the workflow.", | ||||
| 			EnvVar: "GITHUB_WORKFLOW", | ||||
| 		}, | ||||
| 		cli.StringFlag{ | ||||
| 			Name:   "github.action", | ||||
| 			Usage:  "The name of the action.", | ||||
| 			EnvVar: "GITHUB_ACTION", | ||||
| 		}, | ||||
| 		cli.StringFlag{ | ||||
| 			Name:   "github.event.name", | ||||
| 			Usage:  "The webhook name of the event that triggered the workflow.", | ||||
| 			EnvVar: "GITHUB_EVENT_NAME", | ||||
| 		}, | ||||
| 		cli.StringFlag{ | ||||
| 			Name:   "github.event.path", | ||||
| 			Usage:  "The path to a file that contains the payload of the event that triggered the workflow. Value: /github/workflow/event.json.", | ||||
| 			EnvVar: "GITHUB_EVENT_PATH", | ||||
| 		}, | ||||
| 		cli.StringFlag{ | ||||
| 			Name:   "github.workspace", | ||||
| 			Usage:  "The GitHub workspace path. Value: /github/workspace.", | ||||
| 			EnvVar: "GITHUB_WORKSPACE", | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	app.Version = Version | ||||
| @@ -219,6 +244,13 @@ func run(c *cli.Context) error { | ||||
| 	} | ||||
|  | ||||
| 	plugin := Plugin{ | ||||
| 		GitHub: GitHub{ | ||||
| 			Workflow:  c.String("github.workflow"), | ||||
| 			Workspace: c.String("github.workspace"), | ||||
| 			Action:    c.String("github.action"), | ||||
| 			EventName: c.String("github.event.name"), | ||||
| 			EventPath: c.String("github.event.path"), | ||||
| 		}, | ||||
| 		Repo: Repo{ | ||||
| 			FullName:  c.String("repo"), | ||||
| 			Namespace: c.String("repo.namespace"), | ||||
|   | ||||
							
								
								
									
										19
									
								
								plugin.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								plugin.go
									
									
									
									
									
								
							| @@ -13,6 +13,15 @@ import ( | ||||
| ) | ||||
|  | ||||
| type ( | ||||
| 	// GitHub information. | ||||
| 	GitHub struct { | ||||
| 		Workflow  string | ||||
| 		Workspace string | ||||
| 		Action    string | ||||
| 		EventName string | ||||
| 		EventPath string | ||||
| 	} | ||||
|  | ||||
| 	// Repo information. | ||||
| 	Repo struct { | ||||
| 		FullName  string | ||||
| @@ -65,6 +74,7 @@ type ( | ||||
|  | ||||
| 	// Plugin values. | ||||
| 	Plugin struct { | ||||
| 		GitHub GitHub | ||||
| 		Repo   Repo | ||||
| 		Commit Commit | ||||
| 		Build  Build | ||||
| @@ -370,6 +380,15 @@ func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) error { | ||||
|  | ||||
| // Message is plugin default message. | ||||
| func (p Plugin) Message() []string { | ||||
| 	if p.Config.GitHub { | ||||
| 		return []string{fmt.Sprintf("%s/%s triggered by %s (%s)", | ||||
| 			p.Repo.FullName, | ||||
| 			p.GitHub.Workflow, | ||||
| 			p.Repo.Namespace, | ||||
| 			p.GitHub.EventName, | ||||
| 		)} | ||||
| 	} | ||||
|  | ||||
| 	return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s", | ||||
| 		p.Build.Status, | ||||
| 		p.Build.Link, | ||||
|   | ||||
| @@ -51,29 +51,27 @@ func TestDefaultMessageFormat(t *testing.T) { | ||||
| 	assert.Equal(t, []string{"[success] <https://github.com/appleboy/go-hello> (master)『update travis』by Bo-Yi Wu"}, message) | ||||
| } | ||||
|  | ||||
| // func TestDefaultMessageFormatFromGitHub(t *testing.T) { | ||||
| // 	plugin := Plugin{ | ||||
| // 		Repo: Repo{ | ||||
| // 			Name:      "go-hello", | ||||
| // 			Namespace: "appleboy", | ||||
| // 		}, | ||||
| // 		Commit: Commit{ | ||||
| // 			Sha:     "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", | ||||
| // 			Author:  "Bo-Yi Wu", | ||||
| // 			Branch:  "master", | ||||
| // 			Message: "update travis", | ||||
| // 		}, | ||||
| // 		Build: Build{ | ||||
| // 			Number: 101, | ||||
| // 			Status: "success", | ||||
| // 			Link:   "https://github.com/appleboy/go-hello", | ||||
| // 		}, | ||||
| // 	} | ||||
| func TestDefaultMessageFormatFromGitHub(t *testing.T) { | ||||
| 	plugin := Plugin{ | ||||
| 		Config: Config{ | ||||
| 			GitHub: true, | ||||
| 		}, | ||||
| 		Repo: Repo{ | ||||
| 			FullName:  "appleboy/go-hello", | ||||
| 			Name:      "go-hello", | ||||
| 			Namespace: "appleboy", | ||||
| 		}, | ||||
| 		GitHub: GitHub{ | ||||
| 			Workflow:  "test-workflow", | ||||
| 			Action:    "send notification", | ||||
| 			EventName: "push", | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| // 	message := plugin.Message() | ||||
| 	message := plugin.Message() | ||||
|  | ||||
| // 	assert.Equal(t, []string{"[success] <https://github.com/appleboy/go-hello> (master)『update travis』by Bo-Yi Wu"}, message) | ||||
| // } | ||||
| 	assert.Equal(t, []string{"appleboy/go-hello/test-workflow triggered by appleboy (push)"}, message) | ||||
| } | ||||
|  | ||||
| func TestSendMessage(t *testing.T) { | ||||
| 	plugin := Plugin{ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user