|
1 | | -# generate-docs |
2 | | -TODO |
| 1 | +## generate-docs |
| 2 | +Update and maintain the documentation for your project with ease. |
| 3 | + |
| 4 | +Given an API file written in a supported language (eg. Python), this action will generate and maintain an OpenAPI spec file (in YAML format) |
| 5 | +for that file, then open a pull request with the changes and an explanation. |
| 6 | + |
| 7 | +## Requirements |
| 8 | +The following Github Secrets must be accessible to the action: |
| 9 | +** RUNLLM_API_KEY: The API key for your Runllm account. |
| 10 | +** GITHUB_TOKEN: The Github token for the repository. |
| 11 | + |
| 12 | +## Usage |
| 13 | +This action is expected to be used only with push-based workflows. It is recommended to use this action by copy-pasting the following github workflow: |
| 14 | + |
| 15 | +```yaml |
| 16 | +name: Maintain OpenAPI Spec |
| 17 | + |
| 18 | +on: |
| 19 | + push: |
| 20 | + branches: main |
| 21 | + path: |
| 22 | + - '<path-to-your-api-file>' |
| 23 | + |
| 24 | +permissions: |
| 25 | + contents: write |
| 26 | + pull-requests: write |
| 27 | + |
| 28 | +jobs: |
| 29 | + update-docs: |
| 30 | + runs-on: ubuntu-latest |
| 31 | + steps: |
| 32 | + - name: Generate OpenAPI Documentation |
| 33 | + uses: runllm/generate-docs@v1 |
| 34 | + with: |
| 35 | + runllm_api_key: ${{ secrets.RUNLLM_API_KEY }} |
| 36 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + input_api_file: "<path-to-your-api-file>" |
| 38 | + output_spec_file: "<path-to-your-output-spec-file>" |
| 39 | + base_branch: "main" # Optional |
| 40 | +``` |
| 41 | +
|
| 42 | +## Example |
| 43 | +
|
| 44 | +Imagine we have added the following Github Action workflow: |
| 45 | +
|
| 46 | +```yaml |
| 47 | +name: Maintain OpenAPI Spec |
| 48 | + |
| 49 | +on: |
| 50 | + push: |
| 51 | + branches: main |
| 52 | + path: |
| 53 | + - 'src/books_server.py' |
| 54 | + |
| 55 | +permissions: |
| 56 | + contents: write |
| 57 | + pull-requests: write |
| 58 | + |
| 59 | +jobs: |
| 60 | + update-docs: |
| 61 | + runs-on: ubuntu-latest |
| 62 | + steps: |
| 63 | + - name: Generate OpenAPI Documentation |
| 64 | + uses: runllm/generate-docs@v1 |
| 65 | + with: |
| 66 | + runllm_api_key: ${{ secrets.RUNLLM_API_KEY }} |
| 67 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + input_api_file: "src/books_server.py" |
| 69 | + output_spec_file: "docs/openapi.yml" |
| 70 | +``` |
| 71 | +
|
| 72 | +Now, let's say we create the following file at `src/books_server.py` and merge to main: |
| 73 | + |
| 74 | + |
| 75 | +```python |
| 76 | +from flask import Flask, jsonify, request, abort |
| 77 | +
|
| 78 | +app = Flask(__name__) |
| 79 | +
|
| 80 | +# In-memory database simulation |
| 81 | +books = [] |
| 82 | +
|
| 83 | +@app.route('/books', methods=['GET']) |
| 84 | +def get_books(): |
| 85 | + return jsonify(books) |
| 86 | +
|
| 87 | +@app.route('/books', methods=['POST']) |
| 88 | +def add_book(): |
| 89 | + if not request.json or 'title' not in request.json or 'author' not in request.json: |
| 90 | + abort(400) |
| 91 | + book = { |
| 92 | + 'id': len(books) + 1, |
| 93 | + 'title': request.json['title'], |
| 94 | + 'author': request.json['author'], |
| 95 | + 'isbn': request.json.get('isbn', ""), |
| 96 | + 'publishedYear': request.json.get('publishedYear', None) |
| 97 | + } |
| 98 | + books.append(book) |
| 99 | + return jsonify(book), 201 |
| 100 | +
|
| 101 | +@app.route('/books/<int:book_id>', methods=['GET']) |
| 102 | +def get_book(book_id): |
| 103 | + book = next((book for book in books if book['id'] == book_id), None) |
| 104 | + if book is None: |
| 105 | + abort(404) |
| 106 | + return jsonify(book) |
| 107 | +
|
| 108 | +@app.route('/books/<int:book_id>', methods=['PUT']) |
| 109 | +def update_book(book_id): |
| 110 | + book = next((book for book in books if book['id'] == book_id), None) |
| 111 | + if book is None: |
| 112 | + abort(404) |
| 113 | + if not request.json: |
| 114 | + abort(400) |
| 115 | +
|
| 116 | + book['title'] = request.json.get('title', book['title']) |
| 117 | + book['author'] = request.json.get('author', book['author']) |
| 118 | + book['isbn'] = request.json.get('isbn', book['isbn']) |
| 119 | + book['publishedYear'] = request.json.get('publishedYear', book['publishedYear']) |
| 120 | + return jsonify(book) |
| 121 | +
|
| 122 | +@app.route('/books/<int:book_id>', methods=['DELETE']) |
| 123 | +def delete_book(book_id): |
| 124 | + global books |
| 125 | + books = [book for book in books if book['id'] != book_id] |
| 126 | + return jsonify({'result': True}) |
| 127 | +
|
| 128 | +if __name__ == '__main__': |
| 129 | + app.run(debug=True) |
| 130 | +``` |
| 131 | + |
| 132 | +The action will automatically generate a pull request adding the following OpenAPI spec file at `docs/openapi.yml`: |
| 133 | + |
| 134 | +```yaml |
| 135 | +openapi: 3.0.0 |
| 136 | +info: |
| 137 | + title: Book Management API |
| 138 | + version: 1.0.0 |
| 139 | + description: This API allows you to manage a collection of books. You can add, retrieve, update, and delete books. |
| 140 | +
|
| 141 | +paths: |
| 142 | + /books: |
| 143 | + get: |
| 144 | + summary: Retrieve a list of all books |
| 145 | + description: This endpoint returns a list of all books in the collection. Each book is represented with its unique ID, title, author, ISBN, and the year it was published. |
| 146 | + responses: |
| 147 | + '200': |
| 148 | + description: A list of books |
| 149 | + content: |
| 150 | + application/json: |
| 151 | + schema: |
| 152 | + type: array |
| 153 | + items: |
| 154 | + $ref: '#/components/schemas/Book' |
| 155 | + post: |
| 156 | + summary: Add a new book |
| 157 | + description: This endpoint allows you to add a new book to the collection. The title and author are required, while the ISBN and published year are optional. |
| 158 | + requestBody: |
| 159 | + required: true |
| 160 | + content: |
| 161 | + application/json: |
| 162 | + schema: |
| 163 | + $ref: '#/components/schemas/Book' |
| 164 | + responses: |
| 165 | + '201': |
| 166 | + description: The book was successfully created |
| 167 | + content: |
| 168 | + application/json: |
| 169 | + schema: |
| 170 | + $ref: '#/components/schemas/Book' |
| 171 | + '400': |
| 172 | + description: The request was invalid. Either the title or author was not provided. |
| 173 | + ... |
| 174 | +``` |
| 175 | + |
| 176 | +Future edits to `src/book_server.py` will continously update the OpenAPI spec file and open corresponding pull requests with any updates. |
| 177 | +Never let your documentation get out of date again! |
0 commit comments