<commit_before>/*
 * Copyright 2019 Google LLC
 *
 * Licensed 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
 *
 *     https://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 com.google.cloud.artifactregistry.gradle.plugin;

import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.artifactregistry.auth.CredentialProvider;
import com.google.cloud.artifactregistry.auth.DefaultCredentialProvider;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.ProjectConfigurationException;
import org.gradle.api.UncheckedIOException;
import org.gradle.api.artifacts.repositories.ArtifactRepository;
import org.gradle.api.artifacts.repositories.PasswordCredentials;
import org.gradle.api.credentials.Credentials;
import org.gradle.api.initialization.Settings;
import org.gradle.api.initialization.dsl.ScriptHandler;
import org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.provider.Property;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.tasks.Input;
import org.gradle.internal.authentication.DefaultBasicAuthentication;
import org.gradle.plugin.management.PluginManagementSpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ArtifactRegistryGradlePlugin implements Plugin<Object> {

  private static final Logger logger = LoggerFactory.getLogger(ArtifactRegistryGradlePlugin.class);

  static class ArtifactRegistryPasswordCredentials implements PasswordCredentials {
    private String username;
    private String password;

    ArtifactRegistryPasswordCredentials(String username, String password) {
      this.username = username;
      this.password = password;
    }

    @Input
    @Override
    public String getUsername() {
      return username;
    }

    @Input
    @Override
    public String getPassword() {
      return password;
    }

    @Override
    public void setUsername(String username) {
      this.username = username;
    }

    @Override
    public void setPassword(String password) {
      this.password = password;
    }
  }

  private final CredentialProvider credentialProvider = new DefaultCredentialProvider();

  @Override
  public void apply(Object o) {
    ArtifactRegistryPasswordCredentials crd;
    try {
      GoogleCredentials credentials = (GoogleCredentials)credentialProvider.getCredential();
      credentials.refreshIfExpired();
      AccessToken accessToken = credentials.getAccessToken();
      String token = accessToken.getTokenValue();
      crd = new ArtifactRegistryPasswordCredentials("oauth2accesstoken", token);
    } catch (IOException e) {
      throw new UncheckedIOException("Failed to get access token from gcloud or Application Default Credentials", e);
    }

    if (o instanceof Project) {
      applyProject((Project) o, crd);
    } else if (o instanceof Gradle) {<commit_msg>Log when no credentials can be found.<commit_after>/*
 * Copyright 2019 Google LLC
 *
 * Licensed 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
 *
 *     https://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 com.google.cloud.artifactregistry.gradle.plugin;

import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.artifactregistry.auth.CredentialProvider;
import com.google.cloud.artifactregistry.auth.DefaultCredentialProvider;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.ProjectConfigurationException;
import org.gradle.api.UncheckedIOException;
import org.gradle.api.artifacts.repositories.ArtifactRepository;
import org.gradle.api.artifacts.repositories.PasswordCredentials;
import org.gradle.api.credentials.Credentials;
import org.gradle.api.initialization.Settings;
import org.gradle.api.initialization.dsl.ScriptHandler;
import org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.provider.Property;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.tasks.Input;
import org.gradle.internal.authentication.DefaultBasicAuthentication;
import org.gradle.plugin.management.PluginManagementSpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ArtifactRegistryGradlePlugin implements Plugin<Object> {

  private static final Logger logger = LoggerFactory.getLogger(ArtifactRegistryGradlePlugin.class);

  static class ArtifactRegistryPasswordCredentials implements PasswordCredentials {
    private String username;
    private String password;

    ArtifactRegistryPasswordCredentials(String username, String password) {
      this.username = username;
      this.password = password;
    }

    @Input
    @Override
    public String getUsername() {
      return username;
    }

    @Input
    @Override
    public String getPassword() {
      return password;
    }

    @Override
    public void setUsername(String username) {
      this.username = username;
    }

    @Override
    public void setPassword(String password) {
      this.password = password;
    }
  }

  private final CredentialProvider credentialProvider = new DefaultCredentialProvider();

  @Override
  public void apply(Object o) {
    ArtifactRegistryPasswordCredentials crd;
    try {
      GoogleCredentials credentials = (GoogleCredentials)credentialProvider.getCredential();
      credentials.refreshIfExpired();
      AccessToken accessToken = credentials.getAccessToken();
      String token = accessToken.getTokenValue();
      crd = new ArtifactRegistryPasswordCredentials("oauth2accesstoken", token);
    } catch (IOException e) {
      logger.info("Failed to get access token from gcloud or Application Default Credentials", e);
      return;
    }

    if (o instanceof Project) {
      applyProject((Project) o, crd);
    } else if (o instanceof Gradle) {
{'fraction_non_alphanumeric': 0.08183745583038869, 'fraction_numerical': 0.003674911660777385, 'mean_word_length': 4.485271317829457, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1380225', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1842, 'n_words': 509}