Skip to content

Commit

Permalink
update pr 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruv-Sharma01 committed Jan 24, 2025
1 parent 955af6e commit 5957bbf
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 126 deletions.
1 change: 1 addition & 0 deletions myenv/bin/python
1 change: 1 addition & 0 deletions myenv/bin/python3
1 change: 1 addition & 0 deletions myenv/bin/python3.12
1 change: 1 addition & 0 deletions myenv/lib64
5 changes: 5 additions & 0 deletions myenv/pyvenv.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
home = /usr/bin
include-system-site-packages = false
version = 3.12.3
executable = /usr/bin/python3.12
command = /usr/bin/python3 -m venv /home/dhruv-sharma/Desktop/OWASP_BLT/BLT/myenv
228 changes: 114 additions & 114 deletions project_channels.csv

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions website/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
PRAnalysisReport,
Project,
Repo,
SlackChannel,
SlackIntegration,
Subscription,
Suggestion,
Expand Down Expand Up @@ -121,6 +122,11 @@ class ImageInline(admin.TabularInline):
extra = 1


class SlackChannelAdmin(admin.ModelAdmin):
list_display = ("slack_channel", "slack_id", "slack_url")
search_fields = ("slack_channel", "slack_id")


class IssueAdmin(admin.ModelAdmin):
list_display = (
"id",
Expand Down Expand Up @@ -494,3 +500,4 @@ class PostAdmin(admin.ModelAdmin):
admin.site.register(Activity)
admin.site.register(PRAnalysisReport)
admin.site.register(Post, PostAdmin)
admin.site.register(SlackChannel, SlackChannelAdmin)
60 changes: 60 additions & 0 deletions website/management/commands/import_slack_channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import requests
from django.conf import settings
from django.core.management.base import BaseCommand

from website.models import Project # Replace `website` with the actual app name

SLACK_TOKEN = settings.SLACK_TOKEN
SLACK_API_URL = "https://slack.com/api/conversations.list"
HEADERS = {"Authorization": f"Bearer {SLACK_TOKEN}"}


class Command(BaseCommand):
help = "Fetch Slack channels and associate them with projects"

def fetch_channels(self):
url = SLACK_API_URL
params = {"limit": 1000, "types": "public_channel"} # Fetch only public channels
channels = []

while url:
response = requests.get(url, headers=HEADERS, params=params)
data = response.json()

if not data.get("ok"):
self.stdout.write(f"Error: {data.get('error')}")
break

channels.extend(data.get("channels", []))
cursor = data.get("response_metadata", {}).get("next_cursor")
if cursor:
url = SLACK_API_URL + f"?cursor={cursor}"
else:
url = None

return channels

def handle(self, *args, **kwargs):
self.stdout.write("Fetching Slack channels...")

channels = self.fetch_channels()
for channel in channels:
if channel["name"].startswith("project-"):
project_name = channel["name"].replace("project-", "").capitalize()

# Update or create project with Slack details
project, created = Project.objects.update_or_create(
name=project_name,
defaults={
"slack_channel": channel["name"],
"slack_id": channel["id"],
"slack_url": f"https://OWASP.slack.com/archives/{channel['id']}",
},
)

if created:
self.stdout.write(f"Created new project: {project_name}")
else:
self.stdout.write(f"Updated existing project: {project_name}")

self.stdout.write(f"Processed {len(channels)} Slack channels.")
1 change: 0 additions & 1 deletion website/migrations/0180_project_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("website", "0179_contributorstats"),
]
Expand Down
1 change: 0 additions & 1 deletion website/migrations/0181_alter_project_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("website", "0180_project_slack"),
]
Expand Down
1 change: 0 additions & 1 deletion website/migrations/0182_merge_20250116_0944.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("website", "0180_rename_project_visit_count_repo_repo_visit_count"),
("website", "0181_alter_project_slack"),
Expand Down
29 changes: 29 additions & 0 deletions website/migrations/0183_slackchannel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 5.1.4 on 2025-01-19 17:57

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("website", "0182_merge_20250116_0944"),
]

operations = [
migrations.CreateModel(
name="SlackChannel",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("slack_channel", models.CharField(max_length=255)),
("slack_id", models.CharField(max_length=255, unique=True)),
("slack_url", models.URLField()),
],
),
]
4 changes: 3 additions & 1 deletion website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,9 @@ class Project(models.Model):
project_visit_count = models.IntegerField(default=0)
twitter = models.CharField(max_length=30, null=True, blank=True)
slack = models.URLField(null=True, blank=True)

slack_channel = models.CharField(max_length=255, blank=True, null=True)
slack_id = models.CharField(max_length=255, unique=True, blank=True, null=True)

facebook = models.URLField(null=True, blank=True)
logo = models.ImageField(upload_to="project_logos", null=True, blank=True)
created = models.DateTimeField(auto_now_add=True) # Standardized field name
Expand Down
6 changes: 3 additions & 3 deletions website/templates/projects/project_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ <h3 class="text-xl font-bold text-gray-900">Add New Project</h3>
<div>
<label class="block text-sm font-medium text-gray-700">Slack URL</label>
<input type="url"
name="slack"
class="mt-1 w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-red-500"
placeholder="https://slack.com/..." />
name="slack"
class="mt-1 w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-red-500"
placeholder="https://slack.com/..." />
</div>
</div>
<!-- Repositories Section -->
Expand Down
10 changes: 7 additions & 3 deletions website/templates/projects/repo_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,18 @@ <h1 class="text-3xl font-bold text-gray-900">{{ repo.name }}</h1>
</a>
{% endif %}
{% if repo.slack %}
<a href="{{ repo.slack }}" target="_blank" class="inline-flex items-center px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-lg transition-colors">
<svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<a href="{{ repo.slack }}"
target="_blank"
class="inline-flex items-center px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-lg transition-colors">
<svg class="w-5 h-5 mr-2"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 01-9 9m9-9a9 9 0 00-9-9m9 9H3m9 9a9 9 0 01-9-9m9 9c1.657 0 3-4.03 3-9s-1.343-9-3-9m0 18c-1.657 0-3-4.03-3-9s1.343-9 3-9m-9 9a9 9 0 019-9" />
</svg>
Join on Slack
</a>
{% endif %}

<a href="{{ repo.repo_url }}"
target="_blank"
class="inline-flex items-center px-4 py-2 bg-red-500 hover:bg-red-600 text-white rounded-lg transition-colors">
Expand Down
3 changes: 1 addition & 2 deletions website/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ def validate_url(url):
status=400,
)


# Validate repository URLs
repo_urls = request.POST.getlist("repo_urls[]")
for url in repo_urls:
Expand Down Expand Up @@ -1413,4 +1412,4 @@ def get(self, request, slug):
response["Pragma"] = "no-cache"
response["Expires"] = "0"

return response
return response

0 comments on commit 5957bbf

Please sign in to comment.