github.com/argoproj/argo-events@v1.9.1/eventsources/sources/github/validate.go (about)

     1  /*
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  	http://www.apache.org/licenses/LICENSE-2.0
     7  Unless required by applicable law or agreed to in writing, software
     8  distributed under the License is distributed on an "AS IS" BASIS,
     9  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  See the License for the specific language governing permissions and
    11  limitations under the License.
    12  */
    13  
    14  package github
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	"github.com/argoproj/argo-events/common"
    21  	"github.com/argoproj/argo-events/eventsources/common/webhook"
    22  	"github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1"
    23  )
    24  
    25  // ValidateEventSource validates a github event source
    26  func (listener *EventListener) ValidateEventSource(ctx context.Context) error {
    27  	return validate(&listener.GithubEventSource)
    28  }
    29  
    30  func validate(githubEventSource *v1alpha1.GithubEventSource) error {
    31  	if githubEventSource == nil {
    32  		return common.ErrNilEventSource
    33  	}
    34  	if githubEventSource.GetOwnedRepositories() == nil && githubEventSource.Organizations == nil {
    35  		return fmt.Errorf("either repositories or organizations is required")
    36  	}
    37  	if githubEventSource.GetOwnedRepositories() != nil && githubEventSource.Organizations != nil {
    38  		return fmt.Errorf("only one of repositories and organizations is allowed")
    39  	}
    40  	if githubEventSource.NeedToCreateHooks() && len(githubEventSource.Events) == 0 {
    41  		return fmt.Errorf("events must be defined to create a github webhook")
    42  	}
    43  
    44  	if githubEventSource.ContentType != "" {
    45  		if !(githubEventSource.ContentType == "json" || githubEventSource.ContentType == "form") {
    46  			return fmt.Errorf("content type must be \"json\" or \"form\"")
    47  		}
    48  	}
    49  
    50  	// in order to avoid requests ending accidentally to public GitHub,
    51  	// make sure that both are set if either one is provided
    52  	if githubEventSource.GithubBaseURL != "" || githubEventSource.GithubUploadURL != "" {
    53  		if githubEventSource.GithubBaseURL == "" {
    54  			return fmt.Errorf("githubBaseURL is required when githubUploadURL is set")
    55  		}
    56  		if githubEventSource.GithubUploadURL == "" {
    57  			return fmt.Errorf("githubUploadURL is required when githubBaseURL is set")
    58  		}
    59  	}
    60  	return webhook.ValidateWebhookContext(githubEventSource.Webhook)
    61  }