diff --git a/.github/workflows/ISSUE_TEMPLATE.md b/.github/workflows/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..716bdd5 --- /dev/null +++ b/.github/workflows/ISSUE_TEMPLATE.md @@ -0,0 +1,13 @@ +--- +title: Broken links +labels: broken-links +assignees: '' +--- + +Elenco dei link problematici: +{% for link in env.BROKEN_LINKS.split(',') %} +- {{ link }} +{% endfor %} + + + diff --git a/.github/workflows/controllo_link.yml b/.github/workflows/controllo_link.yml new file mode 100644 index 0000000..4309789 --- /dev/null +++ b/.github/workflows/controllo_link.yml @@ -0,0 +1,61 @@ +name: Broken Links Checker +on: + push: + branches: + - master + schedule: + - cron: '0 0 1 * *' +env: + ISSUE_TEMPLATE: ".github/workflows/ISSUE_TEMPLATE.md" + + contents: read + issues: write + +jobs: + check: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Get links from README.md + id: get-links + run: | + LINKS=$(grep -oP '\[.*?\]\((http[s]?://[^)]*)\)' README.md | sed -E 's/\[.*\]\(([^)]+)\)/\1/' | paste -sd "," -) + echo "WEBSITE_URL=$LINKS" >> $GITHUB_ENV + + - name: Run Broken Links Checker + run: | + # Split the list of links into an array separated by commas + IFS=',' read -ra LINKS <<< "$WEBSITE_URL" + BROKEN_LINKS="" + + # Scan the links and verify if they are reachable + for link in "${LINKS[@]}"; do + if [[ $link =~ ^(http|https):// ]]; then + # If the link starts with "http://" or "https://", check if it is reachable + if ! curl -IsSk "$link" > /dev/null; then + BROKEN_LINKS="$BROKEN_LINKS,$link" + fi + fi + done + + # Remove the initial comma if there are unreachable links + if [ -n "$BROKEN_LINKS" ]; then + BROKEN_LINKS=$(echo "$BROKEN_LINKS" | cut -c 2-) + echo "BROKEN_LINKS=$BROKEN_LINKS" >> $GITHUB_ENV + echo "Broken links found: $BROKEN_LINKS" >&2 + echo "Broken links found, failing the workflow" >&2 + exit 1 + else + echo "No broken links found" + fi + + - uses: actions/checkout@v3 + if: failure() + + - uses: JasonEtco/create-an-issue@v2 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + filename: ${{ env.ISSUE_TEMPLATE }} + if: failure()