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

Report suggestions to repository #3110

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
23 changes: 23 additions & 0 deletions website/migrations/0170_suggestion_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.3 on 2024-12-22 12:08

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("website", "0169_dailystatusreport_current_mood_and_more"),
]

operations = [
migrations.AddField(
model_name="suggestion",
name="company",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="website.company",
),
),
]
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Generated by Django 5.1.3 on 2024-12-19 13:05
# Generated by Django 5.1.3 on 2024-12-22 14:03

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("website", "0170_suggestion_company"),
("website", "0173_pranalysisreport"),
("website", "0173_remove_company_admin_remove_company_integrations_and_more"),
]
Expand Down
1 change: 1 addition & 0 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ class Suggestion(models.Model):
up_votes = models.IntegerField(null=True, blank=True, default=0)
down_votes = models.IntegerField(null=True, blank=True, default=0)
created = models.DateTimeField(auto_now_add=True)
organization = models.ForeignKey(Organization, null=True, blank=True, on_delete=models.CASCADE)

def __str__(self):
return f"{self.title} by {self.user}"
Expand Down
10 changes: 10 additions & 0 deletions website/templates/feature_suggestion.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ <h1>
<p>
<strong>User:</strong>{{ suggestion.user }}
</p>
{% if suggestion.company %}
<p>
<strong>Company:</strong>{{ suggestion.company.name }}
</p>
{% endif %}
<p hidden id="suggestion-id---{{ suggestion.suggestion_id }}">{{ suggestion.suggestion_id }}</p>
<div class="suggestion-vote">
<button type="button"
Expand All @@ -152,6 +157,11 @@ <h2 id="semiheading">Suggest us Features</h2>
name="description"
rows="4"
placeholder="Description of your suggestion"></textarea>
<label>Company</label>
<select id="company" name="company">
<option value="" selected>-- None --</option>
{% for company in companies %}<option value="{{ company.id }}">{{ company.name }}</option>{% endfor %}
</select>
<br>
<button type="submit" onclick="Savefeature()">Post Suggestion</button>
</form>
Expand Down
65 changes: 62 additions & 3 deletions website/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Badge,
Domain,
Issue,
Organization,
PRAnalysisReport,
Suggestion,
SuggestionVotes,
Expand Down Expand Up @@ -521,22 +522,77 @@ def set_vote_status(request):
return JsonResponse({"success": False, "error": "Invalid request method"}, status=400)


import json
import os

import requests
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from django.shortcuts import render
from giturlparse import parse


@login_required
def add_suggestions(request):
if request.method == "POST":
user = request.user
data = json.loads(request.body)
title = data.get("title")
description = data.get("description", "")
organization_id = data.get("organization")
if title and description and user:
suggestion = Suggestion(user=user, title=title, description=description)
if organization_id:
try:
organization = Organization.objects.get(id=organization_id)
except Organization.DoesNotExist:
organization = None
else:
organization = None
suggestion = Suggestion(
user=user, title=title, description=description, organization=organization
)
suggestion.save()
messages.success(request, "Suggestion added successfully.")

# GitHub issue creation
github_repo_url = os.environ.get("GITHUB_REPO_URL")
github_token = os.environ.get("GITHUB_ACCESS_TOKEN")

github_url = github_repo_url.replace("https", "git").replace("http", "git") + ".git"
p = parse(github_url)

url = "https://api.github.com/repos/%s/%s/issues" % (p.owner, p.repo)

organization_name = organization.name if organization else ""
organization_text = f" for company: {organization_name}" if organization else ""
suggestion = {
"title": title,
"body": description + "\n\n" + " Suggested by " + str(user) + organization_text,
"milestone": 42,
}

if github_repo_url and github_token:
response = requests.post(
url, json.dumps(suggestion), headers={"Authorization": "token " + github_token}
)

if response.status_code == 201:
messages.success(
request, "Suggestion added successfully and GitHub issue created."
)
else:
messages.warning(request, "Suggestion added but failed to create GitHub issue.")
else:
messages.warning(request, "Suggestion added but GitHub settings are missing.")

return JsonResponse({"status": "success"})
else:
messages.error(request, "Please fill all the fields.")
return JsonResponse({"status": "error"}, status=400)

companies = Company.objects.all()
return render(request, "suggestions/add_suggestion.html", {"companies": companies})


class GoogleLogin(SocialLoginView):
adapter_class = GoogleOAuth2Adapter
Expand Down Expand Up @@ -720,7 +776,10 @@ def get_context_data(self, *args, **kwargs):

def view_suggestions(request):
suggestion = Suggestion.objects.all()
return render(request, "feature_suggestion.html", {"suggestions": suggestion})
companies = Company.objects.all()
return render(
request, "feature_suggestion.html", {"suggestions": suggestion, "companies": companies}
)


def sitemap(request):
Expand Down
Loading