Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make this action run on pull_request_target events #33

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions cmd/pr-size-labeler-action/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func (EnvArgs) Version() string {
// Constants for default configuration and event names.
const (
DefaultConfigPath = ".github/pull-request-size.yml"
EventPullRequest = "pull_request"
ParamNameFiles = "files"
ParamNameDiff = "diff"
)
Expand Down Expand Up @@ -116,7 +115,7 @@ func main() {
var args EnvArgs
arg.MustParse(&args)

if !isValidEvent(args.EventName) || !isValidRepoFormat(args.RepoName) {
if !isValidGitHubEventType(args.EventName) || !isValidRepoFormat(args.RepoName) {
return
}

Expand All @@ -138,13 +137,19 @@ func main() {
prProcessor.ProcessPullRequest()
}

// isValidEvent checks if the event name is a valid pull request event.
func isValidEvent(eventName string) bool {
if eventName != EventPullRequest {
fmt.Println("Event is not a pull request, doing nothing")
return false
// isValidGitHubEventType checks if the event name is a valid pull request event.
func isValidGitHubEventType(eventName string) bool {
allowedEvents := map[string]bool{
"pull_request": true,
"pull_request_target": true,
}
return true

if allowedEvents[strings.ToLower(eventName)] {
return true
}

fmt.Println("Event is not a valid pull request event, doing nothing")
return false
}

// isValidRepoFormat checks if the repository name follows the 'owner/repository' format.
Expand Down
27 changes: 27 additions & 0 deletions cmd/pr-size-labeler-action/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ func TestCalculateSizeAndDiff(t *testing.T) {
}
}

func TestIsValidGitHubEventType(t *testing.T) {
tests := []struct {
name string
eventName string
want bool
}{
{"Valid Event pull_request", "pull_request", true},
{"Valid Event pull_request_target", "pull_request_target", true},
{"Invalid Event empty", "", false},
{"Invalid Event random string", "random_event", false},
{"Invalid Event issue", "issue", false},
{"Invalid Event commit", "commit", false},
{"Invalid Event push", "push", false},
{"Invalid Event merge", "merge", false},
{"Invalid Event null", "null", false},
{"Invalid Event pull_request_closed", "pull_request_closed", false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isValidGitHubEventType(tt.eventName); got != tt.want {
t.Errorf("isValidGitHubEventType(%v) = %v, want %v", tt.eventName, got, tt.want)
}
})
}
}

func TestGetSize(t *testing.T) {
// Define configuration entries for clarity
xsConfig := ConfigEntry{"xs", 10, 1, []string{"size/xs"}}
Expand Down
Loading