protected override void CacheMetadata(CodeActivityMetadata metadata)
        {
            if (WorkspaceUUIDOrSlug == null)
            {
                metadata.AddValidationError(string.Format(Resources.ValidationValue_Error, nameof(WorkspaceUUIDOrSlug)));
            }
            if (RepositoryUUIDOrSlug == null)
            {
                metadata.AddValidationError(string.Format(Resources.ValidationValue_Error, nameof(RepositoryUUIDOrSlug)));
            }

            // When Create repository (POST) is selected make sure user is using slug not UUID
            if (Action == RequestTypes.POST && Validation.IsUUID(RepositoryUUIDOrSlug.GetArgumentLiteralValue()))
            {
                metadata.AddValidationError(string.Format(Resources.ValidationValue_Error_ManageRepository_RepositoryUUIDOrSlug_NotUUID, nameof(RepositoryUUIDOrSlug)));
            }

            // Check for valid UUID or Slug value
            if (RepositoryUUIDOrSlug.GetArgumentLiteralValue() == "")
            {
                metadata.AddValidationError(Resources.ValidationValue_Error_ManageRepository_RepositoryUUIDOrSlug);
            }

            base.CacheMetadata(metadata);
        }
示例#2
0
        protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            // Object Container: Use objectContainer.Get<T>() to retrieve objects from the scope
            var objectContainer = context.GetFromContext <IObjectContainer>(BitbucketAPIScope.ParentContainerPropertyTag);
            var client          = objectContainer.Get <FluentClient>();

            // Inputs
            var repositoryLocation   = RepositoryLocation.Get(context);
            var fileToUpload         = FileToUpload.Get(context);
            var commitMessage        = CommitMessage.Get(context);
            var repositoryUUIDOrSlug = RepositoryUUIDOrSlug.Get(context);
            var workspaceUUIDOrSlug  = WorkspaceUUIDOrSlug.Get(context);
            var branchName           = BranchName.Get(context);

            // Validate whether Workspace UUID or Name provided (assume name will never be a GUID format)
            if (Validation.IsUUID(workspaceUUIDOrSlug))
            {
                HttpUtility.UrlEncode(workspaceUUIDOrSlug);
            }

            // Validate whether Repository UUID or Slug provided (assume slug will never be a GUID format)
            if (Validation.IsUUID(repositoryUUIDOrSlug))
            {
                HttpUtility.UrlEncode(repositoryUUIDOrSlug);
            }

            // Create standard request URI
            var uri = "repositories/" + workspaceUUIDOrSlug + "/" + repositoryUUIDOrSlug + "/src";

            // Initialise and populate multipart content
            var multipartContent = new MultipartFormDataContent();

            multipartContent.Add(new ByteArrayContent(File.ReadAllBytes(fileToUpload)), repositoryLocation, Path.GetFileName(fileToUpload));
            multipartContent.Add(new StringContent(commitMessage), "message");

            // Check if optional branch name parameter provided. Add to request if not null.
            if (branchName != null)
            {
                multipartContent.Add(new StringContent(branchName), "branch");
            }

            // Execution Logic
            var response         = new JObject();
            var exceptionHandler = new ApiExceptionHandler();

            try
            {
                response = await AsyncRequests.PostRequest_WithBody(client, uri, cancellationToken, multipartContent);
            }
            catch (ApiException ex) // Catches any API exception and returns the message
            {
                await exceptionHandler.ParseExceptionAsync(ex);
            }

            // Outputs - API response as JObject
            return((ctx) =>
            {
                JsonResult.Set(ctx, response);
            });
        }
        protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            // Object Container: Use objectContainer.Get<T>() to retrieve objects from the scope
            var objectContainer = context.GetFromContext <IObjectContainer>(BitbucketAPIScope.ParentContainerPropertyTag);
            var client          = objectContainer.Get <FluentClient>();

            // Inputs
            var workspaceUUIDOrSlug  = WorkspaceUUIDOrSlug.Get(context);
            var repositoryUUIDOrSlug = RepositoryUUIDOrSlug.Get(context);
            var requestType          = Enum.GetName(typeof(RequestTypes), (int)Action);

            // Validate whether Workspace UUID or Slug provided (assume name will never be a GUID format)
            if (Validation.IsUUID(workspaceUUIDOrSlug))
            {
                HttpUtility.UrlEncode(workspaceUUIDOrSlug);
            }

            // Validate whether Repository UUID or Slug provided (assume slug will never be a GUID format)
            if (Validation.IsUUID(repositoryUUIDOrSlug))
            {
                HttpUtility.UrlEncode(repositoryUUIDOrSlug);
            }

            // Create standard request URI
            var uri = "repositories/" + workspaceUUIDOrSlug + "/" + repositoryUUIDOrSlug;

            // Initialise
            var response         = new JObject();
            var exceptionHandler = new ApiExceptionHandler();

            // Execution Logic for all types of API requests available for this endpoint
            try
            {
                switch (requestType)
                {
                case "GET":
                {
                    response = await AsyncRequests.GetRequest(client, uri, cancellationToken);

                    break;
                }

                case "POST":
                {
                    response = await AsyncRequests.PostRequest_NoBody(client, uri, cancellationToken);

                    break;
                }

                case "DELETE":
                {
                    response = await AsyncRequests.DeleteRequest(client, uri, cancellationToken);

                    break;
                }
                }
            }
            catch (ApiException ex) // Catches any API exception and returns the message
            {
                await exceptionHandler.ParseExceptionAsync(ex);
            }

            // Outputs - API response as JObject
            return((ctx) =>
            {
                JsonResult.Set(ctx, response);
            });
        }