Skip to content

Commit

Permalink
Merge pull request #17 from thomasrepnik/feature/branch-name-update
Browse files Browse the repository at this point in the history
Feature/branch name update
close #16
  • Loading branch information
thomasrepnik authored Sep 28, 2022
2 parents 0158927 + c105b17 commit f620b64
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 31 deletions.
20 changes: 11 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.6.5'
id 'org.jetbrains.intellij' version '1.9.0'
}

group 'ch.repnik'
version '1.1.2'
version '1.2.0'

repositories {
mavenCentral()
}

dependencies {
testImplementation 'org.hamcrest:hamcrest:2.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.9.0'
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

test {
Expand All @@ -30,16 +31,17 @@ test {

// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2021.2'
version.set('2022.2.2')
plugins = ['git4idea']
}

runPluginVerifier{
ideVersions = ["IC-2021.2"]
ideVersions = ["IC-2022.2.2"]
}

patchPluginXml {
untilBuild null
sinceBuild.set("222")
untilBuild.set(null)
}


Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
38 changes: 22 additions & 16 deletions src/main/java/ch/repnik/intellij/CommitPrefixCheckinHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.repnik.intellij;

import ch.repnik.intellij.settings.PluginSettings;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.BranchChangeListener;
Expand All @@ -15,45 +16,45 @@
import git4idea.GitLocalBranch;
import git4idea.branch.GitBranchUtil;
import git4idea.repo.GitRepository;
import git4idea.status.GitRefreshListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommitPrefixCheckinHandler extends CheckinHandler implements BranchChangeListener {
public class CommitPrefixCheckinHandler extends CheckinHandler implements BranchChangeListener, GitRefreshListener {

private final Logger log = Logger.getInstance(getClass());
private CheckinProjectPanel panel;
private final CheckinProjectPanel panel;
private static final Pattern branchNamePattern = Pattern.compile("(?<=\\/)*([A-Z0-9]+-[0-9]+)");
private static final Pattern prefixPattern = Pattern.compile("[A-Z0-9]+-[0-9]+");


private String newCommitMessage;


public CommitPrefixCheckinHandler(CheckinProjectPanel panel) {
this.panel = panel;

MessageBusConnection connect = panel.getProject().getMessageBus().connect();
connect.subscribe(BranchChangeListener.VCS_BRANCH_CHANGED, this);
connect.subscribe(GitRefreshListener.TOPIC, this);

//Sets the new message on the new commit UI
updateCommitMessage();
}

private void updateCommitMessage(){
PsiDocumentManager psiInstance = PsiDocumentManager.getInstance(this.panel.getProject());
if (psiInstance instanceof PsiDocumentManagerImpl){
if (!((PsiDocumentManagerImpl) psiInstance).isCommitInProgress()){
panel.setCommitMessage(getNewCommitMessage());
ApplicationManager.getApplication().invokeLater(() -> {
PsiDocumentManager psiInstance = PsiDocumentManager.getInstance(this.panel.getProject());
if (psiInstance instanceof PsiDocumentManagerImpl){
if (!((PsiDocumentManagerImpl) psiInstance).isCommitInProgress()){
panel.setCommitMessage(getNewCommitMessage());
}else{
log.info("PsiDocumentManager reported commit in progress. Skipping Git Auto Prefix");
}
}else{
log.info("PsiDocumentManager reported commit in progress. Skipping Git Auto Prefix");
log.info("PsiDocumentManager is not an instance of PsiDocumentManagerImpl. Skipping Git Auto Prefix");
}
}else{
log.info("PsiDocumentManager is not an instance of PsiDocumentManagerImpl. Skipping Git Auto Prefix");
}
});
}

@Nullable
Expand All @@ -71,9 +72,8 @@ private String getNewCommitMessage(){
Optional<String> jiraTicketName = getJiraTicketName(branchName);

if (jiraTicketName.isPresent()){
String newMessage = updatePrefix(jiraTicketName.get(), panel.getCommitMessage(), getWrapLeft(), getWrapRight());
//Sets the value for the new Panel UI
return newMessage;
return updatePrefix(jiraTicketName.get(), panel.getCommitMessage(), getWrapLeft(), getWrapRight());
}

return panel.getCommitMessage();
Expand Down Expand Up @@ -177,4 +177,10 @@ public void branchWillChange(@NotNull String branchName) {
public void branchHasChanged(@NotNull String branchName) {
updateCommitMessage();
}

//Detects repository updates made in the terminal
@Override
public void repositoryUpdated(@NotNull GitRepository repository) {
updateCommitMessage();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.repnik.intellij;

import com.intellij.openapi.vcs.CheckinProjectPanel;
import com.intellij.openapi.vcs.changes.CommitContext;
import com.intellij.openapi.vcs.checkin.CheckinHandler;
import com.intellij.openapi.vcs.checkin.VcsCheckinHandlerFactory;
import git4idea.GitVcs;
Expand All @@ -14,7 +15,7 @@ public GitBaseCheckinHandlerFactory(){

@NotNull
@Override
protected CheckinHandler createVcsHandler(CheckinProjectPanel panel) {
protected CheckinHandler createVcsHandler(@NotNull CheckinProjectPanel panel, @NotNull CommitContext commitContext) {
return new CommitPrefixCheckinHandler(panel);
}
}
20 changes: 17 additions & 3 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<idea-plugin>
<id>commit-prefix-plugin</id>
<name>Git Auto Prefix</name>
<version>1.1.2</version>
<version>1.2.0</version>
<vendor url="https://github.com/thomasrepnik/idea-commit-prefix-plugin">Thomas Repnik</vendor>

<change-notes>Fixes an occasional exception on branch switch</change-notes>
<change-notes><![CDATA[
<p>
<ul>
<li>The Plugin now recognizes branch switches made on the CLI/Terminal</li>
<li>Update to Java 17</li>
</ul>
</p>
]]></change-notes>

<description><![CDATA[
<p>
Expand All @@ -13,6 +20,8 @@
<li>Choose your own delimiter between the issue key and commit message</li>
<li>Wrap the issue key if necessary</li>
</ul>
<br/>
<strong>Important:</strong>This plugin only works on branches created by Jira (or manually, with exactly the same naming as jira would use)
</p>
<table cellspacing="5">
Expand All @@ -21,6 +30,11 @@
<th>Commit prefix (with delimiter)</th>
<th>Commit prefix (wrapped)</th>
</tr>
<tr>
<td>main</td>
<td><em>no action</em></td>
<td><em>no action</em></td>
</tr>
<tr>
<td>master</td>
<td><em>no action</em></td>
Expand Down Expand Up @@ -66,7 +80,7 @@

<extensions defaultExtensionNs="com.intellij">
<vcsCheckinHandlerFactory implementation="ch.repnik.intellij.GitBaseCheckinHandlerFactory" />
<applicationConfigurable groupId="tools" instance="ch.repnik.intellij.settings.PluginSettingsConfigurable"></applicationConfigurable>
<applicationConfigurable groupId="tools" instance="ch.repnik.intellij.settings.PluginSettingsConfigurable"/>
</extensions>

<actions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ public void getJiraTicketName_withoutBranchType_retunsJiraTicket() {
assertThat(result.get(), is("ABC-1234"));
}

@Test
public void getJiraTicketName_reproduce() {
Optional<String> result = CommitPrefixCheckinHandler.getJiraTicketName("feature/DATA-4214-ab-CEP3.0-Transition-polling");

assertThat(result.isPresent(), is(true));
assertThat(result.get(), is("DATA-4214"));
}

@Test
public void getJiraTicketName_featureBranchType_retunsJiraTicket() {
Optional<String> result = CommitPrefixCheckinHandler.getJiraTicketName("feature/ABC-1234-app-not-working");
Expand Down Expand Up @@ -188,4 +196,4 @@ public void getJiraTicketName_emptySuffix_retunsJiraTicket() {
assertThat(result.get(), is("ABC-1234"));
}

}
}

0 comments on commit f620b64

Please sign in to comment.