Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
- name: Set up Elixir
uses: erlef/setup-beam@v1 # https://github.com/erlef/setup-beam
with:
elixir-version: '1.14.2' # Define the elixir version [required]
otp-version: '24.3.4' # Define the OTP version [required]
elixir-version: '1.18.4' # Define the elixir version [required]
otp-version: '28.0.1' # Define the OTP version [required]
- name: Restore dependencies cache
uses: actions/cache@v4
with:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ _logs/
id_rsa_fly
id_rsa_fly-cert.pub
.vscode
backup.sql
backup.sql
backup.sql.zip
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<div align="center">

![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/dwyl/hits/ci.yml?label=build&style=flat-square&branch=main)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/dwyl/hits/ci.yml?label=build&style=flat-square&branch=main)](https://github.com/dwyl/hits/actions/workflows/ci.yml)
[![codecov.io](https://img.shields.io/codecov/c/github/dwyl/hits/master.svg?style=flat-square)](https://codecov.io/github/dwyl/hits?branch=master)
[![HitCount](https://hits.dwyl.com/dwyl/hits.svg)](https://github.com/dwyl/hits)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square)](https://github.com/dwyl/hits/issues/74)
Expand Down
14 changes: 14 additions & 0 deletions lib/hits/validate.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Hits.Validate do
@doc """
Validate GitHub user/org and repository name (Strings).
"""

# ^[[:alnum:]-_.]+$ means the name is composed of
# one or multiple alphanumeric character
# or "-_." characters
def repository_valid?(repo), do: String.match?(repo, ~r/^[[:alnum:]\-_.]+$/)

# see: https://github.com/dwyl/hits/issues/154
# alphanumeric follow by one or zero "-" or just alphanumerics
def user_valid?(user), do: String.match?(user, ~r/^([[:alnum:]]+-)*[[:alnum:]]+$/)
end
17 changes: 5 additions & 12 deletions lib/hits_web/controllers/hit_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule HitsWeb.HitController do
use HitsWeb, :controller
# use Phoenix.Channel
# import Ecto.Query
alias Hits.{Hit, Repository, User, Useragent}
alias Hits.{Hit, Repository, User, Useragent, Validate}

use Params

Expand Down Expand Up @@ -39,9 +39,11 @@ defmodule HitsWeb.HitController do
params = Params.data(schema)
params_map = Params.to_map(schema)

if schema.valid? and user_valid?(user) and repository_valid?(repo) do
if schema.valid?
and Validate.user_valid?(user)
and Validate.repository_valid?(repo) do
# insert hit. Note: the .svg is for legacy reasons 🙄
{_user_schema, _useragent_schema, repo} = insert_hit(conn, user, "#{repo}.svg")
{_user_schema, _ua_schema, repo} = insert_hit(conn, user, "#{repo}.svg")

count =
if params.show == "unique" do
Expand Down Expand Up @@ -203,13 +205,4 @@ defmodule HitsWeb.HitController do
|> send_resp(404, Hits.make_badge(404, params["style"]))
end
end

# see: https://github.com/dwyl/hits/issues/154
# alphanumeric follow by one or zero "-" or just alphanumerics
defp user_valid?(user), do: String.match?(user, ~r/^([[:alnum:]]+-)*[[:alnum:]]+$/)

# ^[[:alnum:]-_.]+$ means the name is composed of
# one or multiple alphanumeric character
# or "-_." characters
defp repository_valid?(repo), do: String.match?(repo, ~r/^[[:alnum:]-_.]+$/)
end
16 changes: 11 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ defmodule Hits.MixProject do
aliases: aliases(),
deps: deps(),
test_coverage: [tool: ExCoveralls],
preferred_cli_env: [
package: package(),
description: "Track page views on any GitHub page"
]
end

def cli do
[
preferred_envs: [
c: :test,
coveralls: :test,
"coveralls.detail": :test,
"coveralls.post": :test,
"coveralls.html": :test
],
package: package(),
description: "Track page views on any GitHub page"
"coveralls.html": :test,
docs: :docs
]
]
end

Expand Down
18 changes: 18 additions & 0 deletions test/hits/validate_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Hits.ValidateTest do
use ExUnit.Case, async: true
alias Hits.Validate

test "user_valid?/1 returns true if the string is a valid GitHub username" do
user = "pink-fluffy-unicorns-123"
# dbg(user)
assert Validate.user_valid?(user) == true
assert Validate.user_valid?("c@t") == false
end

test "repository_valid?/1 returns true if a string is a valid GitHub repo" do
repo = "pink-fluffy-unicorns_123"
# dbg(repo)
assert Validate.repository_valid?(repo) == true
assert Validate.repository_valid?("c@t") == false
end
end
Loading