func()

in pkg/client/sonar/sonar.go [88:128]


func (sc *Client) ChangePassword(ctx context.Context, user string, oldPassword string, newPassword string) error {
	resp, err := sc.startRequest(ctx).Get("/system/health")
	if err != nil {
		return fmt.Errorf("failed check sonar health: %w", err)
	}

	if err = sc.checkError(resp, err); err != nil {
		return fmt.Errorf("failed to check sonar health: %w", err)
	}

	var systemHealthResponse SystemHealthResponse

	if err = json.Unmarshal(resp.Body(), &systemHealthResponse); err != nil {
		return fmt.Errorf(cantUnmarshalMsg, resp.Body(), err)
	}

	// make sure that Sonar is up and running
	if systemHealthResponse.Health != "GREEN" {
		return fmt.Errorf("sonar is not in green state, current state - %s; %v", systemHealthResponse.Health, systemHealthResponse)
	}

	resp, err = sc.startRequest(ctx).
		SetFormData(map[string]string{
			loginField:         user,
			"password":         newPassword,
			"previousPassword": oldPassword,
		}).
		Post("/users/change_password")

	if err = sc.checkError(resp, err); err != nil {
		return fmt.Errorf("failed to change password: %w", err)
	}

	// so starting from SonarQube 8.9.9 they changed flow of "/api/users/change_password" endpoint
	// after successful change of password, Sonar refresh JWT token in cookie,
	// so we need to update cookie to get new token
	// https://github.com/SonarSource/sonarqube/commit/eb6741754b2b35172012bc5b30f5b0d53a61f7be#diff-be83bcff4cfc3fb4d04542ca6eea91cfe7738b7bd754d86eb366ce3e18b0aa34
	sc.resty.SetCookies(resp.Cookies())

	return nil
}