Skip to content

Commit e2ec666

Browse files
Merge branch 'main' into develop
2 parents 197e866 + f59f273 commit e2ec666

4 files changed

Lines changed: 121 additions & 40 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
(async () => {
7+
const [, , diffFile] = process.argv;
8+
if (!diffFile) {
9+
console.error('Usage: generate_pr_description.js <diff_file>');
10+
process.exit(1);
11+
}
12+
13+
if (!fs.existsSync(diffFile)) {
14+
console.error(`Error: Diff file not found at ${diffFile}`);
15+
process.exit(1);
16+
}
17+
18+
const apiKey = process.env.GEMINI_API_KEY;
19+
if (!apiKey) {
20+
console.error('Error: GEMINI_API_KEY environment variable is required');
21+
process.exit(1);
22+
}
23+
24+
// Create prompt for PR description generation
25+
const promptTemplate = `You are an expert programmer who is tasked with writing a pull request description.
26+
You will be given the git diff of the changes and you must write a markdown pull request description.
27+
Do not include the git diff in the pull request description. Do not include any other text in the pull request description.
28+
The pull request description should follow the following format:
29+
30+
## Description
31+
A short description of the changes.
32+
33+
## Changes
34+
- [ ] Change 1
35+
- [ ] Change 2
36+
37+
## Verification
38+
- [ ] Verification step 1
39+
- [ ] Verification step 2
40+
`;
41+
42+
const diffContent = fs.readFileSync(diffFile, 'utf8');
43+
const combinedPrompt = `${promptTemplate}\n\nHere is the git diff:\n\n${diffContent}`;
44+
45+
try {
46+
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${apiKey}`, {
47+
method: 'POST',
48+
headers: { 'Content-Type': 'application/json' },
49+
body: JSON.stringify({
50+
contents: [{
51+
parts: [{
52+
text: combinedPrompt
53+
}]
54+
}],
55+
generationConfig: {
56+
temperature: 0.7,
57+
topK: 40,
58+
topP: 0.95,
59+
maxOutputTokens: 2048,
60+
}
61+
})
62+
});
63+
64+
if (!response.ok) {
65+
const errorText = await response.text();
66+
console.error(`Error: Gemini API request failed with status ${response.status}`);
67+
console.error(`Response: ${errorText}`);
68+
process.exit(1);
69+
}
70+
71+
const json = await response.json();
72+
73+
if (!json.candidates || !json.candidates[0] || !json.candidates[0].content) {
74+
console.error('Error: Invalid response from Gemini API');
75+
console.error(JSON.stringify(json, null, 2));
76+
process.exit(1);
77+
}
78+
79+
const result = json.candidates[0].content.parts[0].text;
80+
process.stdout.write(result);
81+
} catch (error) {
82+
console.error(`Error: Failed to generate pull request description: ${error.message}`);
83+
process.exit(1);
84+
}
85+
})();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "auto-pr-description",
3+
"version": "1.0.0",
4+
"description": "GitHub Action to automatically generate PR descriptions using AI",
5+
"main": "generate_pr_description.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"github-action",
11+
"pull-request",
12+
"ai",
13+
"gemini",
14+
"automation"
15+
],
16+
"author": "RedShelf",
17+
"license": "MIT",
18+
"engines": {
19+
"node": ">=18.0.0"
20+
}
21+
}

.github/actions/validate-pr/validate_pr.sh

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ function compare_versions() {
6464
}
6565

6666

67-
SEMANTIC_PREFIXES="^(feat|fix|chore|docs|style|refactor|perf|test)[(:]"
68-
JIRA_TICKET="([A-Z]+-[0-9]+)"
67+
SEMANTIC_PREFIXES="^(feat|fix|chore|ci|docs|style|refactor|perf|test)[(:]"
6968
VERSION_REGEX="v([0-9]+)\.([0-9]+)\.([0-9]+)"
7069

7170
if [ "$PR_BRANCH" == "main" ] && [ "$TARGET_BRANCH" == "develop" ]; then
@@ -83,20 +82,13 @@ if [[ "$TARGET_BRANCH" == "develop" ]] || [[ "$TARGET_BRANCH" =~ ^release/v ]] |
8382
Details:
8483
- Current title: $PR_TITLE
8584
Action: Update the PR title to start with a valid semantic prefix
86-
Valid prefixes: feat:, fix:, chore:, docs:, style:, refactor:, perf:, test:
87-
Example: feat: Add new feature (ABC-123)
85+
Valid prefixes: feat:, fix:, chore:, ci:, docs:, style:, refactor:, perf:, test:
86+
Examples:
87+
feat(ABC-123): Add new feature
88+
docs: correct misspelling in readme
8889
Note: You must push a new commit to update this validation result"
8990
exit 1
9091
fi
91-
92-
if [[ ! "$PR_TITLE" =~ $JIRA_TICKET ]]; then
93-
echo "Error: Missing Jira ticket reference in PR title
94-
Details:
95-
- Current title: $PR_TITLE
96-
Action: Include a Jira ticket ID in the PR title using the format (ABC-123)
97-
Example: feat: Add new feature (ABC-123)"
98-
exit 1
99-
fi
10092
fi
10193

10294
if [[ "$TARGET_BRANCH" == "main" ]]; then

.github/actions/validate-pr/validate_pr_test.sh

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,32 @@ expect "$ACTUAL" "Error: Invalid PR title format
4141
Details:
4242
- Current title: This is a PR title (ISSUE-1234)
4343
Action: Update the PR title to start with a valid semantic prefix
44-
Valid prefixes: feat:, fix:, chore:, docs:, style:, refactor:, perf:, test:
45-
Example: feat: Add new feature (ABC-123)
44+
Valid prefixes: feat:, fix:, chore:, ci:, docs:, style:, refactor:, perf:, test:
45+
Examples:
46+
feat(ABC-123): Add new feature
47+
docs: correct misspelling in readme
4648
Note: You must push a new commit to update this validation result"
4749

48-
echo Scenario: PR title missing Jira ticket for feature branch
49-
beforeEach
50-
51-
# GIVEN
52-
export PR_TITLE="feat: This is a PR title"
5350

54-
# WHEN
55-
ACTUAL="$($SCRIPT_DIR/validate_pr.sh)"
56-
57-
# THEN
58-
expect "$?" "1"
59-
expect "$ACTUAL" "Error: Missing Jira ticket reference in PR title
60-
Details:
61-
- Current title: feat: This is a PR title
62-
Action: Include a Jira ticket ID in the PR title using the format (ABC-123)
63-
Example: feat: Add new feature (ABC-123)"
6451

65-
echo Scenario: PR title missing Jira ticket for feature branch targeting hotfix branch
52+
echo Scenario: Valid PR title for feature branch
6653
beforeEach
6754

6855
# GIVEN
69-
export PR_TITLE="feat: This is a PR title"
56+
export PR_TITLE="feat: This is a PR title (ISSUE-1234)"
7057

7158
# WHEN
7259
ACTUAL="$($SCRIPT_DIR/validate_pr.sh)"
7360

7461
# THEN
75-
expect "$?" "1"
76-
expect "$ACTUAL" "Error: Missing Jira ticket reference in PR title
77-
Details:
78-
- Current title: feat: This is a PR title
79-
Action: Include a Jira ticket ID in the PR title using the format (ABC-123)
80-
Example: feat: Add new feature (ABC-123)"
62+
expect "$?" "0"
63+
expect "PR title and branch name validation passed." "$ACTUAL"
8164

82-
echo Scenario: Valid PR title for feature branch
65+
echo Scenario: Valid PR title without Jira ticket for feature branch
8366
beforeEach
8467

8568
# GIVEN
86-
export PR_TITLE="feat: This is a PR title (ISSUE-1234)"
69+
export PR_TITLE="feat: This is a PR title without ticket"
8770

8871
# WHEN
8972
ACTUAL="$($SCRIPT_DIR/validate_pr.sh)"

0 commit comments

Comments
 (0)