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

Branch protection? #3

Open
akielaries opened this issue Dec 23, 2022 · 5 comments
Open

Branch protection? #3

akielaries opened this issue Dec 23, 2022 · 5 comments

Comments

@akielaries
Copy link

I am noticing while trying to use this workflow in my repo, that it essentially requires no branch protection rules in place. At first it gave me errors with my rule for requiring a review before pushing to main:
image

Upon disabling that and verified commits, I get an issue with pushes to main being required through a pull request:
image

Is there a way to use this workflow while keeping branch protection rules in place?

@akielaries akielaries changed the title Branch protection + this workflow Branch protection? Dec 25, 2022
@MShawon
Copy link
Owner

MShawon commented Dec 25, 2022

I'm not sure why this is happening

but can you create a master branch and put the yml file there?

@RinoReyns
Copy link

I have the same issue and I can create yml file on github and push the main branch. I tried with different token setting. After all I decided to trigger this action once a week and generate PR. It is good enough for me.

@akielaries
Copy link
Author

I remember doing something similar but disliked the idea of having to manually intervene on a workflow. I ended up using a different action that's able to create a badge and update it based on the *.yml file. It requires a few steps but is worth not having to manually intervene on automated pieces.

@MShawon
Copy link
Owner

MShawon commented Feb 7, 2023

@akielaries @RinoReyns
can you let me know if adding branch: ${{ github.head_ref }} at the end of yml file resolves the issue?

name: GitHub Clone Count Update Everyday

on:
  schedule:
    - cron: "0 */24 * * *"
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      
      - name: gh login
        run: echo "${{ secrets.SECRET_TOKEN }}" | gh auth login --with-token

      - name: parse latest clone count
        run: |
          curl --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
            -H "Accept: application/vnd.github.v3+json" \
            https://api.github.com/repos/${{ github.repository }}/traffic/clones \
            > clone.json
      - name: create gist and download previous count
        id: set_id
        run: |
          if gh secret list | grep -q "GIST_ID"
          then
              echo "GIST_ID found"
              echo ::set-output name=GIST::${{ secrets.GIST_ID }}
              curl https://gist.githubusercontent.com/${{ github.actor }}/${{ secrets.GIST_ID }}/raw/clone.json > clone_before.json
              if cat clone_before.json | grep '404: Not Found'; then
                echo "GIST_ID not valid anymore. Creating another gist..."
                gist_id=$(gh gist create clone.json | awk -F / '{print $NF}')
                echo $gist_id | gh secret set GIST_ID
                echo ::set-output name=GIST::$gist_id
                cp clone.json clone_before.json
                git rm --ignore-unmatch  CLONE.md
              fi
          else
              echo "GIST_ID not found. Creating a gist..."
              gist_id=$(gh gist create clone.json | awk -F / '{print $NF}')
              echo $gist_id | gh secret set GIST_ID
              echo ::set-output name=GIST::$gist_id
              cp clone.json clone_before.json
          fi
      - name: update clone.json
        run: |
          curl https://raw.githubusercontent.com/MShawon/github-clone-count-badge/master/main.py > main.py
          python3 main.py
      - name: Update gist with latest count
        run: |
          content=$(sed -e 's/\\/\\\\/g' -e 's/\t/\\t/g' -e 's/\"/\\"/g' -e 's/\r//g' "clone.json" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g')
          echo '{"description": "${{ github.repository }} clone statistics", "files": {"clone.json": {"content": "'"$content"'"}}}' > post_clone.json
          curl -s -X PATCH \
            --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
            -H "Content-Type: application/json" \
            -d @post_clone.json https://api.github.com/gists/${{ steps.set_id.outputs.GIST }} > /dev/null 2>&1
          if [ ! -f CLONE.md ]; then
            shields="https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count&url="
            url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set_id.outputs.GIST }}/raw/clone.json"
            repo="https://github.com/MShawon/github-clone-count-badge"
            echo ''> CLONE.md
            echo '
            **Markdown**
            ```markdown' >> CLONE.md
            echo "[![GitHub Clones]($shields$url&logo=github)]($repo)" >> CLONE.md
            echo '
            ```
            **HTML**
            ```html' >> CLONE.md
            echo "<a href='$repo'><img alt='GitHub Clones' src='$shields$url&logo=github'></a>" >> CLONE.md
            echo '```' >> CLONE.md
            
            git add CLONE.md
            git config --global user.name "GitHub Action"
            git config --global user.email "[email protected]"
            git commit -m "create clone count badge"
          fi
      - name: Push
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.head_ref }}

@victory316
Copy link

I faced a simliar issue.

After I added this block on bottom,


      - name: Push
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.head_ref }}

And also adding checkout with reference solved the issue.

      - uses: actions/checkout@v3
        with:
          ref: ${{ github.head_ref }}
          token: ${{ secrets.SECRET_TOKEN}} 
      - name: gh login
        run: echo "${{ secrets.SECRET_TOKEN }}" | gh auth login --with-token

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants