Skip to content

Instantly share code, notes, and snippets.

View artemptushkin's full-sized avatar
:octocat:
Reducing complexity

Artem Ptushkin artemptushkin

:octocat:
Reducing complexity
View GitHub Profile
@artemptushkin
artemptushkin / .gitlab-ci.yml
Last active July 15, 2024 07:54
Python script and gitlab job to remove stale branches from a configured projects, see https://dev.to/art_ptushkin/gitlab-python-based-job-to-remove-stale-branches-1i6i
remove-stale-branches:
needs: []
image: python:slim
variables:
THRESHOLD_PERIOD_DAYS: 30
STALE_PROJECT_NAMESPACES: "$CI_PROJECT_PATH"
parallel:
matrix:
- STALE_PROJECT_NAMESPACES: "foo/baz"
STALE_EXCLUSION_PATTERNS: "example/.*"
@artemptushkin
artemptushkin / current-spring-dependencies.sh
Last active August 17, 2023 12:01
Spring Boot Dependencies troubleshooting cheatsheet
#!/usr/bin/env bash
read version
url="https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/$version/spring-boot-dependencies-$version.pom"
properties_lines=$(curl -s "$url" | sed -n '/<properties>/,/<\/properties>/p' | sed -e 's/<properties>//;s/<\/properties>//')
echo "$properties_lines"
@artemptushkin
artemptushkin / docs.md
Last active August 19, 2024 10:34
gitlab-cancel-redundant-pipeline

Problem

There is a known problem that in Gitlab it's not possible to avoid pipeline duplication in some scenarios.

Context

Let's assume you combine merge and branch pipelines. You can follow the Gitlab guideline or here is an improved one:

workflow:
@artemptushkin
artemptushkin / KotlinExampleConfigurationPropertiesConfig.kt
Last active July 14, 2022 15:11
kotlin-spring-configuration-properties-best-practices
@Configuration
@EnableConfigurationProperties(ExampleParentProperties::class)
class ExampleConfiguration {}
@ConstructorBinding
@ConfigurationProperties(prefix = "io.github.artemptushkin.example")
data class ExampleParentProperties(
val child: ChildProperties = ChildProperties()
)
@artemptushkin
artemptushkin / DefaultHttpStatusCode.java
Created June 14, 2022 11:48
default-http-status-code.java
@Bean
public HttpStatus defaultStatus() {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
@artemptushkin
artemptushkin / ReactiveExceptionHandler.java
Created June 14, 2022 11:45
reactive-global-exception-handler.java
public class ReactiveExceptionHandler extends AbstractErrorWebExceptionHandler {
private final Map<Class<? extends Exception>, HttpStatus> exceptionToStatusCode;
private final HttpStatus defaultStatus;
public ReactiveExceptionHandler(ErrorAttributes errorAttributes, WebProperties.Resources resources,
ApplicationContext applicationContext, Map<Class<? extends Exception>, HttpStatus> exceptionToStatusCode,
HttpStatus defaultStatus) {
super(errorAttributes, resources, applicationContext);
this.exceptionToStatusCode = exceptionToStatusCode;
this.defaultStatus = defaultStatus;
@Bean
public Map<Class<? extends Exception>, HttpStatus> exceptionToStatusCode() {
return Map.of(
CustomExceptionInController.class, HttpStatus.BAD_REQUEST,
CustomExceptionInFilter.class, HttpStatus.BAD_REQUEST
);
}
@artemptushkin
artemptushkin / ServletExceptionHandler.java
Last active June 16, 2022 14:01
servlet-exception-handler
@Slf4j
@ControllerAdvice
@Profile("servlet")
@RequiredArgsConstructor
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class ServletExceptionHandler {
private final Map<Class<? extends Exception>, HttpStatus> exceptionToStatusCode;
private final HttpStatus defaultStatus;
@ExceptionHandler(CustomExceptionInFilter.class)
@artemptushkin
artemptushkin / ExceptionHandlingFilter.java
Created June 6, 2022 17:03
servlet-exception-handling-filter
@Component
@Profile("servlet")
@RequiredArgsConstructor
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class ExceptionHandlingFilter extends OncePerRequestFilter {
private final ServletExceptionHandler exceptionHandler;
/**
* naming this differently than _objectMapper_ you give a chance your code to pass a specific object mapper by the qualifier
* the field name will be considered as the name of the bean
*/
@artemptushkin
artemptushkin / how-to-get-value-from-header-dynamically.java
Created February 18, 2021 14:27
This gist demonstrates how to get dynamically a value from request header in Spring web stack on any layer different from `@Controller`
@Component
@RequiredArgsConstructor
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class HeaderValueProvider implements Supplier<String> { //supplier interface just for the API segregation
private final HttpServletRequest httpServletRequest;
@Override
public String get() {
return httpServletRequest.getHeader("someHeader");
}