mirror of
https://github.com/harness/drone.git
synced 2025-05-15 16:40:06 +08:00
[MISC] Minor Change in Import/Export Progress Reporting (#509)
This commit is contained in:
parent
077cd2c74d
commit
682efb9dba
@ -6,9 +6,13 @@ package repo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
apiauth "github.com/harness/gitness/internal/api/auth"
|
apiauth "github.com/harness/gitness/internal/api/auth"
|
||||||
|
"github.com/harness/gitness/internal/api/usererror"
|
||||||
"github.com/harness/gitness/internal/auth"
|
"github.com/harness/gitness/internal/auth"
|
||||||
|
"github.com/harness/gitness/internal/services/importer"
|
||||||
"github.com/harness/gitness/types"
|
"github.com/harness/gitness/types"
|
||||||
"github.com/harness/gitness/types/enum"
|
"github.com/harness/gitness/types/enum"
|
||||||
)
|
)
|
||||||
@ -28,5 +32,13 @@ func (c *Controller) ImportProgress(ctx context.Context,
|
|||||||
return types.JobProgress{}, err
|
return types.JobProgress{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.importer.GetProgress(ctx, repo)
|
progress, err := c.importer.GetProgress(ctx, repo)
|
||||||
|
if errors.Is(err, importer.ErrNotFound) {
|
||||||
|
return types.JobProgress{}, usererror.NotFound("No recent or ongoing import found for repository.")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return types.JobProgress{}, fmt.Errorf("failed to retrieve import progress: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return progress, err
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,15 @@ package space
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
apiauth "github.com/harness/gitness/internal/api/auth"
|
apiauth "github.com/harness/gitness/internal/api/auth"
|
||||||
"github.com/harness/gitness/internal/api/usererror"
|
"github.com/harness/gitness/internal/api/usererror"
|
||||||
"github.com/harness/gitness/internal/auth"
|
"github.com/harness/gitness/internal/auth"
|
||||||
|
"github.com/harness/gitness/internal/services/exporter"
|
||||||
"github.com/harness/gitness/types"
|
"github.com/harness/gitness/types"
|
||||||
"github.com/harness/gitness/types/enum"
|
"github.com/harness/gitness/types/enum"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExportProgressOutput struct {
|
type ExportProgressOutput struct {
|
||||||
@ -18,24 +21,23 @@ type ExportProgressOutput struct {
|
|||||||
func (c *Controller) ExportProgress(ctx context.Context,
|
func (c *Controller) ExportProgress(ctx context.Context,
|
||||||
session *auth.Session,
|
session *auth.Session,
|
||||||
spaceRef string,
|
spaceRef string,
|
||||||
) (*ExportProgressOutput, error) {
|
) (ExportProgressOutput, error) {
|
||||||
space, err := c.spaceStore.FindByRef(ctx, spaceRef)
|
space, err := c.spaceStore.FindByRef(ctx, spaceRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return ExportProgressOutput{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = apiauth.CheckSpace(ctx, c.authorizer, session, space, enum.PermissionSpaceView, false); err != nil {
|
if err = apiauth.CheckSpace(ctx, c.authorizer, session, space, enum.PermissionSpaceView, false); err != nil {
|
||||||
return nil, err
|
return ExportProgressOutput{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
progress, err := c.exporter.GetProgressForSpace(ctx, space.ID)
|
progress, err := c.exporter.GetProgressForSpace(ctx, space.ID)
|
||||||
|
if errors.Is(err, exporter.ErrNotFound) {
|
||||||
|
return ExportProgressOutput{}, usererror.NotFound("No recent or ongoing export found for space.")
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return ExportProgressOutput{}, fmt.Errorf("failed to retrieve export progress: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(progress) == 0 {
|
return ExportProgressOutput{Repos: progress}, nil
|
||||||
return nil, usererror.NotFound("No ongoing export found for space.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ExportProgressOutput{Repos: progress}, nil
|
|
||||||
}
|
}
|
||||||
|
@ -12,11 +12,12 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/harness/gitness/internal/api/controller/repo"
|
|
||||||
"github.com/harness/gitness/types"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/harness/gitness/internal/api/controller/repo"
|
||||||
|
"github.com/harness/gitness/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -27,16 +28,16 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrNotFound = fmt.Errorf("not found")
|
errHTTPNotFound = fmt.Errorf("not found")
|
||||||
ErrBadRequest = fmt.Errorf("bad request")
|
errHTTPBadRequest = fmt.Errorf("bad request")
|
||||||
ErrInternal = fmt.Errorf("internal error")
|
errHTTPInternal = fmt.Errorf("internal error")
|
||||||
)
|
)
|
||||||
|
|
||||||
type HarnessCodeClient struct {
|
type harnessCodeClient struct {
|
||||||
client *Client
|
client *client
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type client struct {
|
||||||
baseURL string
|
baseURL string
|
||||||
httpClient http.Client
|
httpClient http.Client
|
||||||
|
|
||||||
@ -47,8 +48,8 @@ type Client struct {
|
|||||||
token string
|
token string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient creates a new harness Client for interacting with the platforms APIs.
|
// newClient creates a new harness Client for interacting with the platforms APIs.
|
||||||
func NewClient(baseURL string, accountID string, orgId string, projectId string, token string) (*Client, error) {
|
func newClient(baseURL string, accountID string, orgId string, projectId string, token string) (*client, error) {
|
||||||
if baseURL == "" {
|
if baseURL == "" {
|
||||||
return nil, fmt.Errorf("baseUrl required")
|
return nil, fmt.Errorf("baseUrl required")
|
||||||
}
|
}
|
||||||
@ -65,7 +66,7 @@ func NewClient(baseURL string, accountID string, orgId string, projectId string,
|
|||||||
return nil, fmt.Errorf("token required")
|
return nil, fmt.Errorf("token required")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Client{
|
return &client{
|
||||||
baseURL: baseURL,
|
baseURL: baseURL,
|
||||||
accountId: accountID,
|
accountId: accountID,
|
||||||
orgId: orgId,
|
orgId: orgId,
|
||||||
@ -81,17 +82,17 @@ func NewClient(baseURL string, accountID string, orgId string, projectId string,
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHarnessCodeClient(baseUrl string, accountID string, orgId string, projectId string, token string) (*HarnessCodeClient, error) {
|
func newHarnessCodeClient(baseUrl string, accountID string, orgId string, projectId string, token string) (*harnessCodeClient, error) {
|
||||||
client, err := NewClient(baseUrl, accountID, orgId, projectId, token)
|
client, err := newClient(baseUrl, accountID, orgId, projectId, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &HarnessCodeClient{
|
return &harnessCodeClient{
|
||||||
client: client,
|
client: client,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *HarnessCodeClient) CreateRepo(ctx context.Context, input repo.CreateInput) (*types.Repository, error) {
|
func (c *harnessCodeClient) CreateRepo(ctx context.Context, input repo.CreateInput) (*types.Repository, error) {
|
||||||
path := fmt.Sprintf(pathCreateRepo, c.client.accountId, c.client.orgId, c.client.projectId)
|
path := fmt.Sprintf(pathCreateRepo, c.client.accountId, c.client.orgId, c.client.projectId)
|
||||||
bodyBytes, err := json.Marshal(input)
|
bodyBytes, err := json.Marshal(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -140,7 +141,7 @@ func addQueryParams(req *http.Request, params map[string]string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *HarnessCodeClient) DeleteRepo(ctx context.Context, repoUid string) error {
|
func (c *harnessCodeClient) DeleteRepo(ctx context.Context, repoUid string) error {
|
||||||
path := fmt.Sprintf(pathDeleteRepo, c.client.accountId, c.client.orgId, c.client.projectId, repoUid)
|
path := fmt.Sprintf(pathDeleteRepo, c.client.accountId, c.client.orgId, c.client.projectId, repoUid)
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, appendPath(c.client.baseURL, path), nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, appendPath(c.client.baseURL, path), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -168,7 +169,7 @@ func appendPath(uri string, path string) string {
|
|||||||
return strings.TrimRight(uri, "/") + "/" + strings.TrimLeft(path, "/")
|
return strings.TrimRight(uri, "/") + "/" + strings.TrimLeft(path, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Do(r *http.Request) (*http.Response, error) {
|
func (c *client) Do(r *http.Request) (*http.Response, error) {
|
||||||
addAuthHeader(r, c.token)
|
addAuthHeader(r, c.token)
|
||||||
return c.httpClient.Do(r)
|
return c.httpClient.Do(r)
|
||||||
}
|
}
|
||||||
@ -196,13 +197,13 @@ func unmarshalResponse(resp *http.Response, data interface{}) error {
|
|||||||
func mapStatusCodeToError(statusCode int) error {
|
func mapStatusCodeToError(statusCode int) error {
|
||||||
switch {
|
switch {
|
||||||
case statusCode == 500:
|
case statusCode == 500:
|
||||||
return ErrInternal
|
return errHTTPInternal
|
||||||
case statusCode >= 500:
|
case statusCode >= 500:
|
||||||
return fmt.Errorf("received server side error status code %d", statusCode)
|
return fmt.Errorf("received server side error status code %d", statusCode)
|
||||||
case statusCode == 404:
|
case statusCode == 404:
|
||||||
return ErrNotFound
|
return errHTTPNotFound
|
||||||
case statusCode == 400:
|
case statusCode == 400:
|
||||||
return ErrBadRequest
|
return errHTTPBadRequest
|
||||||
case statusCode >= 400:
|
case statusCode >= 400:
|
||||||
return fmt.Errorf("received client side error status code %d", statusCode)
|
return fmt.Errorf("received client side error status code %d", statusCode)
|
||||||
case statusCode >= 300:
|
case statusCode >= 300:
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@ -26,6 +27,11 @@ import (
|
|||||||
"github.com/harness/gitness/types"
|
"github.com/harness/gitness/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrNotFound is returned if no export data was found.
|
||||||
|
ErrNotFound = errors.New("export not found")
|
||||||
|
)
|
||||||
|
|
||||||
type Repository struct {
|
type Repository struct {
|
||||||
urlProvider *gitnessurl.Provider
|
urlProvider *gitnessurl.Provider
|
||||||
git gitrpc.Interface
|
git gitrpc.Interface
|
||||||
@ -117,7 +123,7 @@ func (r *Repository) Handle(ctx context.Context, data string, _ job.ProgressRepo
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
harnessCodeInfo := input.HarnessCodeInfo
|
harnessCodeInfo := input.HarnessCodeInfo
|
||||||
client, err := NewHarnessCodeClient(r.urlProvider.GetHarnessCodeInternalUrl(), harnessCodeInfo.AccountId, harnessCodeInfo.OrgIdentifier, harnessCodeInfo.ProjectIdentifier, harnessCodeInfo.Token)
|
client, err := newHarnessCodeClient(r.urlProvider.GetHarnessCodeInternalUrl(), harnessCodeInfo.AccountId, harnessCodeInfo.OrgIdentifier, harnessCodeInfo.ProjectIdentifier, harnessCodeInfo.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -202,6 +208,11 @@ func (r *Repository) GetProgressForSpace(ctx context.Context, spaceID int64) ([]
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get job progress for group: %w", err)
|
return nil, fmt.Errorf("failed to get job progress for group: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(progress) == 0 {
|
||||||
|
return nil, ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
return progress, nil
|
return progress, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,6 +35,11 @@ const (
|
|||||||
importJobMaxDuration = 45 * time.Minute
|
importJobMaxDuration = 45 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrNotFound is returned if no import data was found.
|
||||||
|
ErrNotFound = errors.New("import not found")
|
||||||
|
)
|
||||||
|
|
||||||
type Repository struct {
|
type Repository struct {
|
||||||
defaultBranch string
|
defaultBranch string
|
||||||
urlProvider *gitnessurl.Provider
|
urlProvider *gitnessurl.Provider
|
||||||
@ -268,15 +273,15 @@ func (r *Repository) Handle(ctx context.Context, data string, _ job.ProgressRepo
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) GetProgress(ctx context.Context, repo *types.Repository) (types.JobProgress, error) {
|
func (r *Repository) GetProgress(ctx context.Context, repo *types.Repository) (types.JobProgress, error) {
|
||||||
if !repo.Importing {
|
|
||||||
// if the repo is not being imported, or it's job ID has been cleared (or never existed) return state=finished
|
|
||||||
return job.DoneProgress(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
progress, err := r.scheduler.GetJobProgress(ctx, JobIDFromRepoID(repo.ID))
|
progress, err := r.scheduler.GetJobProgress(ctx, JobIDFromRepoID(repo.ID))
|
||||||
if errors.Is(err, gitness_store.ErrResourceNotFound) {
|
if errors.Is(err, gitness_store.ErrResourceNotFound) {
|
||||||
// if the job is not found return state=failed
|
if repo.Importing {
|
||||||
return job.FailProgress(), nil
|
// if the job is not found but repo is marked as importing, return state=failed
|
||||||
|
return job.FailProgress(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise there either was no import, or it completed a long time ago (job cleaned up by now)
|
||||||
|
return types.JobProgress{}, ErrNotFound
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.JobProgress{}, fmt.Errorf("failed to get job progress: %w", err)
|
return types.JobProgress{}, fmt.Errorf("failed to get job progress: %w", err)
|
||||||
|
@ -141,15 +141,6 @@ func (e *Executor) exec(
|
|||||||
return exec.Handle(ctx, input, progressReporter) // runs the job
|
return exec.Handle(ctx, input, progressReporter) // runs the job
|
||||||
}
|
}
|
||||||
|
|
||||||
func DoneProgress() types.JobProgress {
|
|
||||||
return types.JobProgress{
|
|
||||||
State: enum.JobStateFinished,
|
|
||||||
Progress: ProgressMax,
|
|
||||||
Result: "",
|
|
||||||
Failure: "",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func FailProgress() types.JobProgress {
|
func FailProgress() types.JobProgress {
|
||||||
return types.JobProgress{
|
return types.JobProgress{
|
||||||
State: enum.JobStateFailed,
|
State: enum.JobStateFailed,
|
||||||
|
Loading…
Reference in New Issue
Block a user