-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
243 lines (215 loc) · 8.13 KB
/
action.yml
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# action.yml
name: 'Auto PR: dev to main/master'
description: 'Automates PR creation from dev to main/master, generates descriptions with OpenAI, and adds reviewers.'
author: 'Yuri V'
branding:
icon: 'git-pull-request'
color: 'green'
inputs:
openai_api_key:
description: 'OpenAI API Key'
required: true
openai_model:
description: 'OpenAI model to use (e.g., gpt-4, gpt-3.5-turbo, etc.) https://platform.openai.com/docs/models'
required: false
default: 'gpt-4o-mini'
github_token:
description: 'GitHub (PAT) token with repo permissions'
required: true
dev_branch:
description: 'Development branch name'
required: false
default: 'dev'
outputs:
pr_number:
description: 'The number of the pull request created or updated'
runs:
using: 'composite'
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ inputs.dev_branch }}
- name: Set up Git
shell: bash
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Get the default branch
id: get_default_branch
shell: bash
run: |
default_branch=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}')
echo "branch=${default_branch}" >> $GITHUB_OUTPUT
- name: Get the diff using custom script
id: get_diff
shell: bash
run: |
set -e
branch_from="${{ steps.get_default_branch.outputs.branch }}"
branch_to="${{ inputs.dev_branch }}"
echo "Branch from: $branch_from"
echo "Branch to: $branch_to"
git fetch --all
mkdir -p diff
output_file="diff/${branch_from:-main}-${branch_to}_$(date +'%Y%m%d_%H%M').diff"
echo "Output file: $output_file"
{
echo "Commits between $branch_from and $branch_to:"
echo "============================================"
git log origin/"$branch_from"..origin/"$branch_to" || echo "No commits found"
echo "--------------------------------------------"
echo "Git diff between $branch_from and $branch_to"
echo "============================================"
git diff origin/"$branch_from" origin/"$branch_to" || echo "No differences found"
} > "$output_file"
if [ -s "$output_file" ]; then
echo "diff_output<<EOF" >> $GITHUB_OUTPUT
cat "$output_file" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
else
echo "diff_output=No changes detected between $branch_from and $branch_to" >> $GITHUB_OUTPUT
fi
- name: Generate release description using OpenAI API
id: generate_release_notes
env:
OPENAI_API_KEY: ${{ inputs.openai_api_key }}
OPENAI_MODEL: ${{ inputs.openai_model }}
DIFF_OUTPUT: ${{ steps.get_diff.outputs.diff_output }}
shell: bash
run: |
prompt="**Instructions:**\n\nPlease generate a **Pull Request description** for the provided diff, following these guidelines:\n- Add appropriate emojis to the description.\n- Do **not** include the words \"Title\" and \"Description\" in your output.\n- Format your answer in **Markdown**."
escaped_prompt=$(echo "$prompt" | jq -Rs .)
escaped_diff=$(echo "$DIFF_OUTPUT" | jq -Rs .)
response=$(curl -s -X POST https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "{
\"model\": \"${OPENAI_MODEL}\",
\"messages\": [
{
\"role\": \"system\",
\"content\": ${escaped_prompt}
},
{
\"role\": \"user\",
\"content\": ${escaped_diff}
}
],
\"temperature\": 1,
\"max_tokens\": 2048,
\"top_p\": 1,
\"frequency_penalty\": 0,
\"presence_penalty\": 0
}")
# Check for errors in the response
error_message=$(echo "$response" | jq -r '.error.message // empty')
if [ -n "$error_message" ]; then
echo "Error from OpenAI API: $error_message"
exit 1
fi
description=$(echo "$response" | jq -r '.choices[0].message.content')
echo "description<<EOF" >> $GITHUB_OUTPUT
echo "$description" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Set Release Title
id: set_release_title
shell: bash
run: |
echo "title=Release v$(date +'%Y.%m.%d')" >> $GITHUB_OUTPUT
- name: Create or Update Pull Request and Add Reviewers
id: create_pr
uses: actions/github-script@v6
env:
PR_TITLE: ${{ steps.set_release_title.outputs.title }}
PR_BODY: ${{ toJSON(steps.generate_release_notes.outputs.description) }}
with:
github-token: ${{ inputs.github_token }}
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const head = '${{ inputs.dev_branch }}';
const base = '${{ steps.get_default_branch.outputs.branch }}';
const title = process.env.PR_TITLE;
const body = JSON.parse(process.env.PR_BODY);
// Check if there are any commits between base and head
const { data: comparison } = await github.rest.repos.compareCommits({
owner,
repo,
base,
head,
});
if (comparison.commits.length === 0) {
console.log('No commits between ' + base + ' and ' + head + '. Skipping PR creation.');
core.setOutput('pr_number', 'none');
return;
}
// Check if PR from dev to default branch already exists
const { data: pulls } = await github.rest.pulls.list({
owner: owner,
repo: repo,
head: `${owner}:${head}`,
base: base,
state: 'open',
});
let prNumber;
let prAuthor;
if (pulls.length > 0) {
prNumber = pulls[0].number;
prAuthor = pulls[0].user.login;
console.log(`Updating existing PR #${prNumber}`);
await github.rest.pulls.update({
owner: owner,
repo: repo,
pull_number: prNumber,
title: title,
body: body,
});
} else {
console.log('Creating new PR');
const { data: pr } = await github.rest.pulls.create({
owner: owner,
repo: repo,
head: head,
base: base,
title: title,
body: body,
});
prNumber = pr.number;
prAuthor = pr.user.login;
}
// Now, add reviewers
// First, get the list of committers to this PR
const { data: commits } = await github.rest.pulls.listCommits({
owner: owner,
repo: repo,
pull_number: prNumber,
});
const committersSet = new Set();
for (const commit of commits) {
if (commit.author && commit.author.login) {
committersSet.add(commit.author.login);
}
}
// Add the repo owner
committersSet.add(owner);
// Remove the PR author from reviewers
committersSet.delete(prAuthor);
// Remove the bot user (if present)
const botUser = 'github-actions[bot]';
committersSet.delete(botUser);
// Convert set to array
const reviewers = Array.from(committersSet);
console.log(`Adding reviewers: ${reviewers.join(', ')}`);
if (reviewers.length > 0) {
// Add reviewers to the PR
await github.rest.pulls.requestReviewers({
owner: owner,
repo: repo,
pull_number: prNumber,
reviewers: reviewers,
});
} else {
console.log('No reviewers to add.');
}
core.setOutput('pr_number', prNumber);