Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Avoiding looking for legacy settings if no such keys
Browse files Browse the repository at this point in the history
 * Also adding Curl examples to README.
 * Documenting REST API.

Logging legacy settings
  • Loading branch information
tomasbjerre committed May 14, 2016
1 parent 53eff48 commit b8600f3
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 81 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### No issue
Doc

[bf28a8fad66abda](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/bf28a8fad66abda) Tomas Bjerre *2016-05-12 17:22:42*

Reusing Podam factory, to use caching

[53eff487fbacd3b](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/53eff487fbacd3b) Tomas Bjerre *2016-05-12 17:18:11*

## 2.20
### GitHub [#116](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/116) Not saving configuration
Linting with JSHint

Expand Down
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ Some rest resources are available. You can figure out the JSON structure by look
* `POST` Save a button.
* `POST {uuid}/press/repository/{repositoryId}/pullrequest/{pullRequestId}` Press the button.

A new notification to trigger on *COMMENTED* can be added like this.
```
curl -u admin:admin 'http://localhost:7990/bitbucket/rest/prnfb-admin/1.0/settings/notifications' -H 'Content-Type: application/json; charset=UTF-8' -H 'Accept: application/json, text/javascript, */*; q=0.01' --data-binary '{"uuid":"","name":"","projectKey":"","repositorySlug":"","filterString":"","filterRegexp":"","triggers":["COMMENTED"],"injectionUrl":"","injectionUrlRegexp":"","user":"","password":"","proxyUser":"","proxyPassword":"","proxyServer":"","proxyPort":"","url":"http://localhost:80/?abc","method":"GET","postContent":"","headers":[{"name":"","value":""}]}'
```

It will respond with something like this.
```
{"headers":[],"method":"GET","name":"Notification","triggerIfCanMerge":"ALWAYS","triggerIgnoreStateList":[],"triggers":["COMMENTED"],"url":"http://localhost:80/?abc","uuid":"b1306a3a-5a87-4145-80b7-660bc986dd25"}
```

It can then be changed to trigger on *RESCOPED_FROM* and *RESCOPED_TO* like this.
```
curl -u admin:admin 'http://localhost:7990/bitbucket/rest/prnfb-admin/1.0/settings/notifications' -H 'Content-Type: application/json; charset=UTF-8' -H 'Accept: application/json, text/javascript, */*; q=0.01' --data-binary '{"uuid":"b1306a3a-5a87-4145-80b7-660bc986dd25","name":"Notification","projectKey":"","repositorySlug":"","filterString":"","filterRegexp":"","triggerIfCanMerge":"ALWAYS","triggers":["RESCOPED_FROM","RESCOPED_TO"],"injectionUrl":"","injectionUrlRegexp":"","user":"","password":"","proxyUser":"","proxyPassword":"","proxyServer":"","proxyPort":"","url":"http://localhost:80/?abc","method":"GET","postContent":"","headers":[{"name":"","value":""}]}' --compressed
```

It will respond with something like this.
```
{"headers":[],"method":"GET","name":"Notification","triggerIfCanMerge":"ALWAYS","triggerIgnoreStateList":[],"triggers":["RESCOPED_FROM","RESCOPED_TO"],"url":"http://localhost:80/?abc","uuid":"b1306a3a-5a87-4145-80b7-660bc986dd25"}
```

You may use Chrome and Developer Tools (press F12) to view rest calls while editing in GUI to find more examples.

### Jenkins
Parameterized Jenkins jobs can be triggered remotely via:
```
Expand All @@ -131,6 +153,8 @@ If you are using a CSRF protection in Jenkins, you can use the **Injection URL**
* In the headers section, set header **.crumb** with value **${INJECTION_URL_VALUE}**.

## Developer instructions
The .travis.yml is setting up Atlas SDK and building the plugin. It may help you setup your environment.

Prerequisites:

* Atlas SDK [(installation instructions)](https://developer.atlassian.com/docs/getting-started/set-up-the-atlassian-plugin-sdk-and-build-a-project).
Expand Down Expand Up @@ -159,6 +183,5 @@ atlas-debug

Make a release [(detailed instructions)](https://developer.atlassian.com/docs/common-coding-tasks/development-cycle/packaging-and-releasing-your-plugin):
```
mvn release:prepare
mvn release:perform
mvn -B release:prepare -DperformRelease=true release:perform
```
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
164 changes: 86 additions & 78 deletions src/main/java/se/bjurr/prnfb/service/SettingsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,88 +250,18 @@ private void doDeleteNotification(UUID uuid) {
private PrnfbSettings doGetPrnfbSettings() {
Object storedSettings = this.pluginSettings.get(STORAGE_KEY);
if (storedSettings == null) {
this.logger.info("No settings found for " + STORAGE_KEY + ", looking for legacy settings.");
try {
se.bjurr.prnfb.settings.legacy.PrnfbSettings oldSettings = SettingsStorage.getPrnfbSettings(this.pluginSettings);

String ks = oldSettings.getKeyStore().orNull();
String ksp = oldSettings.getKeyStorePassword().orNull();
String kst = oldSettings.getKeyStoreType();
USER_LEVEL adminRestr = USER_LEVEL.SYSTEM_ADMIN;
if (oldSettings.isAdminsAllowed()) {
adminRestr = USER_LEVEL.ADMIN;
se.bjurr.prnfb.settings.legacy.PrnfbSettings legacySettings = SettingsStorage.getPrnfbSettings(this.pluginSettings);
if (this.pluginSettings.get(se.bjurr.prnfb.settings.legacy.SettingsStorage.STORAGE_KEY) != null
|| this.pluginSettings.get(se.bjurr.prnfb.settings.legacy.SettingsStorage.STORAGE_KEY_PRNFS) != null) {
this.logger.info("Using legacy settings.");
return settingsFromLegacy(legacySettings);
}
if (oldSettings.isUsersAllowed()) {
adminRestr = USER_LEVEL.EVERYONE;
}

boolean shouldAcceptAnyCertificate = false;

List<PrnfbButton> newButtons = newArrayList();
for (se.bjurr.prnfb.settings.legacy.PrnfbButton oldButton : oldSettings.getButtons()) {
USER_LEVEL userLevel = USER_LEVEL.SYSTEM_ADMIN;
if (oldButton.getVisibility() == BUTTON_VISIBILITY.ADMIN) {
userLevel = USER_LEVEL.ADMIN;
}
if (oldButton.getVisibility() == BUTTON_VISIBILITY.EVERYONE) {
userLevel = USER_LEVEL.EVERYONE;
}
newButtons.add(new PrnfbButton(UUID.randomUUID(), oldButton.getTitle(), userLevel, null, null));
}

List<PrnfbNotification> newNotifications = newArrayList();
for (se.bjurr.prnfb.settings.legacy.PrnfbNotification oldNotification : oldSettings.getNotifications()) {
try {
PrnfbNotificationBuilder builder = prnfbNotificationBuilder()//
.withFilterRegexp(oldNotification.getFilterRegexp().orNull())//
.withFilterString(oldNotification.getFilterString().orNull())//
.withInjectionUrl(oldNotification.getInjectionUrl().orNull())//
.withInjectionUrlRegexp(oldNotification.getInjectionUrlRegexp().orNull())//
.withMethod(oldNotification.getMethod())//
.withName(oldNotification.getName())//
.withPassword(oldNotification.getPassword().orNull())//
.withPostContent(oldNotification.getPostContent().orNull())//
.withProxyPassword(oldNotification.getProxyPassword().orNull())//
.withProxyPort(oldNotification.getProxyPort())//
.withProxyServer(oldNotification.getProxyServer().orNull())//
.withProxyUser(oldNotification.getProxyUser().orNull())//
.withTriggerIfCanMerge(TRIGGER_IF_MERGE.valueOf(oldNotification.getTriggerIfCanMerge().name()))//
.withUrl(oldNotification.getUrl())//
.withUser(oldNotification.getUser().orNull());

for (Header h : oldNotification.getHeaders()) {
builder.withHeader(h.getName(), h.getValue());
}

for (PullRequestState t : oldNotification.getTriggerIgnoreStateList()) {
builder.withTriggerIgnoreState(t);
}

for (PrnfbPullRequestAction t : oldNotification.getTriggers()) {
builder.withTrigger(t);
}

newNotifications.add(builder.build());
} catch (ValidationException e) {
this.logger.error("", e);
}
}

return prnfbSettingsBuilder()//
.setPrnfbSettingsData(//
prnfbSettingsDataBuilder()//
.setAdminRestriction(adminRestr)//
.setKeyStore(ks)//
.setKeyStorePassword(ksp)//
.setKeyStoreType(kst)//
.setShouldAcceptAnyCertificate(shouldAcceptAnyCertificate)//
.build())//
.setButtons(newButtons)//
.setNotifications(newNotifications)//
.build();

} catch (se.bjurr.prnfb.settings.legacy.ValidationException e) {
} catch (Exception e) {
this.logger.error("", e);
}
this.logger.info("Creating new default settings.");
return prnfbSettingsBuilder()//
.setPrnfbSettingsData(//
prnfbSettingsDataBuilder()//
Expand All @@ -358,6 +288,84 @@ public T perform() throws RuntimeException {
});
}

private PrnfbSettings settingsFromLegacy(se.bjurr.prnfb.settings.legacy.PrnfbSettings oldSettings) {
String ks = oldSettings.getKeyStore().orNull();
String ksp = oldSettings.getKeyStorePassword().orNull();
String kst = oldSettings.getKeyStoreType();
USER_LEVEL adminRestr = USER_LEVEL.SYSTEM_ADMIN;
if (oldSettings.isAdminsAllowed()) {
adminRestr = USER_LEVEL.ADMIN;
}
if (oldSettings.isUsersAllowed()) {
adminRestr = USER_LEVEL.EVERYONE;
}

boolean shouldAcceptAnyCertificate = false;

List<PrnfbButton> newButtons = newArrayList();
for (se.bjurr.prnfb.settings.legacy.PrnfbButton oldButton : oldSettings.getButtons()) {
USER_LEVEL userLevel = USER_LEVEL.SYSTEM_ADMIN;
if (oldButton.getVisibility() == BUTTON_VISIBILITY.ADMIN) {
userLevel = USER_LEVEL.ADMIN;
}
if (oldButton.getVisibility() == BUTTON_VISIBILITY.EVERYONE) {
userLevel = USER_LEVEL.EVERYONE;
}
newButtons.add(new PrnfbButton(UUID.randomUUID(), oldButton.getTitle(), userLevel, null, null));
}

List<PrnfbNotification> newNotifications = newArrayList();
for (se.bjurr.prnfb.settings.legacy.PrnfbNotification oldNotification : oldSettings.getNotifications()) {
try {
PrnfbNotificationBuilder builder = prnfbNotificationBuilder()//
.withFilterRegexp(oldNotification.getFilterRegexp().orNull())//
.withFilterString(oldNotification.getFilterString().orNull())//
.withInjectionUrl(oldNotification.getInjectionUrl().orNull())//
.withInjectionUrlRegexp(oldNotification.getInjectionUrlRegexp().orNull())//
.withMethod(oldNotification.getMethod())//
.withName(oldNotification.getName())//
.withPassword(oldNotification.getPassword().orNull())//
.withPostContent(oldNotification.getPostContent().orNull())//
.withProxyPassword(oldNotification.getProxyPassword().orNull())//
.withProxyPort(oldNotification.getProxyPort())//
.withProxyServer(oldNotification.getProxyServer().orNull())//
.withProxyUser(oldNotification.getProxyUser().orNull())//
.withTriggerIfCanMerge(TRIGGER_IF_MERGE.valueOf(oldNotification.getTriggerIfCanMerge().name()))//
.withUrl(oldNotification.getUrl())//
.withUser(oldNotification.getUser().orNull());

for (Header h : oldNotification.getHeaders()) {
builder.withHeader(h.getName(), h.getValue());
}

for (PullRequestState t : oldNotification.getTriggerIgnoreStateList()) {
builder.withTriggerIgnoreState(t);
}

for (PrnfbPullRequestAction t : oldNotification.getTriggers()) {
builder.withTrigger(t);
}

newNotifications.add(builder.build());
} catch (ValidationException e) {
this.logger.error("", e);
}
}

return prnfbSettingsBuilder()//
.setPrnfbSettingsData(//
prnfbSettingsDataBuilder()//
.setAdminRestriction(adminRestr)//
.setKeyStore(ks)//
.setKeyStorePassword(ksp)//
.setKeyStoreType(kst)//
.setShouldAcceptAnyCertificate(shouldAcceptAnyCertificate)//
.build())//
.setButtons(newButtons)//
.setNotifications(newNotifications)//
.build();
}

private Predicate<HasUuid> withUuid(UUID uuid) {
return new Predicate<HasUuid>() {
@Override
Expand Down

0 comments on commit b8600f3

Please sign in to comment.