Adding actions to before commit toolbar/window

Answered

Is it possible to add another entry to the before commit toolbar/window and execute a plugin action? See image below

0
15 comments

You can use "com.intellij.openapi.vcs.checkin.CheckinHandlerFactory".

<extensions defaultExtensionNs="com.intellij">
<checkinHandlerFactory implementation="com.MyPluginCheckinHandlerFactory"/>
</extensions>


See "Update copyright" as an example. https://github.com/JetBrains/intellij-community/blob/master/plugins/copyright/src/com/maddyhome/idea/copyright/actions/UpdateCopyrightCheckinHandlerFactory.java


0

Thanks I will have a look at it!

0

I have managed to add an entry to the before commit panel but I can't make my Action run in the beforeCheckin() method. What am I missing?

public class MyPluginCheckinHandlerFactory extends CheckinHandlerFactory {


@NotNull
@Override
public CheckinHandler createHandler(@NotNull CheckinProjectPanel panel, @NotNull CommitContext commitContext) {
return new CheckinHandler() {
private boolean checkForCylces = false;

@Nullable
@Override
public RefreshableOnComponent getBeforeCheckinConfigurationPanel() {
return new BooleanCommitOption(panel,"Check for Cyclcic Dependencies",false, this::isCheckForCylces, this::setCheckForCylces);
}

public boolean isCheckForCylces() {
return checkForCylces;
}

public void setCheckForCylces(boolean checkForCylces) {
this.checkForCylces = checkForCylces;
}

@Override
public ReturnResult beforeCheckin() {
if(isCheckForCylces()){
ActionManager manager = ActionManager.getInstance();
if(manager != null){

AnAction cyclicDependencyDetectionAction = manager.getAction("GetSourceCode");
if(cyclicDependencyDetectionAction != null){


new AnAction("Run Cyclic Dependency Action") {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {

cyclicDependencyDetectionAction.startInTransaction();

cyclicDependencyDetectionAction.actionPerformed(e);

}
};
}
}
}
return super.beforeCheckin();
}

};
}

}
0

The code above declares new AnAction, but never calls it.

Why do you need AnAction there? Is it possible to extract "GetSourceCode" logic into a static utility method or method in some Service?

0

Thanks!

How can I execute/call AnAction in the example above?

Currently I would like to keep it that way.

0

This depends on "GetSourceCode" implementation (what it does with AnActionEvent).

You can use "ActionUtil#invokeAction" using "CheckinProjectPanel" as target component and ActionPlaces#UNKNOWN, but it might not have a meaningful DataContext.

0

I use the AnActionEvent to get the Project and Editor objects.

Is it possible to cancel the commit operation after clicking the commit button in the CheckinProjectPanel? My idea is to display a message to the user that the commit was canceled if any dependencies were found.

0

Yes, see ReturnResult.CANCEL.

0

I would like to ask the user wheter he would like to commit, cancel and review the dependencies. Something like this:

Is this possible?

0

Yes. For example, like following:

boolean shouldCommit = Messages.showYesNoDialog(myProject,
"Are you sure you want to ignore the warning and commit anyway?",
"Condition Is Not Satisfied",
VcsBundle.message("checkin.commit"),
VcsBundle.message("checkin.cancel"), null) == Messages.YES;
return shouldCommit ? ReturnResult.COMMIT : ReturnResult.CANCEL;
0

Thank you very much!

VcsBundle.message("checkin.commit")
VcsBundle.message("checkin.cancel")

Using the two methods above I get the following errors:

String literal 'checkin.commit' doesn't appear to be valid property key

Same goes for "checkin.cancel"

0

These were added in 20.3 version. You can use "Commit"/"Cancel" string instead.

0

Thanks, thought so!

Is it possible to achieve something like the "Review" functionality as seen in the second image a few posts above?

 

0

Please sign in to leave a comment.