# Copyright 2021 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: gitlabsources.sources.knative.dev
  labels:
    contrib.eventing.knative.dev/release: devel
    eventing.knative.dev/source: "true"
    duck.knative.dev/source: "true"
    knative.dev/crd-install: "true"
  annotations:
    # Webhook event types as documented at https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#events.
    # NOTE(antoineco): GitLab doesn't currently provide schemas for those events (gitlab-org/gitlab#208924)
    registry.knative.dev/eventTypes: |
      [
        {
          "type": "dev.knative.sources.gitlab.build",
          "description": "Triggered on status change of a job."
        },
        {
          "type": "dev.knative.sources.gitlab.deployment",
          "description": "Triggered when a deployment starts, succeeds, fails, or is cancelled."
        },
        {
          "type": "dev.knative.sources.gitlab.issue",
          "description": "Triggered when a new issue is created or an existing issue was updated/closed/reopened."
        },
        {
          "type": "dev.knative.sources.gitlab.merge_request",
          "description": "Triggered when a merge request is created/updated/merged/closed or a commit is added in the source branch."
        },
        {
          "type": "dev.knative.sources.gitlab.note",
          "description": "Triggered when a new comment is made on commits, merge requests, issues, and code snippets."
        },
        {
          "type": "dev.knative.sources.gitlab.pipeline",
          "description": "Triggered on status change of Pipeline."
        },
        {
          "type": "dev.knative.sources.gitlab.push",
          "description": "Triggered when you push to the repository except when pushing tags."
        },
        {
          "type": "dev.knative.sources.gitlab.tag_push",
          "description": "Triggered when you create (or delete) tags to the repository."
        },
        {
          "type": "dev.knative.sources.gitlab.wiki_page",
          "description": "Triggered when a wiki page is created, updated or deleted."
        }
      ]
spec:
  group: sources.knative.dev
  scope: Namespaced
  names:
    kind: GitLabSource
    plural: gitlabsources
    categories:
    - all
    - knative
    - eventing
    - sources
  versions:
  - name: v1alpha1
    served: true
    storage: true
    subresources:
      status: {}
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            description: Desired state of the event source.
            type: object
            properties:
              projectUrl:
                description: URL of the GitLab project to receive events from.
                type: string
                format: uri
              eventTypes:
                description: List of webhooks to enable on the selected GitLab
                  project. Those correspond to the attributes enumerated at
                  https://docs.gitlab.com/ee/api/projects.html#add-project-hook
                type: array
                items:
                  type: string
                  enum:
                  - confidential_issues_events
                  - confidential_note_events
                  - deployment_events
                  - issues_events
                  - job_events
                  - merge_requests_events
                  - note_events
                  - pipeline_events
                  - push_events
                  - tag_push_events
                  - wiki_page_events
                minItems: 1
              accessToken:
                description: Access token for the GitLab API.
                type: object
                properties:
                  secretKeyRef:
                    description: A reference to a Kubernetes Secret object
                      containing a GitLab access token.
                    type: object
                    properties:
                      name:
                        description: The name of the Kubernetes Secret object
                          which contains the GitLab access token.
                        type: string
                      key:
                        description: The key which contains the GitLab access
                          token within the Kubernetes Secret object referenced by
                          name.
                        type: string
                    required:
                    - name
                    - key
              secretToken:
                description: Arbitrary token used to validate requests to
                  webhooks.
                type: object
                properties:
                  secretKeyRef:
                    description: A reference to a Kubernetes Secret object
                      containing the webhook token.
                    type: object
                    properties:
                      name:
                        description: The name of the Kubernetes Secret object
                          which contains the webhook token.
                        type: string
                      key:
                        description: The key which contains the webhook token
                          within the Kubernetes Secret object referenced by name.
                        type: string
                    required:
                    - name
                    - key
              sslverify:
                description: Whether requests to webhooks should be made over
                  SSL.
                type: boolean
              serviceAccountName:
                description: Service Account the receive adapter Pod should be
                  using.
                type: string
              sink:
                description: The destination of events received from webhooks.
                type: object
                properties:
                  ref:
                    description: Reference to an addressable Kubernetes object
                      to be used as the destination of events.
                    type: object
                    properties:
                      apiVersion:
                        type: string
                      kind:
                        type: string
                      namespace:
                        type: string
                      name:
                        type: string
                    required:
                    - apiVersion
                    - kind
                    - name
                  uri:
                    description: URI to use as the destination of events.
                    type: string
                    format: uri
                oneOf:
                - required: ['ref']
                - required: ['uri']
            required:
            - projectUrl
            - eventTypes
            - accessToken
            - secretToken
            - sink
          status:
            type: object
            properties:
              webhookID:
                description: ID of the project hook registered with GitLab
                type: integer
              sinkUri:
                type: string
                format: uri
              ceAttributes:
                type: array
                items:
                  type: object
                  properties:
                    type:
                      type: string
                    source:
                      type: string
                  required:
                  - type
                  - source
              observedGeneration:
                type: integer
                format: int64
              conditions:
                type: array
                items:
                  type: object
                  properties:
                    type:
                      type: string
                    status:
                      type: string
                      enum: ['True', 'False', Unknown]
                    severity:
                      type: string
                      enum: [Error, Warning, Info]
                    reason:
                      type: string
                    message:
                      type: string
                    lastTransitionTime:
                      type: string
                      format: date-time
                  required:
                  - type
                  - status
    additionalPrinterColumns:
    - name: Ready
      type: string
      jsonPath: .status.conditions[?(@.type=='Ready')].status
    - name: Reason
      type: string
      jsonPath: .status.conditions[?(@.type=='Ready')].reason
    - name: Sink
      type: string
      jsonPath: .status.sinkUri
    - name: Age
      type: date
      jsonPath: .metadata.creationTimestamp
