code.gitea.io/gitea@v1.22.3/modules/actions/github.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package actions
     5  
     6  import (
     7  	webhook_module "code.gitea.io/gitea/modules/webhook"
     8  )
     9  
    10  const (
    11  	GithubEventPullRequest              = "pull_request"
    12  	GithubEventPullRequestTarget        = "pull_request_target"
    13  	GithubEventPullRequestReviewComment = "pull_request_review_comment"
    14  	GithubEventPullRequestReview        = "pull_request_review"
    15  	GithubEventRegistryPackage          = "registry_package"
    16  	GithubEventCreate                   = "create"
    17  	GithubEventDelete                   = "delete"
    18  	GithubEventFork                     = "fork"
    19  	GithubEventPush                     = "push"
    20  	GithubEventIssues                   = "issues"
    21  	GithubEventIssueComment             = "issue_comment"
    22  	GithubEventRelease                  = "release"
    23  	GithubEventPullRequestComment       = "pull_request_comment"
    24  	GithubEventGollum                   = "gollum"
    25  	GithubEventSchedule                 = "schedule"
    26  )
    27  
    28  // IsDefaultBranchWorkflow returns true if the event only triggers workflows on the default branch
    29  func IsDefaultBranchWorkflow(triggedEvent webhook_module.HookEventType) bool {
    30  	switch triggedEvent {
    31  	case webhook_module.HookEventDelete:
    32  		// GitHub "delete" event
    33  		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#delete
    34  		return true
    35  	case webhook_module.HookEventFork:
    36  		// GitHub "fork" event
    37  		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#fork
    38  		return true
    39  	case webhook_module.HookEventIssueComment:
    40  		// GitHub "issue_comment" event
    41  		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issue_comment
    42  		return true
    43  	case webhook_module.HookEventPullRequestComment:
    44  		// GitHub "pull_request_comment" event
    45  		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
    46  		return true
    47  	case webhook_module.HookEventWiki:
    48  		// GitHub "gollum" event
    49  		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum
    50  		return true
    51  	case webhook_module.HookEventSchedule:
    52  		// GitHub "schedule" event
    53  		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
    54  		return true
    55  	case webhook_module.HookEventIssues,
    56  		webhook_module.HookEventIssueAssign,
    57  		webhook_module.HookEventIssueLabel,
    58  		webhook_module.HookEventIssueMilestone:
    59  		// Github "issues" event
    60  		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues
    61  		return true
    62  	}
    63  
    64  	return false
    65  }
    66  
    67  // canGithubEventMatch check if the input Github event can match any Gitea event.
    68  func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEventType) bool {
    69  	switch eventName {
    70  	case GithubEventRegistryPackage:
    71  		return triggedEvent == webhook_module.HookEventPackage
    72  
    73  	// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum
    74  	case GithubEventGollum:
    75  		return triggedEvent == webhook_module.HookEventWiki
    76  
    77  	case GithubEventIssues:
    78  		switch triggedEvent {
    79  		case webhook_module.HookEventIssues,
    80  			webhook_module.HookEventIssueAssign,
    81  			webhook_module.HookEventIssueLabel,
    82  			webhook_module.HookEventIssueMilestone:
    83  			return true
    84  
    85  		default:
    86  			return false
    87  		}
    88  
    89  	case GithubEventPullRequest, GithubEventPullRequestTarget:
    90  		switch triggedEvent {
    91  		case webhook_module.HookEventPullRequest,
    92  			webhook_module.HookEventPullRequestSync,
    93  			webhook_module.HookEventPullRequestAssign,
    94  			webhook_module.HookEventPullRequestLabel,
    95  			webhook_module.HookEventPullRequestReviewRequest,
    96  			webhook_module.HookEventPullRequestMilestone:
    97  			return true
    98  
    99  		default:
   100  			return false
   101  		}
   102  
   103  	case GithubEventPullRequestReview:
   104  		switch triggedEvent {
   105  		case webhook_module.HookEventPullRequestReviewApproved,
   106  			webhook_module.HookEventPullRequestReviewComment,
   107  			webhook_module.HookEventPullRequestReviewRejected:
   108  			return true
   109  
   110  		default:
   111  			return false
   112  		}
   113  
   114  	case GithubEventSchedule:
   115  		return triggedEvent == webhook_module.HookEventSchedule
   116  
   117  	case GithubEventIssueComment:
   118  		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
   119  		return triggedEvent == webhook_module.HookEventIssueComment ||
   120  			triggedEvent == webhook_module.HookEventPullRequestComment
   121  
   122  	default:
   123  		return eventName == string(triggedEvent)
   124  	}
   125  }