bbende commented on code in PR #8765: URL: https://github.com/apache/nifi/pull/8765#discussion_r1594106906
########## nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/main/java/org/apache/nifi/github/GitHubFlowRegistryClient.java: ########## @@ -0,0 +1,633 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.nifi.github; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.databind.type.TypeFactory; +import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.flow.ConnectableComponent; +import org.apache.nifi.flow.Position; +import org.apache.nifi.flow.VersionedComponent; +import org.apache.nifi.flow.VersionedConnection; +import org.apache.nifi.flow.VersionedFlowCoordinates; +import org.apache.nifi.flow.VersionedProcessGroup; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.registry.flow.AbstractFlowRegistryClient; +import org.apache.nifi.registry.flow.BucketLocation; +import org.apache.nifi.registry.flow.FlowLocation; +import org.apache.nifi.registry.flow.FlowRegistryBranch; +import org.apache.nifi.registry.flow.FlowRegistryBucket; +import org.apache.nifi.registry.flow.FlowRegistryClientConfigurationContext; +import org.apache.nifi.registry.flow.FlowRegistryException; +import org.apache.nifi.registry.flow.FlowRegistryPermissions; +import org.apache.nifi.registry.flow.FlowVersionLocation; +import org.apache.nifi.registry.flow.RegisterAction; +import org.apache.nifi.registry.flow.RegisteredFlow; +import org.apache.nifi.registry.flow.RegisteredFlowSnapshot; +import org.apache.nifi.registry.flow.RegisteredFlowSnapshotMetadata; +import org.kohsuke.github.GHCommit; +import org.kohsuke.github.GHContent; +import org.kohsuke.github.GHContentUpdateResponse; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; + +/** + * Implementation of {@link org.apache.nifi.registry.flow.FlowRegistryClient} that uses GitHub for version controlling flows. + */ +public class GitHubFlowRegistryClient extends AbstractFlowRegistryClient { + + static final PropertyDescriptor GITHUB_API_URL = new PropertyDescriptor.Builder() + .name("GitHub API URL") + .description("The URL of the GitHub API") + .addValidator(StandardValidators.URL_VALIDATOR) + .defaultValue("https://api.github.com/") + .required(true) + .build(); + + static final PropertyDescriptor REPOSITORY_NAME = new PropertyDescriptor.Builder() + .name("Repository Name") + .description("The name of the repository") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .required(true) + .build(); + + static final PropertyDescriptor REPOSITORY_OWNER = new PropertyDescriptor.Builder() + .name("Repository Owner") + .description("The owner of the repository") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .required(true) + .build(); + + static final PropertyDescriptor REPOSITORY_BRANCH = new PropertyDescriptor.Builder() + .name("Default Branch") + .description("The default branch to use for this client") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .defaultValue("main") + .required(true) + .build(); + + static final PropertyDescriptor REPOSITORY_PATH = new PropertyDescriptor.Builder() + .name("Repository Path") + .description("The path with in the repository that this client will use to store all data. " + + "If left blank, then the root of the repository will be used.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .required(false) + .build(); + + static final PropertyDescriptor AUTHENTICATION_TYPE = new PropertyDescriptor.Builder() + .name("Authentication Type") + .description("The type of authentication to use for accessing GitHub") + .allowableValues(GitHubAuthenticationType.values()) + .defaultValue(GitHubAuthenticationType.NONE.name()) + .required(true) + .build(); + + static final PropertyDescriptor PERSONAL_ACCESS_TOKEN = new PropertyDescriptor.Builder() + .name("Personal Access Token") + .description("The personal access token to use for authentication") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .required(true) + .sensitive(true) + .dependsOn(AUTHENTICATION_TYPE, GitHubAuthenticationType.PERSONAL_ACCESS_TOKEN.name()) + .build(); + + static final PropertyDescriptor APP_INSTALLATION_TOKEN = new PropertyDescriptor.Builder() + .name("App Installation Token") + .description("The app installation token to use for authentication") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .required(true) + .sensitive(true) + .dependsOn(AUTHENTICATION_TYPE, GitHubAuthenticationType.APP_INSTALLATION_TOKEN.name()) + .build(); + + static final List<PropertyDescriptor> PROPERTY_DESCRIPTORS = List.of( + GITHUB_API_URL, + REPOSITORY_OWNER, + REPOSITORY_NAME, + REPOSITORY_BRANCH, + REPOSITORY_PATH, + AUTHENTICATION_TYPE, + PERSONAL_ACCESS_TOKEN, + APP_INSTALLATION_TOKEN + ); + + private static final ObjectMapper OBJECT_MAPPER = JsonMapper.builder() + .serializationInclusion(JsonInclude.Include.NON_NULL) + .defaultPropertyInclusion(JsonInclude.Value.construct(JsonInclude.Include.NON_NULL, JsonInclude.Include.NON_NULL)) + .annotationIntrospector(new JakartaXmlBindAnnotationIntrospector(TypeFactory.defaultInstance())) + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true) + .enable(SerializationFeature.INDENT_OUTPUT) + .addModule(new VersionedComponentModule()) + .build(); + + static final String DEFAULT_BUCKET_NAME = "default"; + static final String DEFAULT_BUCKET_KEEP_FILE_PATH = DEFAULT_BUCKET_NAME + "/.keep"; + static final String DEFAULT_BUCKET_KEEP_FILE_CONTENT = "Do Not Delete"; + static final String DEFAULT_BUCKET_KEEP_FILE_MESSAGE = "Creating Default bucket"; + + static final String REGISTER_FLOW_COMMENT = "Register Flow"; + static final String DEREGISTER_FLOW_COMMENT = "Deregister Flow"; + static final String DEFAULT_FLOW_SNAPSHOT_COMMIT_MESSAGE = "Saving Flow Snapshot"; + static final String SNAPSHOT_FILE_EXTENSION = ".json"; + static final String SNAPSHOT_FILE_PATH_FORMAT = "%s/%s" + SNAPSHOT_FILE_EXTENSION; + static final String FLOW_CONTENTS_GROUP_ID = "flow-contents-group"; + + private volatile GitHubRepositoryClient repositoryClient; + private final AtomicBoolean initialized = new AtomicBoolean(false); + + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTY_DESCRIPTORS; + } + + @Override + protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) { + final List<ValidationResult> results = new ArrayList<>(super.customValidate(validationContext)); + + final String repoPath = validationContext.getProperty(REPOSITORY_PATH).getValue(); + if (repoPath != null && (repoPath.startsWith("/") || repoPath.endsWith("/"))) { + results.add(new ValidationResult.Builder() + .subject(REPOSITORY_PATH.getDisplayName()) + .valid(false) + .explanation("Path can not start or end with /") + .build()); + } + + return results; + } + + @Override + public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) { + super.onPropertyModified(descriptor, oldValue, newValue); + synchronized (this) { + initialized.set(false); + } + } + + @Override + public boolean isStorageLocationApplicable(final FlowRegistryClientConfigurationContext context, final String location) { + return false; + } + + @Override + public boolean isBranchingSupported(final FlowRegistryClientConfigurationContext context) { + return true; + } + + @Override + public Set<FlowRegistryBranch> getBranches(final FlowRegistryClientConfigurationContext context) throws FlowRegistryException, IOException { + final GitHubRepositoryClient repositoryClient = getRepositoryClient(context); + return repositoryClient.getBranches().stream() + .map(branchName -> { + final FlowRegistryBranch flowRegistryBranch = new FlowRegistryBranch(); + flowRegistryBranch.setName(branchName); + return flowRegistryBranch; + }).collect(Collectors.toSet()); + } + + @Override + public FlowRegistryBranch getDefaultBranch(final FlowRegistryClientConfigurationContext context) { + final FlowRegistryBranch defaultBranch = new FlowRegistryBranch(); + defaultBranch.setName(context.getProperty(REPOSITORY_BRANCH).getValue()); + return defaultBranch; + } + + @Override + public Set<FlowRegistryBucket> getBuckets(final FlowRegistryClientConfigurationContext context, final String branch) throws IOException, FlowRegistryException { + final GitHubRepositoryClient repositoryClient = getRepositoryClient(context); + final Set<FlowRegistryBucket> buckets = repositoryClient.getDirectoryNames("", branch).stream() + .map(this::createFlowRegistryBucket) + .collect(Collectors.toSet()); + + // if the repository has no top-level directories, then return a default bucket entry, this won't exist in the repository until the first time a flow is saved to it + return buckets.isEmpty() ? Set.of(createFlowRegistryBucket(DEFAULT_BUCKET_NAME)) : buckets; + } + + @Override + public FlowRegistryBucket getBucket(final FlowRegistryClientConfigurationContext context, final BucketLocation bucketLocation) throws FlowRegistryException, IOException { + return createFlowRegistryBucket(bucketLocation.getBucketId()); Review Comment: The method `createFlowRegistryBucket` may sound misleading, but it is not creating a bucket in GitHub, it is just creating a bucket object to represent whatever the requested bucket name was. This is because buckets in GitHub aren't really something that needs to exist before hand... if you perform a commit with a path like `Bucket1/MyFlow.json` and `Bucket1` didn't exist, it is created during the commit. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
