22import json
33import secrets
44import os
5+ import requests
56
67# Django imports
78from django .core .management .base import BaseCommand , CommandError
89from django .utils import timezone
9- from django . conf import settings
10+
1011
1112# Module imports
1213from plane .license .models import Instance , InstanceEdition
@@ -20,35 +21,50 @@ def add_arguments(self, parser):
2021 # Positional argument
2122 parser .add_argument ("machine_signature" , type = str , help = "Machine signature" )
2223
23- def read_package_json (self ):
24- with open ("package.json" , "r" ) as file :
25- # Load JSON content from the file
26- data = json .load (file )
24+ def check_for_current_version (self ):
25+ if os .environ .get ("APP_VERSION" , False ):
26+ return os .environ .get ("APP_VERSION" )
27+
28+ try :
29+ with open ("package.json" , "r" ) as file :
30+ data = json .load (file )
31+ return data .get ("version" , "v0.1.0" )
32+ except Exception :
33+ self .stdout .write ("Error checking for current version" )
34+ return "v0.1.0"
2735
28- payload = {
29- "instance_key" : settings .INSTANCE_KEY ,
30- "version" : data .get ("version" , 0.1 ),
31- }
32- return payload
36+ def check_for_latest_version (self , fallback_version ):
37+ try :
38+ response = requests .get (
39+ "https://api.github.com/repos/makeplane/plane/releases/latest" ,
40+ timeout = 10 ,
41+ )
42+ response .raise_for_status ()
43+ data = response .json ()
44+ return data .get ("tag_name" , fallback_version )
45+ except Exception :
46+ self .stdout .write ("Error checking for latest version" )
47+ return fallback_version
3348
3449 def handle (self , * args , ** options ):
3550 # Check if the instance is registered
3651 instance = Instance .objects .first ()
3752
53+ current_version = self .check_for_current_version ()
54+ latest_version = self .check_for_latest_version (current_version )
55+
3856 # If instance is None then register this instance
3957 if instance is None :
4058 machine_signature = options .get ("machine_signature" , "machine-signature" )
4159
4260 if not machine_signature :
4361 raise CommandError ("Machine signature is required" )
4462
45- payload = self .read_package_json ()
46-
4763 instance = Instance .objects .create (
4864 instance_name = "Plane Community Edition" ,
4965 instance_id = secrets .token_hex (12 ),
50- current_version = payload . get ( "version" ) ,
51- latest_version = payload . get ( "version" ) ,
66+ current_version = current_version ,
67+ latest_version = latest_version ,
5268 last_checked_at = timezone .now (),
5369 is_test = os .environ .get ("IS_TEST" , "0" ) == "1" ,
5470 edition = InstanceEdition .PLANE_COMMUNITY .value ,
@@ -57,11 +73,11 @@ def handle(self, *args, **options):
5773 self .stdout .write (self .style .SUCCESS ("Instance registered" ))
5874 else :
5975 self .stdout .write (self .style .SUCCESS ("Instance already registered" ))
60- payload = self . read_package_json ()
76+
6177 # Update the instance details
6278 instance .last_checked_at = timezone .now ()
63- instance .current_version = payload . get ( "version" )
64- instance .latest_version = payload . get ( "version" )
79+ instance .current_version = current_version
80+ instance .latest_version = latest_version
6581 instance .is_test = os .environ .get ("IS_TEST" , "0" ) == "1"
6682 instance .edition = InstanceEdition .PLANE_COMMUNITY .value
6783 instance .save ()
0 commit comments