Skip to content

Commit 82ac80f

Browse files
Merge pull request #24 from GangGreenTemperTatum/ads/eng-220-feature-rigging-cicd-pipeline-example
feat: rigging cicd workflow example
2 parents 1d12095 + 7909811 commit 82ac80f

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# rigging
2+
3+
## Welcome
4+
5+
If this PR is from a non-fork, you can leave the PR description blank and let [rigging](https://github.com/dreadnode/rigging) perform some magic here.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import asyncio
2+
import base64
3+
import os
4+
import typing as t
5+
6+
from pydantic import ConfigDict, StringConstraints
7+
8+
import rigging as rg
9+
from rigging import logger
10+
from rigging.generator import GenerateParams, Generator, register_generator
11+
12+
logger.enable("rigging")
13+
14+
MAX_TOKENS = 8000
15+
TRUNCATION_WARNING = "\n\n**Note**: Due to the large size of this diff, some content has been truncated."
16+
str_strip = t.Annotated[str, StringConstraints(strip_whitespace=True)]
17+
18+
19+
class PRDiffData(rg.Model):
20+
"""XML model for PR diff data"""
21+
22+
content: str_strip = rg.element()
23+
24+
@classmethod
25+
def xml_example(cls) -> str:
26+
return """<diff><content>example diff content</content></diff>"""
27+
28+
29+
class PRDecorator(Generator):
30+
"""Generator for creating PR descriptions"""
31+
32+
model_config = ConfigDict(arbitrary_types_allowed=True, validate_assignment=True)
33+
34+
api_key: str = ""
35+
max_tokens: int = MAX_TOKENS
36+
37+
def __init__(self, model: str, params: rg.GenerateParams) -> None:
38+
api_key = params.extra.get("api_key")
39+
if not api_key:
40+
raise ValueError("api_key is required in params.extra")
41+
42+
super().__init__(model=model, params=params, api_key=api_key)
43+
self.api_key = api_key
44+
self.max_tokens = params.max_tokens or MAX_TOKENS
45+
46+
async def generate_messages(
47+
self,
48+
messages: t.Sequence[t.Sequence[rg.Message]],
49+
params: t.Sequence[GenerateParams],
50+
) -> t.Sequence[rg.GeneratedMessage]:
51+
responses = []
52+
for message_seq, p in zip(messages, params):
53+
base_generator = rg.get_generator(self.model, params=p)
54+
llm_response = await base_generator.generate_messages([message_seq], [p])
55+
responses.extend(llm_response)
56+
return responses
57+
58+
59+
register_generator("pr_decorator", PRDecorator)
60+
61+
62+
async def generate_pr_description(diff_text: str) -> str:
63+
"""Generate a PR description from the diff text"""
64+
diff_tokens = len(diff_text) // 4
65+
if diff_tokens >= MAX_TOKENS:
66+
char_limit = (MAX_TOKENS * 4) - len(TRUNCATION_WARNING)
67+
diff_text = diff_text[:char_limit] + TRUNCATION_WARNING
68+
69+
diff_data = PRDiffData(content=diff_text)
70+
params = rg.GenerateParams(
71+
extra={
72+
"api_key": os.environ["OPENAI_API_KEY"],
73+
"diff_text": diff_text,
74+
},
75+
temperature=0.7,
76+
max_tokens=500,
77+
)
78+
79+
generator = rg.get_generator("pr_decorator!gpt-4-turbo-preview", params=params)
80+
prompt = f"""You are a helpful AI that generates clear and concise PR descriptions. Analyze the provided git diff and create a summary, specifically focusing on the elements of the code that has changed
81+
including high severity functions etc using exactly this format:
82+
83+
### PR Summary
84+
85+
#### Overview of Changes
86+
<overview paragraph>
87+
88+
#### Key Modifications
89+
1. **<modification title>**: <description>
90+
(continue as needed)
91+
92+
#### Potential Impact
93+
- <impact point 1>
94+
(continue as needed)
95+
96+
Here is the PR diff to analyze:
97+
{diff_data.to_xml()}"""
98+
99+
chat = await generator.chat(prompt).run()
100+
return chat.last.content.strip()
101+
102+
103+
async def main():
104+
"""Main function for CI environment"""
105+
if not os.environ.get("OPENAI_API_KEY"):
106+
raise ValueError("OPENAI_API_KEY environment variable must be set")
107+
108+
try:
109+
diff_text = os.environ.get("GIT_DIFF", "")
110+
if not diff_text:
111+
raise ValueError("No diff found in GIT_DIFF environment variable")
112+
113+
try:
114+
diff_text = base64.b64decode(diff_text).decode("utf-8")
115+
except Exception:
116+
padding = 4 - (len(diff_text) % 4)
117+
if padding != 4:
118+
diff_text += "=" * padding
119+
diff_text = base64.b64decode(diff_text).decode("utf-8")
120+
121+
logger.debug(f"Processing diff of length: {len(diff_text)}")
122+
description = await generate_pr_description(diff_text)
123+
124+
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
125+
f.write("content<<EOF\n")
126+
f.write(description)
127+
f.write("\nEOF\n")
128+
f.write(f"debug_diff_length={len(diff_text)}\n")
129+
f.write(f"debug_description_length={len(description)}\n")
130+
debug_preview = description[:500]
131+
f.write("debug_preview<<EOF\n")
132+
f.write(debug_preview)
133+
f.write("\nEOF\n")
134+
135+
except Exception as e:
136+
logger.error(f"Error in main: {e}")
137+
raise
138+
139+
140+
if __name__ == "__main__":
141+
asyncio.run(main())
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Update PR Description with Rigging
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize]
6+
7+
jobs:
8+
update-description:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
pull-requests: write
12+
contents: read
13+
14+
steps:
15+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 #v4.2.2
16+
with:
17+
fetch-depth: 0
18+
19+
# Get the diff first
20+
- name: Get Diff
21+
id: diff
22+
run: |
23+
git fetch origin ${{ github.base_ref }}
24+
MERGE_BASE=$(git merge-base HEAD origin/${{ github.base_ref }})
25+
# Encode the diff as base64 to preserve all characters
26+
DIFF=$(git diff $MERGE_BASE..HEAD | base64 -w 0)
27+
echo "diff=$DIFF" >> $GITHUB_OUTPUT
28+
29+
- uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b #v5.0.3
30+
with:
31+
python-version: "3.11"
32+
33+
- name: Install dependencies
34+
run: |
35+
python -m pip install --upgrade pip
36+
pip cache purge
37+
pip install pydantic
38+
pip install rigging[all]
39+
40+
# Generate the description using the diff
41+
- name: Generate PR Description
42+
id: description
43+
env:
44+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
46+
PR_NUMBER: ${{ github.event.pull_request.number }}
47+
GIT_DIFF: ${{ steps.diff.outputs.diff }}
48+
run: |
49+
python .github/scripts/rigging_pr_decorator.py
50+
51+
# Update the PR description
52+
- name: Update PR Description
53+
uses: nefrob/pr-description@4dcc9f3ad5ec06b2a197c5f8f93db5e69d2fdca7 #v1.2.0
54+
with:
55+
content: |
56+
## AI-Generated Summary
57+
58+
${{ steps.description.outputs.content }}
59+
60+
---
61+
62+
This summary was generated with ❤️ by [rigging](https://rigging.dreadnode.io/)
63+
regex: ".*"
64+
regexFlags: s
65+
token: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)