Skip to content

Commit 06b35be

Browse files
committed
Add workspace tagging for IntelliJ toolkit
1 parent 9dc73d3 commit 06b35be

File tree

8 files changed

+245
-2
lines changed

8 files changed

+245
-2
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/azure-sdk-reference-book/build.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ dependencies {
2323
annotationProcessor 'org.projectlombok:lombok:1.18.8'
2424

2525
compileOnly 'org.jetbrains:annotations:20.1.0'
26+
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.11.2', {
27+
exclude group: "com.fasterxml.jackson", module: "jackson-bom"
28+
}
2629
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.11.2', {
2730
exclude group: "com.fasterxml.jackson", module: "jackson-bom"
2831
}
29-
32+
compile 'com.microsoft.azuretools:azuretools-core:3.51.0-SNAPSHOT', {
33+
exclude group: "com.microsoft.azure", module: "azure-client-authentication"
34+
exclude group: "com.microsoft.azure", module: "azure-client-runtime"
35+
exclude group: "javax.xml.bind", module: "jaxb-api"
36+
}
3037
compile project(':azure-intellij-plugin-lib')
3138
compile "com.microsoft.azure:azure-toolkit-common-lib:0.5.0-SNAPSHOT"
3239

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*/
5+
6+
package com.microsoft.azure.toolkit.intellij.azuresdk.dependencesurvey.activity;
7+
8+
import com.intellij.openapi.application.ApplicationManager;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.openapi.roots.OrderEnumerator;
11+
import com.intellij.openapi.startup.StartupActivity;
12+
import com.microsoft.azure.toolkit.intellij.azuresdk.service.WorkspaceTaggingService;
13+
import com.microsoft.azuretools.telemetry.TelemetryConstants;
14+
import com.microsoft.azuretools.telemetrywrapper.EventType;
15+
import com.microsoft.azuretools.telemetrywrapper.EventUtil;
16+
import org.apache.commons.lang.StringUtils;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
import java.util.Map;
20+
import java.util.Set;
21+
import java.util.regex.Matcher;
22+
import java.util.regex.Pattern;
23+
import java.util.stream.Collectors;
24+
25+
public class WorkspaceTaggingActivity implements StartupActivity.DumbAware {
26+
private static final Pattern PATTERN = Pattern.compile("(Gradle|Maven): (.*):(.*):(.*)");
27+
28+
@Override
29+
public void runActivity(@NotNull final Project project) {
30+
ApplicationManager.getApplication().runReadAction(() -> trackProjectDependencies(project));
31+
32+
}
33+
34+
private void trackProjectDependencies(@NotNull final Project project) {
35+
final Set<String> tagSet = new java.util.HashSet<>();
36+
OrderEnumerator.orderEntries(project).forEachLibrary(library -> {
37+
final Matcher matcher = PATTERN.matcher(StringUtils.isEmpty(library.getName()) ? StringUtils.EMPTY : library.getName());
38+
if (matcher.matches()) {
39+
final String tag = WorkspaceTaggingService.getWorkspaceTag(matcher.group(2), matcher.group(3));
40+
if (StringUtils.isNotEmpty(tag)) {
41+
tagSet.add(tag);
42+
}
43+
}
44+
return true;
45+
});
46+
final Map<String, String> properties = tagSet.stream().collect(Collectors.toMap(tag -> tag, tag -> "true"));
47+
EventUtil.logEvent(EventType.info, TelemetryConstants.SYSTEM, TelemetryConstants.WORKSPACE_TAGGING, properties);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*/
5+
6+
package com.microsoft.azure.toolkit.intellij.azuresdk.model;
7+
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
import lombok.Getter;
10+
import lombok.NoArgsConstructor;
11+
import lombok.ToString;
12+
13+
@ToString
14+
@Getter
15+
@NoArgsConstructor
16+
public class AzureJavaSdkEntity {
17+
@JsonProperty("Package")
18+
private String packageName;
19+
@JsonProperty("GroupId")
20+
private String groupId;
21+
@JsonProperty("VersionGA")
22+
private String versionGA;
23+
@JsonProperty("VersionPreview")
24+
private String versionPreview;
25+
@JsonProperty("DisplayName")
26+
private String displayName;
27+
@JsonProperty("ServiceName")
28+
private String serviceName;
29+
@JsonProperty("RepoPath")
30+
private String repoPath;
31+
@JsonProperty("MSDocs")
32+
private String msDocs;
33+
@JsonProperty("GHDocs")
34+
private String ghDocs;
35+
@JsonProperty("Type")
36+
private String type;
37+
@JsonProperty("New")
38+
private Boolean isNew; // New
39+
@JsonProperty("PlannedVersions")
40+
private String plannedVersions;
41+
@JsonProperty(value = "Hide")
42+
private Boolean isHide;
43+
@JsonProperty("Notes")
44+
private String notes;
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*/
5+
6+
package com.microsoft.azure.toolkit.intellij.azuresdk.model;
7+
8+
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
10+
import lombok.ToString;
11+
12+
@ToString
13+
@Getter
14+
@NoArgsConstructor
15+
public class WorkspaceTagEntity {
16+
private String groupId;
17+
private String artifactId;
18+
private String tag;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*/
5+
6+
package com.microsoft.azure.toolkit.intellij.azuresdk.service;
7+
8+
import com.fasterxml.jackson.core.JsonParser;
9+
import com.fasterxml.jackson.databind.DeserializationFeature;
10+
import com.fasterxml.jackson.databind.MappingIterator;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.fasterxml.jackson.databind.json.JsonMapper;
13+
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
14+
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
15+
import com.microsoft.azure.toolkit.intellij.azuresdk.model.AzureJavaSdkEntity;
16+
import com.microsoft.azure.toolkit.intellij.azuresdk.model.WorkspaceTagEntity;
17+
import com.microsoft.azure.toolkit.lib.common.cache.Cacheable;
18+
import com.microsoft.azure.toolkit.lib.common.cache.Preload;
19+
import org.apache.commons.lang3.ObjectUtils;
20+
import org.apache.commons.lang3.StringUtils;
21+
import org.jetbrains.annotations.Nullable;
22+
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.net.URL;
26+
import java.util.List;
27+
28+
public class WorkspaceTaggingService {
29+
private static final String SDK_METADATA_URL = "https://raw.githubusercontent.com/Azure/azure-sdk/master/_data/releases/latest/java-packages.csv";
30+
31+
private static final ObjectMapper CSV_MAPPER = new CsvMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
32+
private static final ObjectMapper JSON_MAPPER = new JsonMapper().configure(JsonParser.Feature.ALLOW_COMMENTS, true);
33+
private static final String WORKSPACE_TAG_JSON = "/workspaceTag.json";
34+
35+
@Nullable
36+
public static String getWorkspaceTag(String groupId, final String artifactId) {
37+
if (StringUtils.isAnyEmpty(groupId, artifactId)) {
38+
return null;
39+
}
40+
return ObjectUtils.firstNonNull(getAzureArtifactTag(groupId, artifactId), getDependencyTag(groupId, artifactId));
41+
}
42+
43+
private static String getAzureArtifactTag(final String groupId, final String artifactId) {
44+
try {
45+
return getAzureSDKArtifacts()
46+
.stream()
47+
.filter(entity -> StringUtils.isNotEmpty(entity.getType())
48+
&& StringUtils.equalsIgnoreCase(entity.getGroupId(), groupId)
49+
&& StringUtils.equalsIgnoreCase(entity.getPackageName(), artifactId))
50+
.map(AzureJavaSdkEntity::getType)
51+
.findFirst().orElse(null);
52+
} catch (IOException e) {
53+
// swallow exception for workspace tagging
54+
return null;
55+
}
56+
}
57+
58+
private static String getDependencyTag(final String groupId, final String artifactId) {
59+
try {
60+
return getWorkspaceTagEntities()
61+
.stream()
62+
.filter(entity -> (StringUtils.equalsIgnoreCase(entity.getGroupId(), groupId) || entity.getGroupId().matches(groupId))
63+
&& (StringUtils.equalsIgnoreCase(entity.getArtifactId(), artifactId) || entity.getArtifactId().matches(artifactId)))
64+
.map(WorkspaceTagEntity::getTag)
65+
.findFirst().orElse(null);
66+
} catch (IOException e) {
67+
// swallow exception for workspace tagging
68+
return null;
69+
}
70+
}
71+
72+
@Preload
73+
@Cacheable(value = "workspace-tag", condition = "!(force&&force[0])")
74+
public static List<WorkspaceTagEntity> getWorkspaceTagEntities(boolean... force) throws IOException {
75+
try (final InputStream stream = WorkspaceTaggingService.class.getResourceAsStream(WORKSPACE_TAG_JSON)) {
76+
final MappingIterator<WorkspaceTagEntity> iterator = JSON_MAPPER.readerFor(WorkspaceTagEntity.class).readValues(stream);
77+
return iterator.readAll();
78+
}
79+
}
80+
81+
@Preload
82+
@Cacheable(value = "workspace-tag-azure", condition = "!(force&&force[0])")
83+
public static List<AzureJavaSdkEntity> getAzureSDKArtifacts(boolean... force) throws IOException {
84+
final URL destination = new URL(SDK_METADATA_URL);
85+
final CsvSchema schema = CsvSchema.emptySchema().withHeader();
86+
final MappingIterator<AzureJavaSdkEntity> mappingIterator = CSV_MAPPER.readerFor(AzureJavaSdkEntity.class).with(schema).readValues(destination);
87+
return mappingIterator.readAll();
88+
}
89+
}

PluginsAndFeatures/azure-toolkit-for-intellij/azure-sdk-reference-book/src/main/resources/META-INF/azure-sdk-reference-book.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<idea-plugin>
22
<extensionPoints>
33
</extensionPoints>
4+
<extensions defaultExtensionNs="com.intellij">
5+
<postStartupActivity implementation="com.microsoft.azure.toolkit.intellij.azuresdk.dependencesurvey.activity.WorkspaceTaggingActivity"/>
6+
</extensions>
47
<actions>
58
<action id="AzureToolkit.OpenSdkReferenceBook"
69
class="com.microsoft.azure.toolkit.intellij.azuresdk.referencebook.OpenReferenceBookAction"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[
2+
// java ee
3+
{ "groupId": "javax", "artifactId": "javaee-api", "tag": "javaee" },
4+
{ "groupId": "javax.xml.bind", "artifactId": "jaxb-api", "tag": "javaee" },
5+
// jdbc
6+
{ "groupId": "mysql", "artifactId": "mysql-connector-java", "tag": "jdbc" },
7+
{ "groupId": "com.microsoft.sqlserver", "artifactId": "mssql-jdbc", "tag": "jdbc" },
8+
{ "groupId": "com.oracle.database.jdbc", "artifactId": "ojdbc10", "tag": "jdbc" },
9+
// jpa
10+
{ "groupId": "org.hibernate", "artifactId": "hibernate-core", "tag": "jpa" },
11+
{ "groupId": "org.eclipse.persistence", "artifactId": "eclipselink", "tag": "jpa" },
12+
// lombok
13+
{ "groupId": "org.projectlombok", "artifactId": "lombok", "tag": "lombok" },
14+
// mockito
15+
{ "groupId": "org.mockito", "artifactId": "mockito-core", "tag": "mockito" },
16+
{ "groupId": "org.powermock", "artifactId": "powermock-core", "tag": "mockito" },
17+
// redis
18+
{ "groupId": "org.springframework.data", "artifactId": "spring-data-redis", "tag": "redis" },
19+
{ "groupId": "redis.clients", "artifactId": "jedis", "tag": "redis" },
20+
{ "groupId": "org.redisson", "artifactId": "redisson", "tag": "redis" },
21+
{ "groupId": "io.lettuce", "artifactId": "lettuce-core", "tag": "redis" },
22+
// spring boot
23+
{ "groupId": "org.springframework.boot", "artifactId": ".*", "tag": "springboot" },
24+
// sql
25+
{ "groupId": "org.jooq", "artifactId": "jooq", "tag": "sql" },
26+
{ "groupId": "org.mybatis", "artifactId": "mybatis", "tag": "sql" },
27+
// unit test
28+
{ "groupId": "org.junit.jupiter", "artifactId": "junit-jupiter-api", "tag": "unitTest" },
29+
{ "groupId": "junit", "artifactId": "junit", "tag": "unitTest" },
30+
{ "groupId": "org.testng", "artifactId": "testng", "tag": "unitTest" }
31+
]

Utils/azuretools-core/src/com/microsoft/azuretools/telemetry/TelemetryConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public class TelemetryConstants {
181181
public static final String UPDATE_A_CLUSTER = "update-a-cluster";
182182
public static final String SHOW_WHATS_NEW = "show-whats-new";
183183
public static final String UNHANDLED_EXCEPTION = "unhandled-exception";
184-
184+
public static final String WORKSPACE_TAGGING = "workspace-tagging";
185185
// property name
186186
public static final String WEBAPP_DEPLOY_TO_SLOT = "webappDeployToSlot";
187187
public static final String RUNTIME = "runtime";

0 commit comments

Comments
 (0)