From 5143dcca66f09ca60197c940e78af2f320abdead Mon Sep 17 00:00:00 2001 From: Stephen Augustus Date: Mon, 25 May 2020 00:31:31 -0400 Subject: [PATCH] pkg/gh2gcs: Initial commit Signed-off-by: Stephen Augustus --- BUILD.bazel | 1 + cmd/gh2gcs/cmd/BUILD.bazel | 1 + cmd/gh2gcs/cmd/root.go | 25 +++++++++++++++--- pkg/gh2gcs/BUILD.bazel | 26 +++++++++++++++++++ pkg/gh2gcs/config.go | 52 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 pkg/gh2gcs/BUILD.bazel create mode 100644 pkg/gh2gcs/config.go diff --git a/BUILD.bazel b/BUILD.bazel index 4939349aafb0..f0f5f2b5a593 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -37,6 +37,7 @@ filegroup( "//lib:all-srcs", "//pkg/command:all-srcs", "//pkg/gcp:all-srcs", + "//pkg/gh2gcs:all-srcs", "//pkg/git:all-srcs", "//pkg/github:all-srcs", "//pkg/http:all-srcs", diff --git a/cmd/gh2gcs/cmd/BUILD.bazel b/cmd/gh2gcs/cmd/BUILD.bazel index 3e00cce5e5d7..845732bf84cb 100644 --- a/cmd/gh2gcs/cmd/BUILD.bazel +++ b/cmd/gh2gcs/cmd/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "k8s.io/release/cmd/gh2gcs/cmd", visibility = ["//visibility:public"], deps = [ + "//pkg/gh2gcs:go_default_library", "//pkg/github:go_default_library", "//pkg/log:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", diff --git a/cmd/gh2gcs/cmd/root.go b/cmd/gh2gcs/cmd/root.go index 2fd8b6f6dccd..5ca18d6e15df 100644 --- a/cmd/gh2gcs/cmd/root.go +++ b/cmd/gh2gcs/cmd/root.go @@ -22,6 +22,7 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "k8s.io/release/pkg/gh2gcs" "k8s.io/release/pkg/github" "k8s.io/release/pkg/log" ) @@ -125,11 +126,29 @@ func run(opts *options) error { // TODO: Support downloading releases via yaml config // TODO: Support single release or multi-release scenarios - if err := gh.DownloadReleaseAssets(opts.org, opts.repo, opts.tag, opts.outputDir); err != nil { - return err + uploadConfig := &gh2gcs.UploadConfig{} + + if opts.tag != "" { + repoConfig := &gh2gcs.RepoConfig{ + Org: opts.org, + Name: opts.repo, + ReleaseTags: []string{ + opts.tag, + }, + } + + uploadConfig.RepoConfigs = append(uploadConfig.RepoConfigs, *repoConfig) } - // TODO: Add GCS upload logic + for _, rc := range uploadConfig.RepoConfigs { + if err := gh2gcs.DownloadReleases(&rc, gh, opts.outputDir); err != nil { + return err + } + + if err := gh2gcs.UploadToGCS(&rc, gh, opts.outputDir); err != nil { + return err + } + } return nil } diff --git a/pkg/gh2gcs/BUILD.bazel b/pkg/gh2gcs/BUILD.bazel new file mode 100644 index 000000000000..d01afe95f13e --- /dev/null +++ b/pkg/gh2gcs/BUILD.bazel @@ -0,0 +1,26 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["config.go"], + importpath = "k8s.io/release/pkg/gh2gcs", + visibility = ["//visibility:public"], + deps = [ + "//pkg/github:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/pkg/gh2gcs/config.go b/pkg/gh2gcs/config.go new file mode 100644 index 000000000000..af49dbee0bdb --- /dev/null +++ b/pkg/gh2gcs/config.go @@ -0,0 +1,52 @@ +/* +Copyright 2020 The Kubernetes 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. +*/ + +package gh2gcs + +import ( + "github.com/sirupsen/logrus" + "k8s.io/release/pkg/github" +) + +type UploadConfig struct { + RepoConfigs []RepoConfig +} + +type RepoConfig struct { + Org string + Name string + ReleaseTags []string + GCSBucket string + ReleaseDir string +} + +func DownloadReleases(repoCfg *RepoConfig, ghClient *github.GitHub, outputDir string) error { + tags := repoCfg.ReleaseTags + + for _, tag := range tags { + if err := ghClient.DownloadReleaseAssets(repoCfg.Org, repoCfg.Name, tag, outputDir); err != nil { + return err + } + } + + return nil +} + +// TODO: Add GCS upload logic +func UploadToGCS(repoCfg *RepoConfig, ghClient *github.GitHub, outputDir string) error { + logrus.Info("Uploading to GCS...") + return nil +}