mirror of https://github.com/harness/drone.git
123 lines
3.5 KiB
Go
123 lines
3.5 KiB
Go
// Copyright 2023 Harness, Inc.
|
|
//
|
|
// 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.
|
|
|
|
package webhook
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
|
|
urlprovider "github.com/harness/gitness/app/url"
|
|
"github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
|
|
registryevents "github.com/harness/gitness/registry/app/events"
|
|
"github.com/harness/gitness/registry/app/pkg"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
const ociPrefix = "oci://"
|
|
|
|
func GetArtifactCreatedPayload(
|
|
ctx context.Context,
|
|
info pkg.RegistryInfo,
|
|
principalID int64,
|
|
registryID int64,
|
|
regIdentifier string,
|
|
tag string,
|
|
digest string,
|
|
urlProvider urlprovider.Provider,
|
|
) registryevents.ArtifactCreatedPayload {
|
|
payload := registryevents.ArtifactCreatedPayload{
|
|
RegistryID: registryID,
|
|
PrincipalID: principalID,
|
|
ArtifactType: info.PackageType,
|
|
}
|
|
artifactURL := urlProvider.RegistryURL(ctx, info.RootIdentifier, regIdentifier) + "/" + info.Image + ":" + tag
|
|
urlWithoutProtocol := GetRepoURLWithoutProtocol(artifactURL)
|
|
baseArtifact := registryevents.BaseArtifact{
|
|
Name: info.Image,
|
|
Ref: fmt.Sprintf("%s:%s", info.Image, tag),
|
|
}
|
|
if info.PackageType == artifact.PackageTypeDOCKER {
|
|
payload.Artifact = ®istryevents.DockerArtifact{
|
|
BaseArtifact: baseArtifact,
|
|
Tag: tag,
|
|
URL: urlWithoutProtocol,
|
|
Digest: digest,
|
|
}
|
|
} else if info.PackageType == artifact.PackageTypeHELM {
|
|
payload.Artifact = ®istryevents.HelmArtifact{
|
|
BaseArtifact: baseArtifact,
|
|
Tag: tag,
|
|
URL: ociPrefix + urlWithoutProtocol,
|
|
Digest: digest,
|
|
}
|
|
}
|
|
return payload
|
|
}
|
|
|
|
func GetArtifactDeletedPayload(
|
|
ctx context.Context,
|
|
principalID int64,
|
|
registryID int64,
|
|
regIdentifier string,
|
|
tag string,
|
|
digest string,
|
|
rootIdentifier string,
|
|
packageType artifact.PackageType,
|
|
image string,
|
|
urlProvider urlprovider.Provider,
|
|
) registryevents.ArtifactDeletedPayload {
|
|
payload := registryevents.ArtifactDeletedPayload{
|
|
RegistryID: registryID,
|
|
PrincipalID: principalID,
|
|
ArtifactType: packageType,
|
|
}
|
|
artifactURL := urlProvider.RegistryURL(ctx, rootIdentifier, regIdentifier) + "/" + image + ":" + tag
|
|
urlWithoutProtocol := GetRepoURLWithoutProtocol(artifactURL)
|
|
|
|
baseArtifact := registryevents.BaseArtifact{
|
|
Name: image,
|
|
Ref: fmt.Sprintf("%s:%s", image, tag),
|
|
}
|
|
if packageType == artifact.PackageTypeDOCKER {
|
|
payload.Artifact = ®istryevents.DockerArtifact{
|
|
BaseArtifact: baseArtifact,
|
|
Tag: tag,
|
|
Digest: digest,
|
|
URL: urlWithoutProtocol,
|
|
}
|
|
} else if packageType == artifact.PackageTypeHELM {
|
|
payload.Artifact = ®istryevents.HelmArtifact{
|
|
BaseArtifact: baseArtifact,
|
|
Tag: tag,
|
|
Digest: digest,
|
|
URL: ociPrefix + urlWithoutProtocol,
|
|
}
|
|
}
|
|
return payload
|
|
}
|
|
|
|
func GetRepoURLWithoutProtocol(registryURL string) string {
|
|
repoURL := registryURL
|
|
parsedURL, err := url.Parse(repoURL)
|
|
if err != nil {
|
|
log.Error().Stack().Err(err).Msg("Error parsing URL: ")
|
|
return ""
|
|
}
|
|
|
|
return parsedURL.Host + parsedURL.Path
|
|
}
|