Skip to content

Commit

Permalink
Merge branch 'fix-inherited'
Browse files Browse the repository at this point in the history
  • Loading branch information
kovetskiy committed Nov 5, 2019
2 parents f5bc52e + f51d2a1 commit 0857bc1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 93 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.ngs.stash.externalhooks</groupId>
<artifactId>external-hooks</artifactId>
<version>7.3.0</version>
<version>7.5.0</version>
<organization>
<name>reconquest</name>
</organization>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,18 @@ public ExternalAsyncPostReceiveHook(
triggers.add(StandardRepositoryHookTrigger.BRANCH_CREATE);
triggers.add(StandardRepositoryHookTrigger.PULL_REQUEST_MERGE);

this.externalHookScript =
new ExternalHookScript(
authenticationContext,
permissions,
pluginLicenseManager,
clusterService,
storageProperties,
hookScriptService,
pluginSettingsFactory,
securityService,
"external-post-receive-hook",
HookScriptType.POST,
triggers);
this.externalHookScript = new ExternalHookScript(
authenticationContext,
permissions,
pluginLicenseManager,
clusterService,
storageProperties,
hookScriptService,
pluginSettingsFactory,
securityService,
"external-post-receive-hook",
HookScriptType.POST,
triggers);
}

@Override
Expand All @@ -96,10 +95,9 @@ public void onRepositoryHookEnabledEvent(RepositoryHookEnabledEvent event) {
return;
}

GetRepositoryHookSettingsRequest request =
(new GetRepositoryHookSettingsRequest.Builder(
event.getScope(), event.getRepositoryHookKey()))
.build();
GetRepositoryHookSettingsRequest request = (new GetRepositoryHookSettingsRequest.Builder(
event.getScope(), event.getRepositoryHookKey()))
.build();

RepositoryHookSettings hookSettings = this.repositoryHookService.getSettings(request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ public void validate(
if (!settings.getBoolean("safe_path", false)) {
if (!permissions.hasGlobalPermission(Permission.SYS_ADMIN)) {
errors.addFieldError(
"exe",
"You should be a Bitbucket System Administrator to edit this field "
"exe", "You should be a Bitbucket System Administrator to edit this field "
+ "without \"safe mode\" option.");
return;
}
Expand Down Expand Up @@ -228,16 +227,15 @@ public void install(@Nonnull Settings settings, @Nonnull Scope scope) {
this.deleteHookScript(hookScript);
}

HookScriptCreateRequest.Builder test =
new HookScriptCreateRequest.Builder(this.hookComponentId, PLUGIN_KEY, this.hookScriptType)
.content(script);
HookScriptCreateRequest.Builder test = new HookScriptCreateRequest.Builder(
this.hookComponentId, PLUGIN_KEY, this.hookScriptType)
.content(script);
HookScriptCreateRequest hookScriptCreateRequest = test.build();

hookScript =
securityService
.withPermission(
Permission.SYS_ADMIN, "External Hook Plugin: Allow repo admins to set hooks")
.call(() -> hookScriptService.create(hookScriptCreateRequest));
hookScript = securityService
.withPermission(
Permission.SYS_ADMIN, "External Hook Plugin: Allow repo admins to set hooks")
.call(() -> hookScriptService.create(hookScriptCreateRequest));
pluginSettings.put(hookId, String.valueOf(hookScript.getId()));

HookScriptSetConfigurationRequest.Builder configBuilder =
Expand Down Expand Up @@ -321,10 +319,9 @@ private void deleteHookScript(HookScript hookScript) {
securityService
.withPermission(
Permission.SYS_ADMIN, "External Hooks Plugin: Allow repo admins to update hooks")
.call(
() -> {
hookScriptService.delete(hookScript);
return null;
});
.call(() -> {
hookScriptService.delete(hookScript);
return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,35 +53,32 @@ public ExternalMergeCheckHook(
triggers.add(StandardRepositoryHookTrigger.PULL_REQUEST_MERGE);

this.repositoryHookService = repositoryHookService;
this.externalHookScript =
new ExternalHookScript(
authenticationContext,
permissions,
pluginLicenseManager,
clusterService,
storageProperties,
hookScriptService,
pluginSettingsFactory,
securityService,
"external-merge-check-hook",
HookScriptType.PRE,
triggers);
this.externalHookScript = new ExternalHookScript(
authenticationContext,
permissions,
pluginLicenseManager,
clusterService,
storageProperties,
hookScriptService,
pluginSettingsFactory,
securityService,
"external-merge-check-hook",
HookScriptType.PRE,
triggers);
}

@Override
public RepositoryHookResult preUpdate(
PreRepositoryHookContext context, PullRequestMergeHookRequest request) {
if (!this.externalHookScript.isLicenseDefined()) {
return RepositoryHookResult.rejected(
"Unlicensed Add-on.",
"License for External Hooks Add-on is missing.\n"
"Unlicensed Add-on.", "License for External Hooks Add-on is missing.\n"
+ "Visit \"Manage Apps\" page in your Bitbucket instance for more info.");
}

if (!this.externalHookScript.isLicenseValid()) {
return RepositoryHookResult.rejected(
"License is not valid.",
"License for External Hooks Add-on is expired.\n"
"License is not valid.", "License for External Hooks Add-on is expired.\n"
+ "Visit \"Manage Apps\" page in your Bitbucket instance for more info.");
}

Expand Down Expand Up @@ -109,10 +106,9 @@ public void onRepositoryHookEnabledEvent(RepositoryHookEnabledEvent event) {
return;
}

GetRepositoryHookSettingsRequest request =
(new GetRepositoryHookSettingsRequest.Builder(
event.getScope(), event.getRepositoryHookKey()))
.build();
GetRepositoryHookSettingsRequest request = (new GetRepositoryHookSettingsRequest.Builder(
event.getScope(), event.getRepositoryHookKey()))
.build();

RepositoryHookSettings hookSettings = this.repositoryHookService.getSettings(request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,18 @@ public ExternalPreReceiveHook(
triggers.add(StandardRepositoryHookTrigger.BRANCH_CREATE);

this.repositoryHookService = repositoryHookService;
this.externalHookScript =
new ExternalHookScript(
authenticationContext,
permissions,
pluginLicenseManager,
clusterService,
storageProperties,
hookScriptService,
pluginSettingsFactory,
securityService,
"external-pre-receive-hook",
HookScriptType.PRE,
triggers);
this.externalHookScript = new ExternalHookScript(
authenticationContext,
permissions,
pluginLicenseManager,
clusterService,
storageProperties,
hookScriptService,
pluginSettingsFactory,
securityService,
"external-pre-receive-hook",
HookScriptType.PRE,
triggers);
}

@Override
Expand All @@ -96,10 +95,9 @@ public void onRepositoryHookEnabledEvent(RepositoryHookEnabledEvent event) {
return;
}

GetRepositoryHookSettingsRequest request =
(new GetRepositoryHookSettingsRequest.Builder(
event.getScope(), event.getRepositoryHookKey()))
.build();
GetRepositoryHookSettingsRequest request = (new GetRepositoryHookSettingsRequest.Builder(
event.getScope(), event.getRepositoryHookKey()))
.build();

RepositoryHookSettings hookSettings = this.repositoryHookService.getSettings(request);

Expand All @@ -121,15 +119,13 @@ public RepositoryHookResult preUpdate(
@Nonnull PreRepositoryHookContext context, @Nonnull RepositoryHookRequest request) {
if (!this.externalHookScript.isLicenseDefined()) {
return RepositoryHookResult.rejected(
"Unlicensed Add-on.",
"License for External Hooks Add-on is missing.\n"
"Unlicensed Add-on.", "License for External Hooks Add-on is missing.\n"
+ "Visit \"Manage Apps\" page in your Bitbucket instance for more info.");
}

if (!this.externalHookScript.isLicenseValid()) {
return RepositoryHookResult.rejected(
"License is not valid.",
"License for External Hooks Add-on is expired.\n"
"License is not valid.", "License for External Hooks Add-on is expired.\n"
+ "Visit \"Manage Apps\" page in your Bitbucket instance for more info.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,15 @@ public void init() throws SchedulerServiceException {

this.schedulerService.registerJobRunner(runner, this);

this.schedulerService.scheduleJob(
this.jobId,
JobConfig.forJobRunnerKey(runner)
.withSchedule(Schedule.forInterval(this.jobInterval, null)));
this.schedulerService.scheduleJob(this.jobId, JobConfig.forJobRunnerKey(runner)
.withSchedule(Schedule.forInterval(this.jobInterval, null)));
}

@PreDestroy
public void destroy() {
int deleted =
this.securityService
.withPermission(Permission.SYS_ADMIN, "External Hook Plugin: Uninstall repo hooks")
.call(() -> this.hookScriptService.deleteByPluginKey(ExternalHookScript.PLUGIN_KEY));
int deleted = this.securityService
.withPermission(Permission.SYS_ADMIN, "External Hook Plugin: Uninstall repo hooks")
.call(() -> this.hookScriptService.deleteByPluginKey(ExternalHookScript.PLUGIN_KEY));

log.info("Successfully deleted {} HookScripts", deleted);

Expand All @@ -121,14 +118,12 @@ protected boolean isPluginLoaded() {
return true;
}

RepositoryHookSearchRequest.Builder searchBuilder =
new RepositoryHookSearchRequest.Builder(
new ProjectScope(projects.getValues().iterator().next()))
.type(RepositoryHookType.PRE_RECEIVE);
RepositoryHookSearchRequest.Builder searchBuilder = new RepositoryHookSearchRequest.Builder(
new ProjectScope(projects.getValues().iterator().next()))
.type(RepositoryHookType.PRE_RECEIVE);

Page<RepositoryHook> page =
repoHookService.search(
searchBuilder.build(), new PageRequestImpl(0, PageRequest.MAX_PAGE_LIMIT));
Page<RepositoryHook> page = repoHookService.search(
searchBuilder.build(), new PageRequestImpl(0, PageRequest.MAX_PAGE_LIMIT));

boolean found = false;
for (RepositoryHook hook : page.getValues()) {
Expand Down Expand Up @@ -221,9 +216,8 @@ protected void createHookScripts(Scope scope) {
RepositoryHookSearchRequest.Builder searchBuilder =
new RepositoryHookSearchRequest.Builder(scope);

Page<RepositoryHook> page =
repoHookService.search(
searchBuilder.build(), new PageRequestImpl(0, PageRequest.MAX_PAGE_LIMIT));
Page<RepositoryHook> page = repoHookService.search(
searchBuilder.build(), new PageRequestImpl(0, PageRequest.MAX_PAGE_LIMIT));

Integer created = 0;
for (RepositoryHook hook : page.getValues()) {
Expand All @@ -239,6 +233,15 @@ protected void createHookScripts(Scope scope) {
continue;
}

if (hook.getScope().getType() != scope.getType()) {
log.warn(
"external-hooks: hook {} is enabled & configured (inherited: {} {})",
hook.getDetails().getKey(),
hook.getScope().getType(),
hook.getScope().getResourceId().orElse(-1));
continue;
}

EnableRepositoryHookRequest.Builder enableHookBuilder =
new EnableRepositoryHookRequest.Builder(scope, hook.getDetails().getKey());

Expand Down

0 comments on commit 0857bc1

Please sign in to comment.