Skip to content

Commit

Permalink
refactor(recipients-selection): move files and update scripts (#607)
Browse files Browse the repository at this point in the history
* moved draw logic into its own folder
* draws now parse the file name for the number of items to draw
* draws now use the `dchoose` tool rather than a github action
* Prettified Code!

---------

Co-authored-by: CluEleSsUK <[email protected]>
  • Loading branch information
CluEleSsUK and CluEleSsUK authored Nov 8, 2023
1 parent 5756f8c commit 17463e5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/execute-draw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ jobs:
with:
node-version: latest

- name: Install dchoose 🔧
run: npm install -g dchoose

- name: Make folders if necessary 📁
run: mkdir -p draws
run: |
mkdir -p ./recipients_selection/draws
mkdir -p ./recipients_selection/lists
- name: Draw 🎲
uses: drand/[email protected]
with:
inputDir: ./lists
outputDir: ./draws
count: 10
working-directory: recipients_selection
run: ./draw.sh

- name: Commit 🔐
run: |
git config --global user.name $BOT_NAME
Expand Down
20 changes: 20 additions & 0 deletions recipients_selection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Recipients selection

This directory contains everything related to the draw process.
Recipients lists are added to a private repo, their names are salted and
then the hashed versions get committed (automatically) to files in the
[./lists](./lists) directory. Once a new hashed recipient list hits the
main branch of this repo, the
[github draw action](../.github/workflows/execute-draw.yml) runs the
[draw.sh](./draw.sh) script. That script filters for lists that don't
have a corresponding draw, parses the filename for the number of
recipients to draw from the list, and triggers a draw using
[drand](https://drand.love). The randomness is not known until after the
list has been committed to, thus ensuring the selection process cannot
be biased. The output of that draw is then committed to a corresponding
file in the [./draws](./draws) directory. These draws are repeatable -
you can take the hashed recipient list and the corresponding drand
randomness, and re-run the draw using the
[dchoose](https://github.com/drand/dchoose) tool to confirm that the
outputs are the same. For further information on the draw process, check
out the [Social Income website](https://socialincome.org).
28 changes: 28 additions & 0 deletions recipients_selection/draw.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

set -eux -o pipefail

# first we make some temp files to store each dirs contents
list_file=$(mktemp)
draw_file=$(mktemp)

# we then list all the files in the list and draw dirs respectively
$(cd ./lists && find . -type f | sort > "$list_file")
$(cd ./draws && find . -type f | sort > "$draw_file")

# we compare the contents to find files that exist as lists but not as draws
remaining_draw_filenames=$(comm -3 "$list_file" "$draw_file" | awk '{print $1}')

# we then actually run the draw
while IFS= read -r filename; do
# first we strip any leading ./
count_to_draw="${filename#./}"
# then we extract the number at the start of the filename
count_to_draw="${count_to_draw%%-*}"

# we then perform the draw, and write the output to the draw file
dchoose --count "$count_to_draw" --verbose < "./lists/$filename" > "./draws/$filename"
done <<< "$remaining_draw_filenames"

# clean up the temp files we created for storing the outputs of `ls`
rm -rf "$list_file" "$draw_file"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 17463e5

Please sign in to comment.