Initialize

This commit is contained in:
Shrikanth Upadhayaya 2024-09-17 11:37:38 -04:00
commit b953c700bf
No known key found for this signature in database
6 changed files with 295 additions and 0 deletions

12
Dockerfile Normal file
View File

@ -0,0 +1,12 @@
FROM python:3.11-slim
RUN apt-get update && apt-get install -y \
git \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt /requirements.txt
COPY entrypoint.py /entrypoint.py
RUN pip install -r requirements.txt
ENTRYPOINT ["/entrypoint.py"]

21
LICENSE.txt Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 AllSpice
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

128
README.md Normal file
View File

@ -0,0 +1,128 @@
# Create Design Review
Create a Design Review on the current repository using the current branch as
HEAD with [AllSpice Actions](https://learn.allspice.io/docs/actions-cicd).
## Usage
Add the following step to your workflow:
```yaml
- name: Create Design Review
uses: https://hub.allspice.io/Actions/create-dr@v0.1
with:
title: "My Design Review"
auth_token: ${{ secrets.ALLSPICE_AUTH_TOKEN }}
```
To make a design review with a set of changes, you'll need to:
1. Create or switch to a branch which is not the base branch:
```sh
git switch -c new-branch
```
2. Make your changes:
```sh
echo "Hello, World!" > hello.txt
```
3. Configure your git user and email:
```sh
git config --global user.email "my-bot@myorg.com"
git config --global user.name "My Bot"
```
4. Commit and push your changes:
```sh
git add .
git commit -m "Add hello.txt"
git push origin new-branch
```
5. Add the action to your workflow:
```yaml
- name: Create Design Review
uses: https://hub.allspice.io/Actions/create-dr@v0.1
with:
title: "My Design Review"
auth_token: ${{ secrets.ALLSPICE_AUTH_TOKEN }}
```
An example job that goes through all of this is:
```yaml
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Create a new branch
run: git switch -c new-branch
- name: Make changes
run: echo "Hello, World!" > hello.txt
- name: Configure git
run: |
git config --global user.email "my-bot@myorg.com"
git config --global user.name "My Bot"
- name: Commit changes
run: |
git add .
git commit -m "Add hello.txt"
git push origin new-branch
- name: Create Design Review
uses: https://hub.allspice.io/Actions/create-dr@v0.1
with:
title: "My Design Review"
auth_token: ${{ secrets.ALLSPICE_AUTH_TOKEN }}
```
### Inputs
- `title` (required): Title of the design review.
- `description` (optional): Description of the design review.
- `base_branch_name` (optional): The name of the branch to use as the base for the DR. If not provided, the default branch is used.
- `log_level` (optional): Level of logger to use. Default is "INFO".
### Example
```yaml
- name: Create Design Review
uses: https://hub.allspice.io/Actions/create-dr@v0.1
with:
title: "New Feature Design Review"
description: "Review of the new feature implementation"
base_branch_name: "develop"
log_level: "DEBUG"
auth_token: ${{ secrets.ALLSPICE_AUTH_TOKEN }}
```
### Important Notes
1. The action uses the current branch as the HEAD for the Design Review.
2. If `base_branch_name` is not provided, the default branch of the repository
will be used as the base for the Design Review.
3. An auth token is required, and the default `${{ gitea.token }}` value does
not work.
## SSL
If your instance is running on a self-signed certificate, you can tell the
action to use your certificate by setting the `REQUESTS_CA_BUNDLE` environment
variable.
```yaml
- name: Create Design Review
uses: https://hub.allspice.io/Actions/create-dr@v0.1
with:
title: "Automated Design Review"
auth_token: ${{ secrets.ALLSPICE_AUTH_TOKEN }}
env:
REQUESTS_CA_BUNDLE: /path/to/your/certificate.cert
```
For more information about AllSpice Actions and how to use them in your
workflows, please refer to the
[AllSpice Documentation](https://learn.allspice.io/docs/actions-cicd).

41
action.yml Normal file
View File

@ -0,0 +1,41 @@
name: "Create Design Review"
description: |
Create a Design Review on the current repository using the current branch as
HEAD.
inputs:
title:
description: "Title of the design review"
required: true
auth_token:
description: "An AllSpice Hub auth token."
required: true
description:
description: "Description of the design review"
required: false
base_branch_name:
description: "The name of the branch to use as the base for the DR. If not provided, the default branch is used"
required: false
log_level:
description: "Level of logger to use, default INFO"
required: false
default: "INFO"
runs:
using: "docker"
image: "Dockerfile"
args:
- "--title"
- ${{ inputs.title }}
- "--repository"
- ${{ github.repository }}
- "--allspice-hub-url"
- ${{ github.server_url }}
- "--description"
- ${{ inputs.description }}
- "--base-branch-name"
- ${{ inputs.base_branch_name }}
- "--log-level"
- ${{ inputs.log_level }}
env:
ALLSPICE_AUTH_TOKEN: ${{ inputs.auth_token }}

92
entrypoint.py Executable file
View File

@ -0,0 +1,92 @@
#! /usr/bin/env -S python3
"""
create_design_review.py: Create a design review with the changes at HEAD.
"""
import argparse
import logging
import os
import subprocess
from allspice import AllSpice, Repository
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--title", help="Title of the design review.", required=True)
parser.add_argument(
"--description",
help="Description of the design review.",
required=False,
)
parser.add_argument(
"--repository",
help="The repository to create the design review for.",
required=True,
)
parser.add_argument(
"--allspice-hub-url",
help="The URL of the AllSpice hub.",
required=True,
)
parser.add_argument(
"--base-branch-name",
help="The name of the branch to use as the base for the DR. If not provided, the default branch is used.",
required=False,
)
parser.add_argument(
"--log-level",
required=False,
default="INFO",
help="Level of logger to use, default INFO.",
)
args = parser.parse_args()
logger.setLevel(args.log_level.upper())
logger.info("Creating design review, with args %s", vars(args))
token_text = os.getenv("ALLSPICE_AUTH_TOKEN")
if not token_text:
raise ValueError("ALLSPICE_AUTH_TOKEN environment variable not set.")
client = AllSpice(
allspice_hub_url=args.allspice_hub_url,
token_text=token_text,
log_level=args.log_level.upper(),
)
owner_name, repo_name = args.repository.split("/")
repository = Repository.request(client, owner_name, repo_name)
current_branch = (
subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"])
.decode()
.strip()
)
base_branch = args.base_branch_name or repository.default_branch
logger.info(
"Creating design review at branch %s with base branch %s",
current_branch,
base_branch,
)
if base_branch == current_branch:
raise ValueError("Base branch cannot be the same as the current branch.")
repository.create_design_review(
title=args.title,
head=current_branch,
base=base_branch,
body=args.description,
)
logger.info("Design review created successfully.")
if __name__ == "__main__":
main()

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
py-allspice~=3.5