energia positiva #23
This file contains 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: Random Daily Commits | |
on: | |
schedule: | |
- cron: "0 11 * * *" # Executa uma vez ao dia as 11:00 UTC | |
workflow_dispatch: | |
push: | |
branches: | |
- main | |
permissions: | |
contents: write | |
pull-requests: write # Permissão para criar PR | |
jobs: | |
random_commits: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Set up Git user | |
run: | | |
git config --global user.name "peregrinno" | |
git config --global user.email "[email protected]" | |
- name: Create new branch | |
run: | | |
BRANCH_NAME="random-commits-$(date +%Y%m%d_%H%M%S)" | |
echo "Branch criada: $BRANCH_NAME" | |
git checkout -b $BRANCH_NAME | |
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV # Define a variável de ambiente | |
- name: Generate random number of commits | |
run: | | |
# Gera um número aleatório entre 1 e 10 | |
RANDOM_COMMITS=$(( ( RANDOM % 10 ) + 1 )) | |
echo "Gerando $RANDOM_COMMITS commits para o dia." | |
# Loop para gerar os commits | |
for i in $(seq 1 $RANDOM_COMMITS); do | |
echo "Commit número $i em $(date)" >> commit.txt | |
git add commit.txt | |
git commit -m "Commit automático $i de $RANDOM_COMMITS no dia $(date)" | |
done | |
- name: Create Python Hello World file with random prefix | |
run: | | |
PREFIX=$(shuf -i 10000-99999 -n 1) | |
FILENAME="auto_src/${PREFIX}_helloworld.py" | |
echo 'print("Hello, World!")' > $FILENAME | |
git add $FILENAME | |
git commit -m "Adicionando Hello World em Python com prefixo aleatório: $FILENAME" | |
- name: Create JavaScript Hello World file with random prefix | |
run: | | |
PREFIX=$(shuf -i 10000-99999 -n 1) | |
FILENAME="auto_src/${PREFIX}_helloworld.js" | |
echo 'console.log("Hello, World!");' > $FILENAME | |
git add $FILENAME | |
git commit -m "Adicionando Hello World em JavaScript com prefixo aleatório: $FILENAME" | |
- name: Push commits to new branch | |
env: | |
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} | |
run: | | |
# Certifique-se de que a nova branch seja enviada corretamente ao repositório remoto | |
git push --set-upstream https://x-access-token:${{ secrets.PAT_TOKEN }}@github.com/peregrinno/auto-commits.git $BRANCH_NAME | |
- name: Wait for branch to be available on remote | |
run: | | |
echo "Verificando se a branch foi enviada corretamente..." | |
REMOTE_BRANCH=$(git ls-remote --heads origin $BRANCH_NAME | wc -l) | |
while [ $REMOTE_BRANCH -eq 0 ]; do | |
echo "Branch $BRANCH_NAME ainda não está no repositório remoto. Tentando novamente em 10 segundos..." | |
sleep 10 | |
REMOTE_BRANCH=$(git ls-remote --heads origin $BRANCH_NAME | wc -l) | |
done | |
echo "Branch $BRANCH_NAME foi encontrada no repositório remoto." | |
- name: Wait for 10 seconds to ensure branch is processed | |
run: sleep 10 # Adicionando um atraso de 10 segundos | |
- name: Create Pull Request using GitHub API | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.PAT_TOKEN }} # Usando o PAT para criar o Pull Request | |
script: | | |
const branchName = process.env.BRANCH_NAME; | |
const pr = await github.rest.pulls.create({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
head: branchName, # Agora usando a variável corretamente | |
base: 'main', | |
title: `Adicionando commits automáticos via branch ${branchName}`, | |
body: 'Este pull request contém commits automáticos gerados pelo script.', | |
}); | |
console.log(`Pull Request criado: ${pr.data.html_url}`); | |
- name: Merge Pull Request | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.PAT_TOKEN }} # Usando o PAT para fazer o merge do Pull Request | |
script: | | |
const { data: pulls } = await github.pulls.list({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
state: 'open', | |
head: `${context.repo.owner}:${process.env.BRANCH_NAME}`, | |
}); | |
if (pulls.length > 0) { | |
const pr = pulls[0]; | |
await github.pulls.merge({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: pr.number, | |
merge_method: 'merge', | |
}); | |
} |