Skip to content

Commit 5402151

Browse files
authored
Merge pull request #1 from blitss/feat/gh-actions
feat: add github workflow
2 parents c971f2e + ccce801 commit 5402151

File tree

6 files changed

+361
-100
lines changed

6 files changed

+361
-100
lines changed

.github/workflows/ci.yaml

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
env:
12+
CARGO_TERM_COLOR: always
13+
RUST_BACKTRACE: 1
14+
CARGO_INCREMENTAL: "false"
15+
16+
jobs:
17+
Test:
18+
strategy:
19+
fail-fast: false # We want all of them to run, even if one fails
20+
matrix:
21+
os: [ "ubuntu-latest" ]
22+
pg: [ "12", "13", "14", "15", "16" ]
23+
24+
runs-on: ${{ matrix.os }}
25+
env:
26+
RUSTC_WRAPPER: sccache
27+
SCCACHE_DIR: /home/runner/.cache/sccache
28+
RUST_TOOLCHAIN: ${{ matrix.rust || 'stable' }}
29+
steps:
30+
- uses: actions/checkout@v4
31+
- name: Set up prerequisites and environment
32+
run: |
33+
sudo apt-get update -y -qq --fix-missing
34+
35+
echo ""
36+
echo "----- Install sccache -----"
37+
mkdir -p $HOME/.local/bin
38+
curl -L https://github.com/mozilla/sccache/releases/download/v0.2.15/sccache-v0.2.15-x86_64-unknown-linux-musl.tar.gz | tar xz
39+
mv -f sccache-v0.2.15-x86_64-unknown-linux-musl/sccache $HOME/.local/bin/sccache
40+
chmod +x $HOME/.local/bin/sccache
41+
echo "$HOME/.local/bin" >> $GITHUB_PATH
42+
mkdir -p /home/runner/.cache/sccache
43+
echo ""
44+
45+
echo "----- Set up dynamic variables -----"
46+
cat $GITHUB_ENV
47+
echo ""
48+
49+
echo "----- Install system dependencies -----"
50+
sudo apt-get install -y \
51+
build-essential \
52+
llvm-14-dev libclang-14-dev clang-14 \
53+
gcc \
54+
libssl-dev \
55+
libz-dev \
56+
make \
57+
pkg-config \
58+
strace \
59+
zlib1g-dev
60+
echo ""
61+
echo "----- Print env -----"
62+
env
63+
echo ""
64+
65+
- name: Install release version of PostgreSQL
66+
run: |
67+
echo "----- Set up PostgreSQL Apt repository -----"
68+
sudo apt-get install -y wget gnupg
69+
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
70+
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
71+
sudo apt-get update -y -qq --fix-missing
72+
echo ""
73+
74+
sudo apt-get install -y \
75+
postgresql-${{ matrix.pg }} \
76+
postgresql-server-dev-${{ matrix.pg }}
77+
78+
echo ""
79+
echo "----- pg_config -----"
80+
pg_config
81+
echo ""
82+
- name: Set up PostgreSQL permissions
83+
run: sudo chmod a+rwx `/usr/lib/postgresql/${{ matrix.pg }}/bin/pg_config --pkglibdir` `/usr/lib/postgresql/${{ matrix.pg }}/bin/pg_config --sharedir`/extension /var/run/postgresql/
84+
85+
- name: Cache cargo registry
86+
uses: actions/cache@v4
87+
continue-on-error: false
88+
with:
89+
path: |
90+
~/.cargo/registry
91+
~/.cargo/git
92+
key: tests-${{ runner.os }}-${{ hashFiles('**/Cargo.lock', '.github/workflows/tests.yml') }}
93+
94+
- name: Cache sccache directory
95+
uses: actions/cache@v4
96+
continue-on-error: false
97+
with:
98+
path: /home/runner/.cache/sccache
99+
key: pgrx-tests-sccache-${{ runner.os }}-${{ hashFiles('**/Cargo.lock', '.github/workflows/tests.yml') }}
100+
101+
- name: Start sccache server
102+
run: sccache --start-server
103+
104+
- name: Print sccache stats (before run)
105+
run: sccache --show-stats
106+
107+
- name: Install cargo-pgrx
108+
run: |
109+
PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version')
110+
cargo install --locked --version=$PGRX_VERSION cargo-pgrx --debug --force
111+
cargo pgrx init --pg${{ matrix.pg }} /usr/lib/postgresql/${{ matrix.pg }}/bin/pg_config
112+
- name: Run tests
113+
run: echo "\q" | cargo pgrx run pg${{ matrix.pg }} && cargo test --no-default-features --features pg${{ matrix.pg }}
114+
115+
- name: Build
116+
run: cargo pgrx package --features pg${{ matrix.pg }} --pg-config /usr/lib/postgresql/${{ matrix.pg }}/bin/pg_config
117+
118+
- name: Archive production artifacts
119+
uses: actions/upload-artifact@v4
120+
with:
121+
name: typeid-${{matrix.pg}}
122+
path: |
123+
target/release/typeid-pg${{ matrix.pg }}
124+
# Attempt to make the cache payload slightly smaller.
125+
- name: Clean up built PGRX files
126+
run: |
127+
cd target/debug/deps/
128+
for built_file in $(find * -type f -executable -print | grep -v "\.so$"); do
129+
base_name=$(echo $built_file | cut -d- -f1);
130+
for basefile in "$base_name".*; do
131+
[ -f "$basefile" ] || continue;
132+
echo "Removing $basefile"
133+
rm $basefile
134+
done;
135+
echo "Removing $built_file"
136+
rm $built_file
137+
done
138+
- name: Stop sccache server
139+
run: sccache --stop-server || true
140+
Install:
141+
runs-on: ubuntu-latest
142+
steps:
143+
- uses: actions/checkout@v4
144+
- name: Install PostgreSQL headers
145+
run: |
146+
sudo apt-get update
147+
sudo apt-get install postgresql-server-dev-14
148+
- name: Install cargo-pgrx
149+
run: |
150+
PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version')
151+
cargo install --locked --version=$PGRX_VERSION cargo-pgrx --debug --force
152+
cargo pgrx init --pg14 $(which pg_config)
153+
- name: Install TypeID/pgrx
154+
run: |
155+
cargo pgrx install --no-default-features --release --sudo
156+
- name: Start PostgreSQL
157+
run: |
158+
sudo systemctl start postgresql.service
159+
pg_isready
160+
# superuser (-s), can create databases (-d) and roles (-r), no password prompt (-w) named runner
161+
sudo -u postgres createuser -s -d -r -w runner
162+
- name: Verify install
163+
run: |
164+
createdb -U runner runner
165+
psql -U runner -c "create extension typeid;"
166+
psql -U runner -c "select typeid_generate('user');"
167+
rustfmt:
168+
runs-on: ubuntu-latest
169+
steps:
170+
- name: Checkout code
171+
uses: actions/checkout@v4
172+
- name: Run rustfmt
173+
run: cargo fmt -- --check

.github/workflows/clippy.yaml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
# rust-clippy is a tool that runs a bunch of lints to catch common
6+
# mistakes in your Rust code and help improve your Rust code.
7+
# More details at https://github.com/rust-lang/rust-clippy
8+
# and https://rust-lang.github.io/rust-clippy/
9+
10+
name: rust-clippy analyze
11+
12+
on:
13+
push:
14+
branches: [ "main" ]
15+
pull_request:
16+
# The branches below must be a subset of the branches above
17+
branches: [ "main" ]
18+
schedule:
19+
- cron: '20 14 * * 5'
20+
21+
jobs:
22+
rust-clippy-analyze:
23+
name: Run rust-clippy analyzing
24+
runs-on: ubuntu-latest
25+
26+
strategy:
27+
matrix:
28+
pg: [ "16" ]
29+
permissions:
30+
contents: read
31+
security-events: write
32+
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v4
36+
37+
- name: Install Rust toolchain
38+
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af #@v1
39+
with:
40+
profile: minimal
41+
toolchain: stable
42+
components: clippy
43+
override: true
44+
45+
- name: Install release version of PostgreSQL
46+
run: |
47+
echo "----- Set up PostgreSQL Apt repository -----"
48+
sudo apt-get install -y wget gnupg
49+
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
50+
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
51+
sudo apt-get update -y -qq --fix-missing
52+
echo ""
53+
54+
sudo apt-get install -y \
55+
postgresql-${{ matrix.pg }} \
56+
postgresql-server-dev-${{ matrix.pg }}
57+
58+
echo ""
59+
echo "----- pg_config -----"
60+
pg_config
61+
echo ""
62+
63+
- name: Set up PostgreSQL permissions
64+
run: sudo chmod a+rwx `/usr/lib/postgresql/${{ matrix.pg }}/bin/pg_config --pkglibdir` `/usr/lib/postgresql/${{ matrix.pg }}/bin/pg_config --sharedir`/extension /var/run/postgresql/
65+
66+
- name: Install cargo-pgrx
67+
run: |
68+
PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version')
69+
cargo install --locked --version=$PGRX_VERSION cargo-pgrx --debug --force
70+
cargo pgrx init --pg${{ matrix.pg }} /usr/lib/postgresql/${{ matrix.pg }}/bin/pg_config
71+
72+
- name: Install required cargo
73+
run: cargo install clippy-sarif sarif-fmt
74+
75+
- name: Run rust-clippy
76+
run:
77+
cargo clippy
78+
--all-features
79+
--message-format=json | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt
80+
continue-on-error: true
81+
82+
- name: Upload analysis results to GitHub
83+
uses: github/codeql-action/upload-sarif@v3
84+
with:
85+
sarif_file: rust-clippy-results.sarif
86+
wait-for-processing: true

src/base32.rs

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,57 @@ pub enum Error {
99
}
1010

1111
fn decode_base32_to_u128(id: &str) -> Result<u128, Error> {
12-
let mut id: [u8; 26] = id.as_bytes().try_into().map_err(|_| Error::InvalidData)?;
13-
let mut max = 0;
14-
for b in &mut id {
15-
*b = CROCKFORD_INV[*b as usize];
16-
max |= *b;
17-
}
18-
if max > 32 || id[0] > 7 {
19-
return Err(Error::InvalidData);
20-
}
12+
let mut id: [u8; 26] = id.as_bytes().try_into().map_err(|_| Error::InvalidData)?;
13+
let mut max = 0;
14+
for b in &mut id {
15+
*b = CROCKFORD_INV[*b as usize];
16+
max |= *b;
17+
}
18+
if max > 32 || id[0] > 7 {
19+
return Err(Error::InvalidData);
20+
}
2121

22-
let mut out = 0u128;
23-
for b in id {
24-
out <<= 5;
25-
out |= b as u128;
26-
}
22+
let mut out = 0u128;
23+
for b in id {
24+
out <<= 5;
25+
out |= b as u128;
26+
}
2727

28-
Ok(out)
28+
Ok(out)
2929
}
3030

3131
fn encode_u128_to_base32(data: u128) -> String {
32-
let mut buf = [0u8; 26];
33-
let mut data = data;
34-
for i in (0..26).rev() {
35-
buf[i] = CROCKFORD[(data & 0x1f) as usize];
36-
debug_assert!(buf[i].is_ascii());
37-
data >>= 5;
38-
}
39-
unsafe { String::from_utf8_unchecked(buf.to_vec()) }
32+
let mut buf = [0u8; 26];
33+
let mut data = data;
34+
for i in (0..26).rev() {
35+
buf[i] = CROCKFORD[(data & 0x1f) as usize];
36+
debug_assert!(buf[i].is_ascii());
37+
data >>= 5;
38+
}
39+
unsafe { String::from_utf8_unchecked(buf.to_vec()) }
4040
}
4141

4242
const CROCKFORD: &[u8; 32] = b"0123456789abcdefghjkmnpqrstvwxyz";
4343
const CROCKFORD_INV: &[u8; 256] = &{
44-
let mut output = [255; 256];
44+
let mut output = [255; 256];
4545

46-
let mut i = 0;
47-
while i < 32 {
48-
output[CROCKFORD[i as usize] as usize] = i;
49-
i += 1;
50-
}
46+
let mut i = 0;
47+
while i < 32 {
48+
output[CROCKFORD[i as usize] as usize] = i;
49+
i += 1;
50+
}
5151

52-
output
52+
output
5353
};
5454

55-
5655
pub fn encode_base32_uuid(uuid: &Uuid) -> String {
57-
encode_u128_to_base32(uuid.as_u128())
56+
encode_u128_to_base32(uuid.as_u128())
5857
}
5958

6059
pub fn decode_base32_uuid(encoded: &str) -> Result<Uuid, Error> {
61-
decode_base32_to_u128(encoded).map(|result: u128| Uuid::from_u128(result))
60+
decode_base32_to_u128(encoded).map(|result: u128| Uuid::from_u128(result))
6261
}
6362

64-
6563
#[cfg(test)]
6664
mod tests {
6765
use uuid::Uuid;
@@ -76,4 +74,4 @@ mod tests {
7674
let decoded = decode_base32_uuid(&encoded).unwrap();
7775
assert_eq!(uuid, decoded);
7876
}
79-
}
77+
}

0 commit comments

Comments
 (0)