Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a custom task for downloading the API #533

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions .github/workflows/openai-api-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
name: Check OpenAI API

on:
workflow_dispatch:

jobs:
checkAPIVersion:
runs-on: ubuntu-latest
steps:
- name: Install Octokit Action
run: npm install @octokit/action
- name: Get latest API commit
id: get-latest-commit
uses: actions/github-script@v6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
result-encoding: string
script: '
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();
const owner = ''openai'';
const repo = ''openai-openapi'';
const filePath = ''openapi.yaml'';
const branch = ''master'';
const commits = await octokit.repos.listCommits({
owner,
repo,
path: filePath,
ref: branch
});
return commits.data[0].sha.trim();
'
- name: Get result latest API commit
run: |
LATEST_API_COMMIT="${{steps.get-latest-commit.outputs.result}}"
echo "LATEST_API_COMMIT=$LATEST_API_COMMIT" >> $GITHUB_ENV
- name: Get current API commit
id: get-current-commit
uses: actions/github-script@v6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
result-encoding: string
script: '
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();
const owner = ''xebia-functional'';
const repo = ''xef'';
const filePath = ''openai-client/generator/config/openai-api-commit'';
const branch = ''main'';
const response = await octokit.repos.getContent({
owner,
repo,
path: filePath,
ref: branch,
});
const content = Buffer.from(response.data.content, ''base64'').toString(''utf-8'');
return content.trim();
'
- name: Get result current API commit
run: |
CURRENT_API_COMMIT="${{steps.get-current-commit.outputs.result}}"
echo "CURRENT_API_COMMIT=$CURRENT_API_COMMIT" >> $GITHUB_ENV
- name: Check existing PR
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
id: get-pr-commit
uses: actions/github-script@v6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
result-encoding: string
script: '
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();
const owner = ''xebia-functional'';
const repo = ''xef'';
const filePath = ''openai-client/generator/config/openai-api-commit'';
const existing = await octokit.pulls.list({
owner,
repo,
head: ''xebia-functional:update/openai-client''
});

if (existing.data.length > 0) {
const prCommit = existing.data[0].head.sha;
const response = await octokit.repos.getContent({
owner,
repo,
path: filePath,
ref: prCommit
});
const prApiCommit = Buffer.from(response.data.content, ''base64'').toString(''utf-8'').trim();

if (prApiCommit === ''${{ env.LATEST_API_COMMIT }}'') {
return prApiCommit;
} else {
return '''';
}

} else {
return '''';
}
'
- name: Update current commit with PR
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
run: |
PR_API_COMMIT="${{steps.get-pr-commit.outputs.result}}"
if [[ ! -z "$PR_API_COMMIT" ]]
then
echo "CURRENT_API_COMMIT=$PR_API_COMMIT" >> $GITHUB_ENV
else
echo "Previous PR outdated or nonexistent"
fi
- name: Checkout
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
uses: actions/checkout@v4
- name: Update commit
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
run: |
echo $LATEST_API_COMMIT > $GITHUB_WORKSPACE/generator/config/openai-api-commit
- name: Set up Java
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 20
- name: Download new API spec
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
uses: gradle/gradle-build-action@v2
with:
arguments: downloadOpenAIAPI
- name: Generate new OpenAI client
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
uses: gradle/gradle-build-action@v2
with:
arguments: openaiClientGenerate
- name: Spotless Apply
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
uses: gradle/gradle-build-action@v2
with:
arguments: spotlessApply
- name: Create PR
if: env.CURRENT_API_COMMIT != env.LATEST_API_COMMIT
uses: peter-evans/create-pull-request@v5
with:
commit-message: 'Update OpenAI Client'
branch: update/openai-client
title: 'Update OpenAI client'
body: 'Updates the OpenAI client based on the latest changes.'
6 changes: 6 additions & 0 deletions openai-client/generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ tasks.test {
useJUnitPlatform()
}

task("downloadOpenAIAPI", JavaExec::class) {
group = "GenerateTasks"
mainClass = "ai.xef.openai.generator.DownloadOpenAIAPI"
classpath = sourceSets["main"].runtimeClasspath
}

task("openaiClientGenerate", JavaExec::class) {
group = "GenerateTasks"
mainClass = "org.openapitools.codegen.OpenAPIGenerator"
Expand Down
1 change: 1 addition & 0 deletions openai-client/generator/config/openai-api-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5c1857ea865e74e45e3b12064e0cc2396ef64be1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ai.xef.openai.generator;

import java.io.*;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

public class DownloadOpenAIAPI {
public static void main(String[] args) {
try {
String commit = readCommit();
downloadAPI(commit);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static String readCommit() throws IOException {
StringBuilder resultStringBuilder = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader("config/openai-api-commit"))) {
String line;
while ((line = br.readLine()) != null) {
resultStringBuilder.append(line).append("\n");
}
}
return resultStringBuilder.toString().trim();
}

private static void downloadAPI(String commit) throws IOException {
URL url = new URL("https://raw.githubusercontent.com/openai/openai-openapi/%s/openapi.yaml".formatted(commit));
ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
try (FileOutputStream fileOutputStream = new FileOutputStream("config/openai-api.yaml")) {
FileChannel fileChannel = fileOutputStream.getChannel();
fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
}
}
}