Skip to content

Commit bbb95ea

Browse files
committed
feat: better git diff retrieval, artifact handeling and smaller fixes
1 parent 4df7e78 commit bbb95ea

File tree

2 files changed

+62
-46
lines changed

2 files changed

+62
-46
lines changed

action.yml

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ runs:
5252
# ----------------------------------------
5353
# 'create-comment' step
5454
# ----------------------------------------
55+
- name: Download artifacts
56+
if: ${{ inputs.step == 'create-comment' }}
57+
uses: actions/download-artifact@v4
58+
59+
- name: Move artifacts to home directory
60+
run: |
61+
# Move artifacts to home directory
62+
mkdir $HOME/artifacts
63+
mv ./* $HOME/artifacts
64+
shell: bash
65+
66+
- name: Get variables from artifacts
67+
if: ${{ inputs.step == 'create-comment' }}
68+
id: env-variables-artifacts
69+
run: |
70+
# Get compared hashes
71+
hash_base=$(yq e '.metadata.hash_base' $HOME/artifacts/validator-metadata/validator-metadata.yml)
72+
hash_feature=$(yq e '.metadata.hash_feature' $HOME/artifacts/validator-metadata/validator-metadata.yml)
73+
74+
# Assert variables found
75+
if [[ -z $hash_base || -z $hash_feature ]]; then
76+
echo "Error: Commit hashes not found"
77+
exit 1
78+
fi
79+
80+
# Print and save variables
81+
echo "Main commit hash: ${hash_base}"
82+
echo "Feature commit hash: ${hash_feature}"
83+
echo "hash_base=${hash_base}" >> $GITHUB_OUTPUT
84+
echo "hash_feature=${hash_feature}" >> $GITHUB_OUTPUT
85+
shell: bash
86+
5587
- name: Checkout main repo
5688
if: ${{ inputs.step == 'create-comment' }}
5789
uses: actions/checkout@v4
@@ -67,17 +99,22 @@ runs:
6799
echo "Config prefix: ${config_prefix}"
68100
echo "config_prefix=${config_prefix}" >> $GITHUB_OUTPUT
69101
70-
# git diff config
71-
rm -f ~/git_diff_config.txt
72-
git_diff_config=$(git diff origin/${{ github.base_ref }}..origin/${{ github.head_ref }} -- ${{ inputs.snakemake_config }})
73-
echo "Git diff config: ${git_diff_config}"
74-
echo "git_diff_config=${git_diff_config}" >> ~/git_diff_config.txt
75102
76-
# Assert variables found
77-
if [[ -z $config_prefix ]]; then
78-
echo "Error: Config prefix not found"
79-
exit 1
80-
fi
103+
# Get git diff of used snakemake config
104+
git_diff_config=$(git diff ${{ steps.env-variables-artifacts.outputs.hash_base }} ${{ steps.env-variables-artifacts.outputs.hash_feature }} -- ${{ inputs.snakemake_config }})
105+
# echo "GIT_DIFF_CONFIG='${git_diff_config}'" >> $GITHUB_ENV
106+
echo "GIT_DIFF_CONFIG<<EOF" >> $GITHUB_ENV
107+
echo "${git_diff_config}" >> $GITHUB_ENV
108+
echo "EOF" >> $GITHUB_ENV
109+
echo "git_diff_config:\n${git_diff_config}"
110+
111+
# Get the number of commits ahead of main
112+
behind_count=$(git rev-list --right-only --count ${{ steps.env-variables-artifacts.outputs.hash_feature }}...${{ steps.env-variables-artifacts.outputs.hash_base }})
113+
ahead_count=$(git rev-list --left-only --count ${{ steps.env-variables-artifacts.outputs.hash_feature }}...${{ steps.env-variables-artifacts.outputs.hash_base }})
114+
echo "BEHIND_COUNT=${behind_count}" >> $GITHUB_ENV
115+
echo "behind_count: ${behind_count}"
116+
echo "AHEAD_COUNT=${ahead_count}" >> $GITHUB_ENV
117+
echo "ahead_count: ${ahead_count}"
81118
shell: bash
82119

83120
- name: Checkout plot repo
@@ -88,32 +125,6 @@ runs:
88125
ref: ${{ github.event.repository.name }}
89126
ssh-key: ${{ inputs.repo_private_key }}
90127

91-
- name: Download artifacts
92-
if: ${{ inputs.step == 'create-comment' }}
93-
uses: actions/download-artifact@v4
94-
95-
- name: Get variables from artifacts
96-
if: ${{ inputs.step == 'create-comment' }}
97-
id: env-variables-artifacts
98-
run: |
99-
# Get compared hashes
100-
hash_base=$(yq e '.metadata.hash_base' validator-metadata/validator-metadata.yml)
101-
feature_commit_hash=$(yq e '.metadata.feature_commit_hash' validator-metadata/validator-metadata.yml)
102-
103-
# Assert variables found
104-
if [[ -z $hash_base || -z $feature_commit_hash ]]; then
105-
echo "Error: Commit hashes not found"
106-
exit 1
107-
fi
108-
109-
# Print and save variables
110-
echo "Main commit hash: ${hash_base}"
111-
echo "Feature commit hash: ${feature_commit_hash}"
112-
echo "hash_base=${hash_base}" >> $GITHUB_OUTPUT
113-
echo "hash_feature=${feature_commit_hash}" >> $GITHUB_OUTPUT
114-
115-
shell: bash
116-
117128
- name: Upload relevant plots
118129
id: upload-plots
119130
if: ${{ inputs.step == 'create-comment' }}
@@ -128,8 +139,8 @@ runs:
128139
for plot in "${plots_array[@]}"
129140
do
130141
echo "Copying ${plot}"
131-
cp -r "results (feature branch)/${{ steps.env-variables-repo.outputs.config_prefix }}/${plot}" "_validation-images/feature"
132-
cp -r "results (base branch)/${{ steps.env-variables-repo.outputs.config_prefix }}/${plot}" "_validation-images/base"
142+
cp -r "$HOME/artifacts/results (feature branch)/${{ steps.env-variables-repo.outputs.config_prefix }}/${plot}" "_validation-images/feature"
143+
cp -r "$HOME/artifacts/results (base branch)/${{ steps.env-variables-repo.outputs.config_prefix }}/${plot}" "_validation-images/base"
133144
done
134145
135146
# Bot config
@@ -151,9 +162,6 @@ runs:
151162
git fetch origin
152163
git merge origin/main --allow-unrelated-histories
153164
154-
git_diff_config=$(cat ~/git_diff_config.txt)
155-
export GIT_DIFF_CONFIG=$(cat ~/git_diff_config.txt)
156-
157165
echo "${{ inputs.plots }}"
158166
pip install -r requirements.txt
159167
python draft_comment.py \
@@ -164,8 +172,8 @@ runs:
164172
--hash_base "${{ steps.env-variables-artifacts.outputs.hash_base }}" \
165173
--hash_feature "${{ steps.env-variables-artifacts.outputs.hash_feature }}" \
166174
--config_prefix "${{ steps.env-variables-repo.outputs.config_prefix }}" \
167-
--dir_base "results (base branch)" \
168-
--dir_feature "results (feature branch)" \
175+
--dir_base "$HOME/artifacts/results (base branch)" \
176+
--dir_feature "$HOME/artifacts/results (feature branch)" \
169177
--plots_hash ${{ steps.upload-plots.outputs.COMMIT_ID }} \
170178
--plots "${{ inputs.plots }}" \
171179
> comment.txt

draft_comment.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ def __init__(
135135
branch_name_base: str,
136136
branch_name_feature: str,
137137
config_prefix: str,
138+
ahead_count: str,
139+
behind_count: str,
138140
git_diff_config: str,
139141
hash_base: str,
140142
hash_feature: str,
@@ -150,6 +152,8 @@ def __init__(
150152
self.branch_name_base = branch_name_base
151153
self.config_prefix = config_prefix
152154
self.git_diff_config = git_diff_config
155+
self.ahead_count = ahead_count
156+
self.behind_count = behind_count
153157
self.hash_base = hash_base
154158
self.hash_feature = hash_feature
155159
self.dir_base = dir_base
@@ -188,7 +192,7 @@ def config_diff(self) -> str:
188192
f"<details>\n"
189193
f" <summary>:warning: Config changes detected!</summary>\n"
190194
f"\n"
191-
f"Results may differ due to these changes\n"
195+
f"Results may differ due to these changes:\n"
192196
f"```diff\n"
193197
f"{self.git_diff_config}\n"
194198
f"```\n"
@@ -345,12 +349,12 @@ def subtext(self) -> str:
345349
if self.hash_feature:
346350
hash_feature = (
347351
f"([{self.hash_feature[:7]}](https://github.com/"
348-
"{self.repo}/commits/{self.hash_feature})) "
352+
f"{self.repo}/commits/{self.hash_feature})) "
349353
)
350354
if self.hash_base:
351355
hash_base = (
352356
f"([{self.hash_base[:7]}](https://github.com/"
353-
"{self.repo}/commits/{self.hash_base}))"
357+
f"{self.repo}/commits/{self.hash_base}))"
354358
)
355359
time = (
356360
pd.Timestamp.now()
@@ -361,6 +365,8 @@ def subtext(self) -> str:
361365
return (
362366
f"Comparing {self.branch_name_feature} {hash_feature}with "
363367
f"{self.branch_name_base} {hash_base}.\n"
368+
f"Branch is {self.ahead_count} commits ahead and {self.behind_count} "
369+
f"commits behind `{self.branch_name_base}`.\n"
364370
f"Last updated on {time}."
365371
)
366372

@@ -410,6 +416,8 @@ def __repr__(self) -> str:
410416
hash_feature=args.hash_feature,
411417
config_prefix=args.config_prefix,
412418
git_diff_config=os.getenv("GIT_DIFF_CONFIG", ""),
419+
ahead_count=os.getenv("AHEAD_COUNT", ""),
420+
behind_count=os.getenv("BEHIND_COUNT", ""),
413421
dir_base=args.dir_base,
414422
dir_feature=args.dir_feature,
415423
plots_hash=args.plots_hash,

0 commit comments

Comments
 (0)