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.", | 			Usage:  "Boolean value, indicates the runtime environment is GitHub Action.", | ||||||
| 			EnvVar: "PLUGIN_GITHUB,GITHUB", | 			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 | 	app.Version = Version | ||||||
| @@ -219,6 +244,13 @@ func run(c *cli.Context) error { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	plugin := Plugin{ | 	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{ | 		Repo: Repo{ | ||||||
| 			FullName:  c.String("repo"), | 			FullName:  c.String("repo"), | ||||||
| 			Namespace: c.String("repo.namespace"), | 			Namespace: c.String("repo.namespace"), | ||||||
|   | |||||||
							
								
								
									
										19
									
								
								plugin.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								plugin.go
									
									
									
									
									
								
							| @@ -13,6 +13,15 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| type ( | type ( | ||||||
|  | 	// GitHub information. | ||||||
|  | 	GitHub struct { | ||||||
|  | 		Workflow  string | ||||||
|  | 		Workspace string | ||||||
|  | 		Action    string | ||||||
|  | 		EventName string | ||||||
|  | 		EventPath string | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	// Repo information. | 	// Repo information. | ||||||
| 	Repo struct { | 	Repo struct { | ||||||
| 		FullName  string | 		FullName  string | ||||||
| @@ -65,6 +74,7 @@ type ( | |||||||
|  |  | ||||||
| 	// Plugin values. | 	// Plugin values. | ||||||
| 	Plugin struct { | 	Plugin struct { | ||||||
|  | 		GitHub GitHub | ||||||
| 		Repo   Repo | 		Repo   Repo | ||||||
| 		Commit Commit | 		Commit Commit | ||||||
| 		Build  Build | 		Build  Build | ||||||
| @@ -370,6 +380,15 @@ func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) error { | |||||||
|  |  | ||||||
| // Message is plugin default message. | // Message is plugin default message. | ||||||
| func (p Plugin) Message() []string { | 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", | 	return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s", | ||||||
| 		p.Build.Status, | 		p.Build.Status, | ||||||
| 		p.Build.Link, | 		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) | 	assert.Equal(t, []string{"[success] <https://github.com/appleboy/go-hello> (master)『update travis』by Bo-Yi Wu"}, message) | ||||||
| } | } | ||||||
|  |  | ||||||
| // func TestDefaultMessageFormatFromGitHub(t *testing.T) { | func TestDefaultMessageFormatFromGitHub(t *testing.T) { | ||||||
| // 	plugin := Plugin{ | 	plugin := Plugin{ | ||||||
| // 		Repo: Repo{ | 		Config: Config{ | ||||||
| // 			Name:      "go-hello", | 			GitHub: true, | ||||||
| // 			Namespace: "appleboy", | 		}, | ||||||
| // 		}, | 		Repo: Repo{ | ||||||
| // 		Commit: Commit{ | 			FullName:  "appleboy/go-hello", | ||||||
| // 			Sha:     "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", | 			Name:      "go-hello", | ||||||
| // 			Author:  "Bo-Yi Wu", | 			Namespace: "appleboy", | ||||||
| // 			Branch:  "master", | 		}, | ||||||
| // 			Message: "update travis", | 		GitHub: GitHub{ | ||||||
| // 		}, | 			Workflow:  "test-workflow", | ||||||
| // 		Build: Build{ | 			Action:    "send notification", | ||||||
| // 			Number: 101, | 			EventName: "push", | ||||||
| // 			Status: "success", | 		}, | ||||||
| // 			Link:   "https://github.com/appleboy/go-hello", | 	} | ||||||
| // 		}, |  | ||||||
| // 	} |  | ||||||
|  |  | ||||||
| // 	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) { | func TestSendMessage(t *testing.T) { | ||||||
| 	plugin := Plugin{ | 	plugin := Plugin{ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user