可靠的 #43
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: Repo Management | |
on: | |
pull_request_target: | |
types: [closed] | |
schedule: | |
- cron: '0 0 * * *' # 每天午夜运行 | |
push: | |
branches: [ main ] # 每次推送到main分支时也运行 | |
permissions: | |
contents: write | |
pull-requests: write | |
jobs: | |
invite-collaborator: | |
runs-on: ubuntu-latest | |
if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == true | |
steps: | |
- name: Check if user is already a collaborator | |
id: check-collaborator | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.PAT_WITH_INVITE_PERMISSIONS }} | |
script: | | |
const { owner, repo } = context.repo; | |
const username = context.payload.pull_request.user.login; | |
try { | |
console.log(`Checking if ${username} is already a collaborator...`); | |
await github.rest.repos.getCollaboratorPermissionLevel({ | |
owner, | |
repo, | |
username, | |
}); | |
console.log(`${username} is already a collaborator.`); | |
core.setOutput('is_collaborator', 'true'); | |
} catch (error) { | |
if (error.status === 404) { | |
console.log(`${username} is not a collaborator.`); | |
core.setOutput('is_collaborator', 'false'); | |
} else { | |
console.error(`Error checking collaborator status: ${error.message}`); | |
throw error; | |
} | |
} | |
- name: Invite collaborator | |
id: invite-collaborator | |
if: steps.check-collaborator.outputs.is_collaborator == 'false' | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.PAT_WITH_INVITE_PERMISSIONS }} | |
script: | | |
const { owner, repo } = context.repo; | |
const username = context.payload.pull_request.user.login; | |
try { | |
console.log(`Attempting to invite ${username} as a collaborator...`); | |
const response = await github.rest.repos.addCollaborator({ | |
owner, | |
repo, | |
username, | |
permission: 'write' | |
}); | |
console.log(`Invitation API response status: ${response.status}`); | |
if (response.status === 201) { | |
console.log(`Invitation sent to ${username} as a collaborator with write permission.`); | |
core.setOutput('invitation_sent', 'true'); | |
} else if (response.status === 204) { | |
console.log(`${username} is already a collaborator.`); | |
core.setOutput('invitation_sent', 'false'); | |
} | |
} catch (error) { | |
console.error(`Error details: ${JSON.stringify(error)}`); | |
core.setFailed(`Error inviting collaborator: ${error.message}`); | |
} | |
- name: Comment on PR | |
if: steps.check-collaborator.outputs.is_collaborator == 'false' && steps.invite-collaborator.outputs.invitation_sent == 'true' | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const { owner, repo } = context.repo; | |
const issue_number = context.payload.pull_request.number; | |
const username = context.payload.pull_request.user.login; | |
try { | |
await github.rest.issues.createComment({ | |
owner, | |
repo, | |
issue_number, | |
body: `Thanks for your contribution, @${username}! You've been invited as a collaborator with write permissions. Please check your email for the invitation.` | |
}); | |
console.log(`Comment posted on PR #${issue_number}`); | |
} catch (error) { | |
console.error(`Error posting comment: ${error.message}`); | |
core.setFailed(`Error posting comment: ${error.message}`); | |
} | |
update-readme: | |
runs-on: ubuntu-latest | |
if: github.event_name == 'schedule' || github.event_name == 'push' | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Set up Python | |
uses: actions/setup-python@v2 | |
with: | |
python-version: '3.x' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install PyGithub pytz | |
- name: Update README | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: python sync_status_readme.py | |
- name: Check for changes | |
id: git-check | |
run: | | |
git diff --exit-code README.md || echo "modified=true" >> $GITHUB_OUTPUT | |
- name: Commit changes | |
if: steps.git-check.outputs.modified == 'true' | |
run: | | |
git config --local user.email "[email protected]" | |
git config --local user.name "GitHub Action" | |
git add README.md | |
git commit -m "Update commit status table" | |
git push |