-
Notifications
You must be signed in to change notification settings - Fork 0
169 lines (140 loc) · 5.98 KB
/
Copy pathaccess-request.yml
File metadata and controls
169 lines (140 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
name: Repository Access Request
on:
issues:
types: [opened, edited]
jobs:
process-access-request:
if: contains(github.event.issue.title, '[ACCESS REQUEST]')
runs-on: ubuntu-latest
permissions:
issues: write
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Parse issue body
id: parse
run: |
BODY="${{ github.event.issue.body }}"
# Extract GitHub username
GITHUB_USERNAME=$(echo "$BODY" | grep -oP '(?<=GitHub Username: ).*')
echo "GITHUB_USERNAME=$GITHUB_USERNAME" >> $GITHUB_ENV
# Extract repository name
REPO_NAME=$(echo "$BODY" | grep -oP '(?<=Repository: ).*')
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV
# Extract reason
REASON=$(echo "$BODY" | grep -oP '(?<=Reason: ).*')
echo "REASON=$REASON" >> $GITHUB_ENV
# Extract email
EMAIL=$(echo "$BODY" | grep -oP '(?<=Email: ).*')
echo "EMAIL=$EMAIL" >> $GITHUB_ENV
# Extract name
NAME=$(echo "$BODY" | grep -oP '(?<=Name: ).*')
echo "NAME=$NAME" >> $GITHUB_ENV
- name: Add approval comment
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ github.event.issue.number }}
body: |
## Access Request Details
- **Name**: ${{ env.NAME }}
- **GitHub Username**: ${{ env.GITHUB_USERNAME }}
- **Repository**: ${{ env.REPO_NAME }}
- **Reason**: ${{ env.REASON }}
### Approval Options
@alejandrogarcia-iteso, please review this request and approve or deny access:
- To approve: Add a comment with the text `/approve`
- To deny: Add a comment with the text `/deny`
Once approved, the user will be automatically added as a collaborator to the repository.
- name: Add labels
uses: andymckay/labeler@master
with:
add-labels: "access-request, pending-approval"
repo-token: ${{ secrets.GITHUB_TOKEN }}
process-approval:
if: contains(github.event.comment.body, '/approve')
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- name: Parse issue body
id: parse
run: |
ISSUE_NUMBER=${{ github.event.issue.number }}
ISSUE_BODY=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER" | jq -r .body)
# Extract GitHub username
GITHUB_USERNAME=$(echo "$ISSUE_BODY" | grep -oP '(?<=GitHub Username: ).*')
echo "GITHUB_USERNAME=$GITHUB_USERNAME" >> $GITHUB_ENV
# Extract repository name
REPO_NAME=$(echo "$ISSUE_BODY" | grep -oP '(?<=Repository: ).*')
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV
- name: Add collaborator
uses: actions/github-script@v6
with:
github-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
script: |
try {
await github.rest.repos.addCollaborator({
owner: 'gamaware',
repo: process.env.REPO_NAME,
username: process.env.GITHUB_USERNAME,
permission: 'pull'
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `✅ Access granted! @${process.env.GITHUB_USERNAME} has been added as a collaborator to the ${process.env.REPO_NAME} repository.`
});
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed',
labels: ['access-request', 'approved']
});
} catch (error) {
console.log(error);
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `❌ Error adding collaborator: ${error.message}`
});
}
process-denial:
if: contains(github.event.comment.body, '/deny')
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: Parse issue body
id: parse
run: |
ISSUE_NUMBER=${{ github.event.issue.number }}
ISSUE_BODY=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER" | jq -r .body)
# Extract GitHub username
GITHUB_USERNAME=$(echo "$ISSUE_BODY" | grep -oP '(?<=GitHub Username: ).*')
echo "GITHUB_USERNAME=$GITHUB_USERNAME" >> $GITHUB_ENV
- name: Add denial comment
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ github.event.issue.number }}
body: |
❌ Access request denied by @${{ github.actor }}.
@${{ env.GITHUB_USERNAME }}, your request has been denied. Please contact the repository owner for more information.
- name: Update issue
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed',
labels: ['access-request', 'denied']
});