mirror of
https://github.com/harness/drone.git
synced 2025-05-13 15:40:12 +08:00
[MISC] Expose Content Encoding as Enum across APIs (#359)
This commit is contained in:
parent
ba6645cdaa
commit
b44948bd97
@ -7,7 +7,6 @@ package gitrpc
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -30,11 +29,10 @@ func (FileAction) Enum() []interface{} {
|
|||||||
|
|
||||||
// CommitFileAction holds file operation data.
|
// CommitFileAction holds file operation data.
|
||||||
type CommitFileAction struct {
|
type CommitFileAction struct {
|
||||||
Action FileAction
|
Action FileAction
|
||||||
Path string
|
Path string
|
||||||
Payload []byte
|
Payload []byte
|
||||||
Encoding string
|
SHA string
|
||||||
SHA string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitFilesParams holds the data for file operations.
|
// CommitFilesParams holds the data for file operations.
|
||||||
@ -100,10 +98,7 @@ func (c *Client) CommitFiles(ctx context.Context, params *CommitFilesParams) (Co
|
|||||||
// send file content
|
// send file content
|
||||||
n := 0
|
n := 0
|
||||||
buffer := make([]byte, FileTransferChunkSize)
|
buffer := make([]byte, FileTransferChunkSize)
|
||||||
reader := io.Reader(bytes.NewReader(action.Payload))
|
reader := bytes.NewReader(action.Payload)
|
||||||
if action.Encoding == "base64" {
|
|
||||||
reader = base64.NewDecoder(base64.StdEncoding, reader)
|
|
||||||
}
|
|
||||||
for {
|
for {
|
||||||
n, err = reader.Read(buffer)
|
n, err = reader.Read(buffer)
|
||||||
if errors.Is(err, io.EOF) {
|
if errors.Is(err, io.EOF) {
|
||||||
|
@ -6,6 +6,7 @@ package repo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/harness/gitness/gitrpc"
|
"github.com/harness/gitness/gitrpc"
|
||||||
@ -17,11 +18,11 @@ import (
|
|||||||
|
|
||||||
// CommitFileAction holds file operation data.
|
// CommitFileAction holds file operation data.
|
||||||
type CommitFileAction struct {
|
type CommitFileAction struct {
|
||||||
Action gitrpc.FileAction `json:"action"`
|
Action gitrpc.FileAction `json:"action"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Payload string `json:"payload"`
|
Payload string `json:"payload"`
|
||||||
Encoding string `json:"encoding"`
|
Encoding enum.ContentEncodingType `json:"encoding"`
|
||||||
SHA string `json:"sha"`
|
SHA string `json:"sha"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitFilesOptions holds the data for file operations.
|
// CommitFilesOptions holds the data for file operations.
|
||||||
@ -51,12 +52,25 @@ func (c *Controller) CommitFiles(ctx context.Context, session *auth.Session,
|
|||||||
|
|
||||||
actions := make([]gitrpc.CommitFileAction, len(in.Actions))
|
actions := make([]gitrpc.CommitFileAction, len(in.Actions))
|
||||||
for i, action := range in.Actions {
|
for i, action := range in.Actions {
|
||||||
|
var rawPayload []byte
|
||||||
|
switch action.Encoding {
|
||||||
|
case enum.ContentEncodingTypeBase64:
|
||||||
|
rawPayload, err = base64.StdEncoding.DecodeString(action.Payload)
|
||||||
|
if err != nil {
|
||||||
|
return CommitFilesResponse{}, fmt.Errorf("failed to decode base64 payload: %w", err)
|
||||||
|
}
|
||||||
|
case enum.ContentEncodingTypeUTF8:
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
// by default we treat content as is
|
||||||
|
rawPayload = []byte(action.Payload)
|
||||||
|
}
|
||||||
|
|
||||||
actions[i] = gitrpc.CommitFileAction{
|
actions[i] = gitrpc.CommitFileAction{
|
||||||
Action: action.Action,
|
Action: action.Action,
|
||||||
Path: action.Path,
|
Path: action.Path,
|
||||||
Payload: []byte(action.Payload),
|
Payload: rawPayload,
|
||||||
Encoding: action.Encoding,
|
SHA: action.SHA,
|
||||||
SHA: action.SHA,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,21 +45,15 @@ type GetContentOutput struct {
|
|||||||
Content Content `json:"content"`
|
Content Content `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileEncodingType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
FileEncodingTypeBase64 FileEncodingType = "base64"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Content restricts the possible types of content returned by the api.
|
// Content restricts the possible types of content returned by the api.
|
||||||
type Content interface {
|
type Content interface {
|
||||||
isContent()
|
isContent()
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileContent struct {
|
type FileContent struct {
|
||||||
Encoding FileEncodingType `json:"encoding"`
|
Encoding enum.ContentEncodingType `json:"encoding"`
|
||||||
Data string `json:"data"`
|
Data string `json:"data"`
|
||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *FileContent) isContent() {}
|
func (c *FileContent) isContent() {}
|
||||||
@ -175,14 +169,12 @@ func (c *Controller) getFileContent(ctx context.Context, readParams gitrpc.ReadP
|
|||||||
SizeLimit: maxGetContentFileSize,
|
SizeLimit: maxGetContentFileSize,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: handle not found error
|
|
||||||
// This requires gitrpc to also return notfound though!
|
|
||||||
return nil, fmt.Errorf("failed to get file content: %w", err)
|
return nil, fmt.Errorf("failed to get file content: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &FileContent{
|
return &FileContent{
|
||||||
Size: output.Blob.Size,
|
Size: output.Blob.Size,
|
||||||
Encoding: FileEncodingTypeBase64,
|
Encoding: enum.ContentEncodingTypeBase64,
|
||||||
Data: base64.StdEncoding.EncodeToString(output.Blob.Content),
|
Data: base64.StdEncoding.EncodeToString(output.Blob.Content),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
23
types/enum/encoding.go
Normal file
23
types/enum/encoding.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// Copyright 2022 Harness Inc. All rights reserved.
|
||||||
|
// Use of this source code is governed by the Polyform Free Trial License
|
||||||
|
// that can be found in the LICENSE.md file for this repository.
|
||||||
|
|
||||||
|
package enum
|
||||||
|
|
||||||
|
// ContentEncodingType describes the encoding of content.
|
||||||
|
type ContentEncodingType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ContentEncodingTypeUTF8 describes utf-8 encoded content.
|
||||||
|
ContentEncodingTypeUTF8 ContentEncodingType = "utf8"
|
||||||
|
|
||||||
|
// ContentEncodingTypeBase64 describes base64 encoded content.
|
||||||
|
ContentEncodingTypeBase64 ContentEncodingType = "base64"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (ContentEncodingType) Enum() []interface{} { return toInterfaceSlice(contentEncodingTypes) }
|
||||||
|
|
||||||
|
var contentEncodingTypes = sortEnum([]ContentEncodingType{
|
||||||
|
ContentEncodingTypeUTF8,
|
||||||
|
ContentEncodingTypeBase64,
|
||||||
|
})
|
@ -7,6 +7,8 @@ import { getConfig } from '../config'
|
|||||||
export const SPEC_VERSION = '0.0.0'
|
export const SPEC_VERSION = '0.0.0'
|
||||||
export type EnumAccessGrant = number
|
export type EnumAccessGrant = number
|
||||||
|
|
||||||
|
export type EnumContentEncodingType = 'base64' | 'utf8'
|
||||||
|
|
||||||
export type EnumMergeCheckStatus = string
|
export type EnumMergeCheckStatus = string
|
||||||
|
|
||||||
export type EnumMergeMethod = 'merge' | 'rebase' | 'squash'
|
export type EnumMergeMethod = 'merge' | 'rebase' | 'squash'
|
||||||
@ -268,7 +270,7 @@ export interface RepoCommitDivergenceRequest {
|
|||||||
|
|
||||||
export interface RepoCommitFileAction {
|
export interface RepoCommitFileAction {
|
||||||
action?: GitrpcFileAction
|
action?: GitrpcFileAction
|
||||||
encoding?: string
|
encoding?: EnumContentEncodingType
|
||||||
path?: string
|
path?: string
|
||||||
payload?: string
|
payload?: string
|
||||||
sha?: string
|
sha?: string
|
||||||
@ -303,12 +305,10 @@ export type RepoContentType = string
|
|||||||
|
|
||||||
export interface RepoFileContent {
|
export interface RepoFileContent {
|
||||||
data?: string
|
data?: string
|
||||||
encoding?: RepoFileEncodingType
|
encoding?: EnumContentEncodingType
|
||||||
size?: number
|
size?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RepoFileEncodingType = string
|
|
||||||
|
|
||||||
export interface RepoMergeCheck {
|
export interface RepoMergeCheck {
|
||||||
mergeable?: boolean
|
mergeable?: boolean
|
||||||
}
|
}
|
||||||
|
@ -3547,6 +3547,11 @@ components:
|
|||||||
schemas:
|
schemas:
|
||||||
EnumAccessGrant:
|
EnumAccessGrant:
|
||||||
type: integer
|
type: integer
|
||||||
|
EnumContentEncodingType:
|
||||||
|
enum:
|
||||||
|
- base64
|
||||||
|
- utf8
|
||||||
|
type: string
|
||||||
EnumMergeCheckStatus:
|
EnumMergeCheckStatus:
|
||||||
type: string
|
type: string
|
||||||
EnumMergeMethod:
|
EnumMergeMethod:
|
||||||
@ -4011,7 +4016,7 @@ components:
|
|||||||
action:
|
action:
|
||||||
$ref: '#/components/schemas/GitrpcFileAction'
|
$ref: '#/components/schemas/GitrpcFileAction'
|
||||||
encoding:
|
encoding:
|
||||||
type: string
|
$ref: '#/components/schemas/EnumContentEncodingType'
|
||||||
path:
|
path:
|
||||||
type: string
|
type: string
|
||||||
payload:
|
payload:
|
||||||
@ -4062,12 +4067,10 @@ components:
|
|||||||
data:
|
data:
|
||||||
type: string
|
type: string
|
||||||
encoding:
|
encoding:
|
||||||
$ref: '#/components/schemas/RepoFileEncodingType'
|
$ref: '#/components/schemas/EnumContentEncodingType'
|
||||||
size:
|
size:
|
||||||
type: integer
|
type: integer
|
||||||
type: object
|
type: object
|
||||||
RepoFileEncodingType:
|
|
||||||
type: string
|
|
||||||
RepoMergeCheck:
|
RepoMergeCheck:
|
||||||
properties:
|
properties:
|
||||||
mergeable:
|
mergeable:
|
||||||
|
Loading…
Reference in New Issue
Block a user