How to create a BitBucket hook that reject pushes with bad file name? - bitbucket-server

I'm trying to make a BitBucket hook that would reject a push if it contained a file not matching a naming convention. So far, I'm able to create a PreRepositoryHook implementation that register the following callback.
public class MyPreRepositoryHook implements PreRepositoryHook<RepositoryHookRequest> {
public MyPreRepositoryHook () {
}
#Nonnull
#Override
public RepositoryHookResult preUpdate(#Nonnull PreRepositoryHookContext context,
#Nonnull RepositoryHookRequest request) {
// hook only wants commits added to the repository
context.registerCommitCallback(
new MyPreCommitCallback(),
RepositoryHookCommitFilter.ADDED_TO_REPOSITORY);
// return accepted() here, the callback gets a chance to reject the change when getResult() is called
return RepositoryHookResult.accepted();
}
In MyPreCommitCallback:
#Override
public boolean onCommitAdded(#Nonnull CommitAddedDetails commitDetails) {
Commit commit = commitDetails.getCommit();
SimpleChangeset.Builder builder = new SimpleChangeset.Builder(commit);
SimpleChangeset simpleChangeset = builder.build();
Page<Change> changes = simpleChangeset.getChanges();
}
But I am unable to get the list of files since the call to simpleChangeset.getChanges always return null.
Any help in getting a list of file names would be appreciated. Thank you.

#Component
public class AltresPreRepositoryHook implements PreRepositoryHook<RepositoryHookRequest> {
private final CommitService commitService;
#Autowired
public AltresPreRepositoryHook(#ComponentImport CommitService commitService) {
this.commitService = commitService;
}
private static class AltresPreCommitCallback implements PreRepositoryHookCommitCallback {
private final RepositoryHookRequest request;
private final CommitService commitService;
private RepositoryHookResult result = RepositoryHookResult.accepted();
public AltresPreCommitCallback(RepositoryHookRequest request, CommitService commitService) {
this.request = request;
this.commitService = commitService;
}
#Nonnull
#Override
public RepositoryHookResult getResult() {
return result;
}
#Override
public boolean onCommitAdded(#Nonnull CommitAddedDetails commitDetails) {
Commit commit = commitDetails.getCommit();
ChangesRequest.Builder builder = new ChangesRequest.Builder(commit.getRepository(), commit.getId());
ChangesRequest changesRequest = builder.build();
final ChangedPathsCollector changedPathsCollector = new ChangedPathsCollector();
commitService.streamChanges(changesRequest, changedPathsCollector);
Collection<String> changedPaths = changedPathsCollector.getChangedPaths();

Related

Getting user that initiated push to pre-receve hook

I´m developing a pre-receive hook with the atlassian-plugin-sdk and I need to get the user name of the user who pushed the changes. So what I want to get inside the hook is what I locally can get with 'git config user.name', is that possible?
Use AuthenticationContext to get the current authenticated user. See the example:
#Component
public class BlockInvalidRefPreReceiveRepositoryHook extends TaboolaBaseHook implements PreRepositoryHook<RepositoryHookRequest> {
private final AuthenticationContext authenticationContext;
#Autowired
public BlockInvalidRefPreReceiveRepositoryHook(#ComponentImport AuthenticationContext authenticationContext) {
this.authenticationContext = authenticationContext;
}
#Nonnull
#Override
public RepositoryHookResult preUpdate(#Nonnull PreRepositoryHookContext preRepositoryHookContext,
#Nonnull RepositoryHookRequest repositoryHookRequest) {
if (!isValidTrigger(repositoryHookRequest.getTrigger())){
return RepositoryHookResult.accepted();
}
ApplicationUser currUser = this.authenticationContext.getCurrentUser();
for (RefChange refChange : repositoryHookRequest.getRefChanges()) {
if (refChange.getType() == RefChangeType.ADD && refChange.getRef().getType() == StandardRefType.TAG) {
// Allow only specific user to push tags
if (currUser.getType() == UserType.NORMAL && !currUser.getName().equals("validUserToPushTags")) {
return RepositoryHookResult.rejected("Only user validUserToPushTags is able to push tags",
"Only user validUserToPushTags is able to push tags");
}
}
}
return RepositoryHookResult.accepted();
}
}
I assume you are making a bitbucket plugin. You can get commit information by registering a CommitCallback to the PreRepositoryHook. Below code is untested, but it should fetch the author of a commit.
public class CommitMessageHook implements PreRepositoryHook<RepositoryHookRequest> {
#Nonnull
#Override
public RepositoryHookResult preUpdate(#Nonnull PreRepositoryHookContext context, #Nonnull RepositoryHookRequest request) {
// hook only when a commit is added to the repository
context.registerCommitCallback(
new CommitCallback(),
RepositoryHookCommitFilter.ADDED_TO_REPOSITORY);
return RepositoryHookResult.accepted();
}
private static class CommitCallback implements PreRepositoryHookCommitCallback {
private RepositoryHookResult result = RepositoryHookResult.accepted();
#Nonnull
#Override
public RepositoryHookResult getResult() {
return result;
}
#Override
public boolean onCommitAdded(#Nonnull CommitAddedDetails commitDetails) {
Commit commit = commitDetails.getCommit();
Person author = commit.getAuthor();
String authorName = author.getName();
return true;
}
}
}

Unreachable security context using Feign RequestInterceptor

The goal is to attach some data from security context using RequestInterceptor, but the problem, that the calling SecurityContextHolder.getContext().getAuthentication() always returns null even though it is not null (I am sure 100%).
As I understand that's because the Interceptor is created and is being run in other thread.
How could I solve this problem and get actual data from security context?
My service:
#FeignClient(value = "api", configuration = { FeignConfig.class })
public interface DocumentService {
#RequestMapping(value = "/list", method = RequestMethod.GET)
DocumentListOperation list();
}
My FeignConfig class:
#Bean
public RequestInterceptor requestInterceptor() {
return new HeaderInterceptor(userService);
}
public class HeaderInterceptor implements RequestInterceptor {
private UserService userService;
public HeaderInterceptor(UserService userService) {
this.userService = userService;
}
#Override
public void apply(RequestTemplate requestTemplate) {
Authentication a = SecurityContextHolder.getContext().getAuthentication()
requestTemplate.header("authentication", a.toString());
}
}
I managed to figure it out, thanks to the article I found here
Firstly you need to initiliaze HystrixRequestContext HystrixRequestContext.initializeContext();.
You have to create your own Context in which you will store information you need to pass to Hystrix child threads.
Here is example:
public class UserHystrixRequestContext {
private static final HystrixRequestVariableDefault<User> userContextVariable = new HystrixRequestVariableDefault<>();
private UserHystrixRequestContext() {}
public static HystrixRequestVariableDefault<User> getInstance() {
return userContextVariable;
}
}
You have to register new concurrency strategy that would wrap Callable interface
#Component
public class CustomHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
public CustomHystrixConcurrencyStrategy() {
HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
}
#Override
public <T> Callable<T> wrapCallable(Callable<T> callable) {
return new HystrixContextWrapper<T>(callable);
}
public static class HystrixContextWrapper<V> implements Callable<V> {
private HystrixRequestContext hystrixRequestContext;
private Callable<V> delegate;
public HystrixContextWrapper(Callable<V> delegate) {
this.hystrixRequestContext = HystrixRequestContext.getContextForCurrentThread();
this.delegate = delegate;
}
#Override
public V call() throws Exception {
HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread();
try {
HystrixRequestContext.setContextOnCurrentThread(this.hystrixRequestContext);
return this.delegate.call();
} finally {
HystrixRequestContext.setContextOnCurrentThread(existingState);
}
}
}
}
So before calling Callable object we set new thread's Context to parent's context.
After that is done you should be able to access your new defined context inside Hystrix child threads
User = UserHystrixRequestContext.getInstance().get();
Hope that will help someone.

Why does my sub-dependency not get set in Dagger?

I am having a hard time figuring out how to inject CachedRithms into my RithmioManager and CachedKamms into my KamilManager?
I have the following files:
AppScopeModule:
#Module
(
library = true,
complete = false,
injects = {
KamilApplication.class,
KamilManager.class
}
)
public class AppScopeModule {
/* package */ static Context sApplicationContext = null;
private final Context mApplicationContext;
AppScopeModule(Context applicationContext) {
KamilManager.initInstance(applicationContext);
mApplicationContext = applicationContext;
}
#Provides
#Singleton
KamilManager provideKamilManager() {
return KamilManager.getInstance();
}
}
KamilApplication:
public class KamilApplication extends Application implements Injector {
private ObjectGraph mObjectGraph;
#Inject
KamilManager KamilManager;
#Override
public void onCreate() {
super.onCreate();
AppScopeModule sharedAppModule = new AppScopeModule(this);
// bootstrap. So that it allows no-arg constructor in AppScopeModule
sharedAppModule.sApplicationContext = this.getApplicationContext();
List<Object> modules = new ArrayList<Object>();
modules.add(sharedAppModule);
modules.add(new AuthModule());
modules.addAll(getAppModules());
mObjectGraph = ObjectGraph.create(modules.toArray());
mObjectGraph.inject(this);
}
}
KamilManager
public class KamilManager {
#Inject
CachedKamms mCachedKamms;
private static KamilManager instance;
private boolean mWearIsConnectedToMobile;
private KamilManager() {
Log.d(TAG, "KamilManager private constructor");
}
public static void initInstance(Context appContext) {
if (instance == null) {
instance = new KamilManager();
.....doing more things here...
}
}
public static KamilManager getInstance() {
return instance;
}
}
But mCAchedKamms is always blank when I initialize the app. Any idea what I'm doing wrong?
You need to call ObjectGraph.inject(this) somewhere in KamilManager.
I suggest you to add this code to your KamilApplication class:
public ObjectGraph getObjectGraph() {
return mObjectGraph;
}
After that you need to somehow get instance of KamilApplication(pass it via constructor maybe?) in KamilManager and call:
kamilApplication.getObjectGraph.inject(this);
after this call every field in class KamilManager annotated with #Inject should be injected.
OR
Just annotate constructor of CachedKamms with #Inject
Extra:
Avoid of using library = true and complete = false unless you know what are you doing. With this settings you disable some validations at compile time.

Why canReveal() of GateKeeper was called before the EventHandler was called (GWT)?

I got a Header presenter which is the nested presenter. The Customer presenter is the child of Header presenter (ie the Customer presenter was put into a slot of Header presenter).
So I want to use MyGateKeeper to manage login page. The HeaderPresenter that will fire PassUserInfoEvent.
public class MyGateKeeper implements Gatekeeper{
private String loginedUserID="";
private final EventBus eventBus;
#Inject
public MyGateKeeper (final EventBus eventBus){
this.eventBus = eventBus;
this.eventBus.addHandler(PassUserInfoEvent.getType(), new PassUserInfoHandler(){
#Override
public void onPassUserInfo(PassUserInfoEvent event) {
// TODO Auto-generated method stub
String userID=event.getUserID();
loginedUserID=userID;
}
});
}
#Override
public boolean canReveal(){
System.out.println(loginedUserID+"Test");
if(!loginedUserID.equals("")){
System.out.println(loginedUserMeaningID+"cxcxc");
return true;
}
else{
return false;
}
}
}
In the CustomerPresenter:
#ProxyCodeSplit
#NameToken(NameTokens.cust)
#UseGatekeeper(MyGateKeeper.class)
public interface MyProxy extends ProxyPlace<CustomerPresenter> {
}
However after run, it does not show the Gui even I loggined. I tested & found that canReveal() in MyGateKeeper was called before PassUserInfoHandler() was called, so canReveal never return true;
How to fix this problem?
The usual pattern to accomplish this is to bind a CurrentUser class in Singleton:
bind(CurrentUser.class).in(Singleton.class);
and inject it into your GateKeeper. Inside your GateKeeper's canReveal method, you'll check that currentUser.isLoggedIn() :
private final CurrentUser currentUser;
#Inject
MyGateKeeper(CurrentUser currentUser) {
this.currentUser = currentUser;
}
#Override
public boolean canReveal() {
return currentUser.isLoggedIn();
}
You should initialize the CurrentUser.isLoggedIn field inside your Bootstrapper (see https://github.com/ArcBees/GWTP/wiki/Bootstrapping-or-Application-Initialization) by calling your server. Here's an example using GWTP's RestDispatch:
public class BootstrapperImpl implements Bootstrapper {
private final String unauthorizedPlace;
private final CurrentUser currentUser;
private final PlaceManager placeManager;
private final RestDispatch restDispatch;
private final UserResource userResource;
#Inject
BootstrapperImpl(
#UnauthorizedPlace String unauthorizedPlace,
CurrentUser currentUser,
PlaceManager placeManager,
RestDispatch restDispatch,
UserResource userResource) {
this.unauthorizedPlace = unauthorizedPlace;
this.currentUser = currentUser;
this.placeManager = placeManager;
this.restDispatch = restDispatch;
this.userResource = userResource;
}
#Override
public void onBootstrap() {
checkIfUserIsLoggedIn();
}
private void checkIfUserIsLoggedIn() {
restDispatch.execute(userResource.isCurrentUserLoggedIn(), new AbstractAsyncCallback<Boolean>() {
#Override
public void onSuccess(Boolean isCurrentUserLoggedIn) {
navigate(isCurrentUserLoggedIn);
}
});
}
private void navigate(Boolean isCurrentUserLoggedIn) {
currentUser.setLoggedIn(isCurrentUserLoggedIn);
if (isCurrentUserLoggedIn) {
placeManager.revealCurrentPlace();
} else {
placeManager.revealPlace(new PlaceRequest.Builder().nameToken(unauthorizedPlace).build());
}
}
}

How to respond to URLs with GWT's built-in MVP-framework?

I'm building a very simple calendar app to get familiar with the MVP-framework introduced with the 2.1 version of GWT.
What I want to achieve is being able to switch between a list of scheduled appointments and a list of the avialable time.
I have created the a CalendarPlace, CalendarActivity, CalendarView and CalendarViewImpl.
I know that to navigate to a different place i would call PlaceController.goTo(Place), so in my calendar app I would call:
clientFactory.getPlaceController.goTo(new CalendarPlace("freeTime");
The URL would be index.html#CalendarPlace:freeTime for the list of free time or
clientFactory.getPlaceController.goTo(new CalendarPlace("appointments");
for the list of scheduled appointments. The URL would be index.html#CalendarPlace:appointments
But the question is where do I respond to the different tokens? I guess the CalendarPlace would be the right place, but how would I do that?
Here is my source code(I took most of the boilerplate from the tutorial here:
CalendarPlace:
public class CalendarPlace extends Place {
private String calendarName;
public CalendarPlace(String token) {
this.calendarName = token;
}
public String getCalendarName() {
return calendarName;
}
public static class Tokenizer implements PlaceTokenizer<CalendarPlace> {
#Override
public CalendarPlace getPlace(String token) {
return new CalendarPlace(token);
}
#Override
public String getToken(CalendarPlace place) {
return place.getCalendarName();
}
}
}
CalendarActivity:
public class CalendarActivity extends AbstractActivity
implements
CalendarView.Presenter {
private ClientFactory clientFactory;
private String name;
public CalendarActivity(CalendarPlace place, ClientFactory clientFactory) {
this.name = place.getCalendarName();
this.clientFactory = clientFactory;
}
#Override
public void goTo(Place place) {
clientFactory.getPlaceController().goTo(place);
}
#Override
public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
CalendarView calendarView = clientFactory.getCalendarView();
calendarView.setName(name);
calendarView.setPresenter(this);
containerWidget.setWidget(calendarView.asWidget());
}
}
CalendarViewImpl:
public class CalendarViewImpl extends Composite implements CalendarView {
private VerticalPanel content;
private String name;
private Presenter presenter;
private OptionBox optionBox;
public CalendarViewImpl() {
//optionBox is used for navigation
//optionBox is where I call PlaceController.goTo() from
optionBox=new OptionBox();
RootPanel.get("bluebar").add(optionBox);
content=new VerticalPanel();
this.initWidget(content);
}
#Override
public void setPresenter(Presenter listener) {
this.presenter=listener;
}
#Override
public void setName(String calendarName) {
this.name = calendarName;
}
public void displayFreeTime() {
//called from somewhere to display the free time
}
public void getAppointments() {
//called from somewhere to display the appointments
}
}
In your CalendarActivity constructor you have access to the place, and therefore the token. Tuck it aside, and then in your start() method you can use it. Activities are meant to be lightweight objects, created for each new navigation.