Merge pull request #19 from rosltahel/main #36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Generate Badge on Main Change | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| generate-badge: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| issues: write | |
| pull-requests: write | |
| contents: read | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v3 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.x' | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| - name: Install libGL | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y libgl1 | |
| - name: Fetch User Details | |
| id: fetch_user_details | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| # Fetch the commit details | |
| COMMIT_SHA="${{ github.sha }}" | |
| COMMIT_DETAILS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ | |
| https://api.github.com/repos/${{ github.repository }}/commits/$COMMIT_SHA) | |
| COMMIT_AUTHOR_LOGIN=$(echo "$COMMIT_DETAILS" | jq -r '.author.login') | |
| COMMIT_AUTHOR_NAME=$(echo "$COMMIT_DETAILS" | jq -r '.commit.author.name') | |
| # Fetch PRs associated with the commit | |
| PRS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ | |
| https://api.github.com/repos/${{ github.repository }}/commits/$COMMIT_SHA/pulls) | |
| # If there are PRs, use the PR author details | |
| if [ "$(echo "$PRS" | jq 'length')" -gt 0 ]; then | |
| PR_AUTHOR_LOGIN=$(echo "$PRS" | jq -r '.[0].user.login') | |
| PR_AUTHOR_DETAILS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ | |
| https://api.github.com/users/$PR_AUTHOR_LOGIN) | |
| PR_AUTHOR_NAME=$(echo "$PR_AUTHOR_DETAILS" | jq -r '.name') | |
| # Use PR author details if available | |
| USER_NAME="$PR_AUTHOR_LOGIN" | |
| USER_FULL_NAME="$PR_AUTHOR_NAME" | |
| else | |
| # Use commit author details if no PR is associated | |
| USER_NAME="$COMMIT_AUTHOR_LOGIN" | |
| USER_FULL_NAME="$COMMIT_AUTHOR_NAME" | |
| fi | |
| # Fallback to GitHub username if full name is not available | |
| if [ -z "$USER_FULL_NAME" ] || [ "$USER_FULL_NAME" == "null" ]; then | |
| USER_FULL_NAME="$USER_NAME" | |
| fi | |
| echo "USER_NAME=$USER_NAME" >> $GITHUB_ENV | |
| echo "USER_FULL_NAME=$USER_FULL_NAME" >> $GITHUB_ENV | |
| - name: Run generate_badge.py with user details | |
| id: generate_badge | |
| env: | |
| PRIVKEY_BASE64: ${{ secrets.PRIVKEY_BASE64 }} | |
| AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} | |
| run: | | |
| if [ -n "$AWS_ACCESS_KEY_ID" ]; then | |
| echo "AWS credentials are set" | |
| else | |
| echo "AWS credentials are missing" | |
| fi | |
| output=$(python generate_badge.py "$USER_FULL_NAME" "$USER_NAME" "github-basic") | |
| echo "badge_url=${output}" >> $GITHUB_OUTPUT # Capture the URL output | |
| - name: Comment on the merge | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| // Get the badge URL from the output of the Python script | |
| const badgeUrl = '${{ steps.generate_badge.outputs.badge_url }}'; | |
| // Create a comment with the badge URL | |
| const comment = `Here is the badge: ${badgeUrl}`; | |
| // Fetch the PR associated with the commit (if any) | |
| const commitSha = '${{ github.sha }}'; | |
| const prs = await github.rest.repos.listPullRequestsAssociatedWithCommit({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| commit_sha: commitSha, | |
| }); | |
| if (prs.data.length > 0) { | |
| // Comment on the first PR associated with the commit | |
| await github.rest.issues.createComment({ | |
| issue_number: prs.data[0].number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: comment, | |
| }); | |
| console.log(`Commented on PR #${prs.data[0].number}`); | |
| } else { | |
| console.log('No PR associated with this commit. Skipping comment.'); | |
| } |