Skip to content

Commit 2f57459

Browse files
Adds a custom task for downloading the API (#533)
* Adds a custom task for downloading the API * GitHub action for checking the OpenAI API
1 parent 7222268 commit 2f57459

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed
+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
name: Check OpenAI API
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
checkAPIVersion:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Install Octokit Action
11+
run: npm install @octokit/action
12+
- name: Get latest API commit
13+
id: get-latest-commit
14+
uses: actions/github-script@v6
15+
env:
16+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17+
with:
18+
result-encoding: string
19+
script: '
20+
const { Octokit } = require("@octokit/action");
21+
const octokit = new Octokit();
22+
const owner = ''openai'';
23+
const repo = ''openai-openapi'';
24+
const filePath = ''openapi.yaml'';
25+
const branch = ''master'';
26+
const commits = await octokit.repos.listCommits({
27+
owner,
28+
repo,
29+
path: filePath,
30+
ref: branch
31+
});
32+
return commits.data[0].sha.trim();
33+
'
34+
- name: Get result latest API commit
35+
run: |
36+
LATEST_API_COMMIT="${{steps.get-latest-commit.outputs.result}}"
37+
echo "LATEST_API_COMMIT=$LATEST_API_COMMIT" >> $GITHUB_ENV
38+
- name: Get current API commit
39+
id: get-current-commit
40+
uses: actions/github-script@v6
41+
env:
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
with:
44+
result-encoding: string
45+
script: '
46+
const { Octokit } = require("@octokit/action");
47+
const octokit = new Octokit();
48+
const owner = ''xebia-functional'';
49+
const repo = ''xef'';
50+
const filePath = ''openai-client/generator/config/openai-api-commit'';
51+
const branch = ''main'';
52+
const response = await octokit.repos.getContent({
53+
owner,
54+
repo,
55+
path: filePath,
56+
ref: branch,
57+
});
58+
const content = Buffer.from(response.data.content, ''base64'').toString(''utf-8'');
59+
return content.trim();
60+
'
61+
- name: Get result current API commit
62+
run: |
63+
CURRENT_API_COMMIT="${{steps.get-current-commit.outputs.result}}"
64+
echo "CURRENT_API_COMMIT=$CURRENT_API_COMMIT" >> $GITHUB_ENV
65+
- name: Check existing PR
66+
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
67+
id: get-pr-commit
68+
uses: actions/github-script@v6
69+
env:
70+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
71+
with:
72+
result-encoding: string
73+
script: '
74+
const { Octokit } = require("@octokit/action");
75+
const octokit = new Octokit();
76+
const owner = ''xebia-functional'';
77+
const repo = ''xef'';
78+
const filePath = ''openai-client/generator/config/openai-api-commit'';
79+
const existing = await octokit.pulls.list({
80+
owner,
81+
repo,
82+
head: ''xebia-functional:update/openai-client''
83+
});
84+
85+
if (existing.data.length > 0) {
86+
const prCommit = existing.data[0].head.sha;
87+
const response = await octokit.repos.getContent({
88+
owner,
89+
repo,
90+
path: filePath,
91+
ref: prCommit
92+
});
93+
const prApiCommit = Buffer.from(response.data.content, ''base64'').toString(''utf-8'').trim();
94+
95+
if (prApiCommit === ''${{ env.LATEST_API_COMMIT }}'') {
96+
return prApiCommit;
97+
} else {
98+
return '''';
99+
}
100+
101+
} else {
102+
return '''';
103+
}
104+
'
105+
- name: Update current commit with PR
106+
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
107+
run: |
108+
PR_API_COMMIT="${{steps.get-pr-commit.outputs.result}}"
109+
if [[ ! -z "$PR_API_COMMIT" ]]
110+
then
111+
echo "CURRENT_API_COMMIT=$PR_API_COMMIT" >> $GITHUB_ENV
112+
else
113+
echo "Previous PR outdated or nonexistent"
114+
fi
115+
- name: Checkout
116+
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
117+
uses: actions/checkout@v4
118+
- name: Update commit
119+
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
120+
run: |
121+
echo $LATEST_API_COMMIT > $GITHUB_WORKSPACE/generator/config/openai-api-commit
122+
- name: Set up Java
123+
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
124+
uses: actions/setup-java@v3
125+
with:
126+
distribution: 'zulu'
127+
java-version: 20
128+
- name: Download new API spec
129+
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
130+
uses: gradle/gradle-build-action@v2
131+
with:
132+
arguments: downloadOpenAIAPI
133+
- name: Generate new OpenAI client
134+
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
135+
uses: gradle/gradle-build-action@v2
136+
with:
137+
arguments: openaiClientGenerate
138+
- name: Spotless Apply
139+
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
140+
uses: gradle/gradle-build-action@v2
141+
with:
142+
arguments: spotlessApply
143+
- name: Create PR
144+
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
145+
uses: peter-evans/create-pull-request@v5
146+
with:
147+
commit-message: 'Update OpenAI Client'
148+
branch: update/openai-client
149+
title: 'Update OpenAI client'
150+
body: 'Updates the OpenAI client based on the latest changes.'

openai-client/generator/build.gradle.kts

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ tasks.test {
1313
useJUnitPlatform()
1414
}
1515

16+
task("downloadOpenAIAPI", JavaExec::class) {
17+
group = "GenerateTasks"
18+
mainClass = "ai.xef.openai.generator.DownloadOpenAIAPI"
19+
classpath = sourceSets["main"].runtimeClasspath
20+
}
21+
1622
task("openaiClientGenerate", JavaExec::class) {
1723
group = "GenerateTasks"
1824
mainClass = "org.openapitools.codegen.OpenAPIGenerator"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5c1857ea865e74e45e3b12064e0cc2396ef64be1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ai.xef.openai.generator;
2+
3+
import java.io.*;
4+
import java.net.URL;
5+
import java.nio.channels.Channels;
6+
import java.nio.channels.FileChannel;
7+
import java.nio.channels.ReadableByteChannel;
8+
9+
public class DownloadOpenAIAPI {
10+
public static void main(String[] args) {
11+
try {
12+
String commit = readCommit();
13+
downloadAPI(commit);
14+
} catch (IOException e) {
15+
throw new RuntimeException(e);
16+
}
17+
}
18+
19+
private static String readCommit() throws IOException {
20+
StringBuilder resultStringBuilder = new StringBuilder();
21+
try (BufferedReader br = new BufferedReader(new FileReader("config/openai-api-commit"))) {
22+
String line;
23+
while ((line = br.readLine()) != null) {
24+
resultStringBuilder.append(line).append("\n");
25+
}
26+
}
27+
return resultStringBuilder.toString().trim();
28+
}
29+
30+
private static void downloadAPI(String commit) throws IOException {
31+
URL url = new URL("https://raw.githubusercontent.com/openai/openai-openapi/%s/openapi.yaml".formatted(commit));
32+
ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
33+
try (FileOutputStream fileOutputStream = new FileOutputStream("config/openai-api.yaml")) {
34+
FileChannel fileChannel = fileOutputStream.getChannel();
35+
fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)