Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle prs from forks #4

Merged
merged 1 commit into from
Aug 14, 2024
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
53 changes: 40 additions & 13 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ inputs:
# Needed for 'create-comment' step
plots:
description: 'Plots to be shown in comment'
repo_private_key:
description: 'Private key to access the plots repo'
validator_key:
description: 'Private ssh key to access the validator repo'
type: string

dev:
description: 'Run in development mode'
type: boolean
Expand All @@ -45,6 +44,24 @@ runs:
# ----------------------------------------
# 'run-self-hosted-validation' step
# ----------------------------------------
- name: General setup and checks
run: |
# Check if from fork
if [ "${{ github.repository }}" != "${{ github.event.pull_request.head.repo.full_name }}" ]; then
echo "Running from fork."
fork=true
exit 1 # Not supported yet
else
echo "Running from main repository."
fork=false
fi

# Bot config
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"

shell: bash

- name: Run self-hosted validation
if: ${{ inputs.step == 'run-self-hosted-validation' }}
run: |
Expand All @@ -62,7 +79,7 @@ runs:
# Fetch hash of main branch
_hash_main=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/commits/${{ github.base_ref }}" | jq -r '.sha')

# Fetch hash of feature branch (via PR number, also works for branches from forks)
pr_details=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}")
Expand All @@ -89,11 +106,14 @@ runs:
--config_file ${{ inputs.snakemake_config }} \
--pre_command "${{ inputs.pre_command }}" \
--main_command "ariadne_all"

shell: bash

# ----------------------------------------
# 'create-comment' step
# ----------------------------------------


- name: Download artifacts
if: ${{ inputs.step == 'create-comment' }}
uses: actions/download-artifact@v4
Expand All @@ -104,6 +124,7 @@ runs:
# Move artifacts to home directory
mkdir $HOME/artifacts
mv ./* $HOME/artifacts

shell: bash

- name: Get variables from artifacts
Expand Down Expand Up @@ -139,16 +160,19 @@ runs:

shell: bash

- name: Checkout main repo
- name: Checkout model repo
if: ${{ inputs.step == 'create-comment' }}
uses: actions/checkout@v4
with:
fetch-depth: 0
path: model-repo

- name: Get variables from repo
if: ${{ inputs.step == 'create-comment' }}
id: env-variables-repo
run: |
cd $GITHUB_WORKSPACE/model-repo

# Get git diff of used snakemake config
git_diff_config=$(git diff $HASH_MAIN $HASH_FEATURE -- ${{ inputs.snakemake_config }})

Expand All @@ -168,18 +192,21 @@ runs:

shell: bash

- name: Checkout plot repo
- name: Checkout validator repo
if: ${{ inputs.step == 'create-comment' }}
uses: actions/checkout@v4
with:
repository: lkstrp/pypsa-validator
ref: ${{ github.repository }}
ssh-key: ${{ inputs.repo_private_key }}
ssh-key: ${{ inputs.validator_key }}
path: validator-repo

- name: Upload relevant plots
id: upload-plots
if: ${{ inputs.step == 'create-comment' }}
run: |
cd $GITHUB_WORKSPACE/validator-repo

rm -rf _validation-images
mkdir -p _validation-images/feature
mkdir -p _validation-images/main
Expand All @@ -194,16 +221,13 @@ runs:
cp -r "$HOME/artifacts/results (feature branch)/${PREFIX_FEATURE}/${plot}" "_validation-images/feature" || true # ignore if run failed
done

# Bot config
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"

# Add plots to repo branch
echo "Adding plots to repo branch"
git add _validation-images
git commit -m "[github-actions.ci] upload validation images to show in comment" || true # ignore if no changes
git push origin ${{ github.repository }}
echo "COMMIT_ID=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT

shell: bash

- name: Set env for comment script
Expand All @@ -212,11 +236,14 @@ runs:
# This needs to be done in a separate step to ensure the output is available
echo "PLOTS_HASH=${{ steps.upload-plots.outputs.COMMIT_ID }}" >> $GITHUB_ENV
echo "PLOTS=${{ inputs.plots }}" >> $GITHUB_ENV

shell: bash

- name: Draft comment
if: ${{ inputs.step == 'create-comment' }}
run: |
cd $GITHUB_WORKSPACE/validator-repo

# Get potential changes from main branch
git fetch origin
if [ "${{ inputs.dev }}" = true ]; then
Expand All @@ -239,7 +266,7 @@ runs:

# Draft comment
# Note: The script uses many env variables. See the script for more details.
python draft_comment.py > comment.txt
python draft_comment.py > $GITHUB_WORKSPACE/comment.txt

shell: bash

Expand Down Expand Up @@ -293,7 +320,7 @@ runs:
with:
script: |
const fs = require('fs');
let comment = fs.readFileSync('comment.txt', 'utf8');
const comment = fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/comment.txt`, 'utf8');

github.rest.issues.updateComment({
owner: context.repo.owner,
Expand Down
4 changes: 3 additions & 1 deletion draft_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ def create_numeric_mask(arr: ArrayLike) -> np.ndarray:
return np.vectorize(lambda x: isinstance(x, (int, float)) and np.isfinite(x))(arr)


def get_env_var(var_name: str, default: Any = None) -> str:
def get_env_var(var_name: str, default: Any = None) -> Any:
"""Get environment variable or raise an error if not set and no default provided."""
value = os.getenv(var_name, default)
if value == "" and default is None:
msg = f"The environment variable '{var_name}' is not set."
raise OSError(msg)
if value.lower() in ["true", "false"]:
return value.lower() == "true"
return value


Expand Down