Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upload results-spreadsheet certsuite sub-command #2389

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ temp/
/all-releases.txt
/latest-release-tag.txt
/release-tag.txt

cmd/certsuite/upload/results_spreadsheet/credentials.json
cmd/certsuite/upload/results_spreadsheet/token.json
2 changes: 2 additions & 0 deletions cmd/certsuite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite/generate"
"github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite/info"
"github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite/run"
"github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite/upload"
"github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite/version"
)

Expand All @@ -26,6 +27,7 @@ func newRootCmd() *cobra.Command {
rootCmd.AddCommand(run.NewCommand())
rootCmd.AddCommand(info.NewCommand())
rootCmd.AddCommand(version.NewCommand())
rootCmd.AddCommand(upload.NewCommand())

return &rootCmd
}
Expand Down
77 changes: 77 additions & 0 deletions cmd/certsuite/upload/results_spreadsheet/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package resultsspreadsheet

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"

"golang.org/x/oauth2"
)

const tokenPermissions = 0o600

// Retrieve a token, saves the token, then returns the generated client.
func getClient(config *oauth2.Config) (*http.Client, error) {
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
tokFile := "cmd/certsuite/upload/results_spreadsheet/token.json"
tok, err := tokenFromFile(tokFile)
if err != nil {
tok, err = getTokenFromWeb(config)
if err != nil {
return nil, err
}
if err := saveToken(tokFile, tok); err != nil {
return nil, err
}
}
return config.Client(context.Background(), tok), nil
}

// Request a token from the web, then returns the retrieved token.
func getTokenFromWeb(config *oauth2.Config) (*oauth2.Token, error) {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the "+
"authorization code: \n%v\n", authURL)

var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
return nil, fmt.Errorf("unable to read authorization code: %v", err)
}

tok, err := config.Exchange(context.TODO(), authCode)
if err != nil {
return nil, fmt.Errorf("unable to retrieve token from web: %v", err)
}
return tok, nil
}

// Retrieves a token from a local file.
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}

// Saves a token to a file path.
func saveToken(path string, token *oauth2.Token) error {
fmt.Printf("Saving credential file to: %s\n", path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, tokenPermissions)
if err != nil {
return fmt.Errorf("unable to cache oauth token: %v", err)
}
defer f.Close()
err = json.NewEncoder(f).Encode(token)
if err != nil {
return fmt.Errorf("unable to encode token: %v", err)
}
return nil
}
78 changes: 78 additions & 0 deletions cmd/certsuite/upload/results_spreadsheet/drive_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package resultsspreadsheet

import (
"fmt"
"log"
"net/url"
"strings"

"google.golang.org/api/drive/v3"
"google.golang.org/api/sheets/v4"
)

func createDriveFolder(srv *drive.Service, folderName, parentFolderID string) (*drive.File, error) {
driveFolder := &drive.File{
Name: folderName,
Parents: []string{parentFolderID},
MimeType: "application/vnd.google-apps.folder",
}

// Search for an existing folder with the same name
q := fmt.Sprintf("name = '%s' and mimeType = 'application/vnd.google-apps.folder' and '%s' in parents and trashed = false", folderName, parentFolderID)
call := srv.Files.List().Q(q).Fields("files(id, name)")

files, err := call.Do()
if err != nil {
return nil, fmt.Errorf("unable to list files: %v", err)
}

if len(files.Files) > 0 {
return nil, fmt.Errorf("folder %s already exists in %s folder ID", folderName, parentFolderID)
}

createdFolder, err := srv.Files.Create(driveFolder).Do()
if err != nil {
return nil, fmt.Errorf("unable to create folder: %v", err)
}

return createdFolder, nil
}

func MoveSpreadSheetToFolder(srv *drive.Service, folder *drive.File, spreadsheet *sheets.Spreadsheet) error {
file, err := srv.Files.Get(spreadsheet.SpreadsheetId).Fields("parents").Do()
if err != nil {
log.Fatalf("Unable to get file: %v", err)
}

// Collect the current parent IDs to remove (if needed)
oldParents := append([]string{}, file.Parents...)

updateCall := srv.Files.Update(spreadsheet.SpreadsheetId, nil)
updateCall.AddParents(folder.Id)

// Remove the file from its old parents
if len(oldParents) > 0 {
for _, parent := range oldParents {
updateCall.RemoveParents(parent)
}
}

_, err = updateCall.Do()
if err != nil {
log.Fatalf("Unable change file location: %v", err)
}

return nil
}

func extractFolderIDFromURL(u string) (string, error) {
parsedURL, err := url.Parse(u)
if err != nil {
return "", err
}

pathSegments := strings.Split(parsedURL.Path, "/")
sebrandon1 marked this conversation as resolved.
Show resolved Hide resolved

// The folder ID is the last segment in the path
return pathSegments[len(pathSegments)-1], nil
}
Loading
Loading