1+ /**
2+ * Copyright (C) 2025 AIDC-AI
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com .alibaba .langengine .gitcode ;
18+
19+ import org .springframework .beans .factory .annotation .Value ;
20+ import org .springframework .stereotype .Component ;
21+ import org .springframework .util .StringUtils ;
22+
23+ @ Component
24+ public class GitCodeConfiguration {
25+
26+ /**
27+ * GitCode API Base URL
28+ */
29+ public static final String GITCODE_API_URL = getEnvOrDefault ("GITCODE_API_URL" , "https://api.gitcode.com" );
30+
31+ /**
32+ * GitCode API Token - read from environment variable first, then from config file
33+ */
34+ @ Value ("${ali.langengine.community.gitcode.access_token:}" )
35+ private String configAccessToken ;
36+
37+ private static GitCodeConfiguration instance ;
38+
39+ public GitCodeConfiguration () {
40+ instance = this ;
41+ }
42+
43+ /**
44+ * Get GitCode Access Token
45+ * Priority: environment variable > config file
46+ */
47+ public static String getAccessToken () {
48+ // Try to get from environment variable first
49+ String envToken = System .getenv ("GITCODE_ACCESS_TOKEN" );
50+ if (StringUtils .hasText (envToken )) {
51+ return envToken ;
52+ }
53+
54+ // If environment variable is not set, get from config file
55+ if (instance != null && StringUtils .hasText (instance .configAccessToken )) {
56+ return instance .configAccessToken ;
57+ }
58+
59+ return null ;
60+ }
61+
62+ /**
63+ * Backward compatibility: keep original static constant, but now calls new method
64+ * @deprecated Recommend using getAccessToken() method
65+ */
66+ @ Deprecated
67+ public static final String GITCODE_ACCESS_TOKEN = getAccessToken ();
68+
69+ /**
70+ * GitCode Search API URL
71+ */
72+ public static final String GITCODE_SEARCH_API_URL = GITCODE_API_URL + "/api/v5/search" ;
73+
74+ /**
75+ * Default timeout in seconds
76+ */
77+ public static final int DEFAULT_TIMEOUT_SECONDS = 30 ;
78+
79+ /**
80+ * Default number of results per page
81+ */
82+ public static final int DEFAULT_PER_PAGE = 20 ;
83+
84+ /**
85+ * Maximum number of results per page
86+ */
87+ public static final int MAX_PER_PAGE = 50 ;
88+
89+ /**
90+ * Get environment variable value, return default value if not exists
91+ *
92+ * @param key Environment variable key
93+ * @param defaultValue Default value
94+ * @return Environment variable value or default value
95+ */
96+ private static String getEnvOrDefault (String key , String defaultValue ) {
97+ String value = System .getenv (key );
98+ return value != null ? value : defaultValue ;
99+ }
100+
101+ /**
102+ * Get integer environment variable value, return default value if not exists or cannot parse
103+ *
104+ * @param key Environment variable key
105+ * @param defaultValue Default value
106+ * @return Environment variable value or default value
107+ */
108+ public static int getIntEnvOrDefault (String key , int defaultValue ) {
109+ String value = System .getenv (key );
110+ if (value != null ) {
111+ try {
112+ return Integer .parseInt (value );
113+ } catch (NumberFormatException e ) {
114+ return defaultValue ;
115+ }
116+ }
117+ return defaultValue ;
118+ }
119+
120+ /**
121+ * Validate if GitCode Access Token exists
122+ *
123+ * @return true if token exists and is not empty, false otherwise
124+ */
125+ public static boolean hasValidToken () {
126+ String token = getAccessToken ();
127+ return StringUtils .hasText (token );
128+ }
129+
130+ /**
131+ * Validate if configuration is complete
132+ *
133+ * @throws IllegalStateException if configuration is incomplete
134+ */
135+ public static void validateConfiguration () {
136+ if (!hasValidToken ()) {
137+ throw new IllegalStateException (
138+ "GitCode Access Token is not configured. " +
139+ "Please set GITCODE_ACCESS_TOKEN environment variable or " +
140+ "configure ali.langengine.community.gitcode.access_token in application.yml" );
141+ }
142+
143+ if (GITCODE_API_URL == null || GITCODE_API_URL .trim ().isEmpty ()) {
144+ throw new IllegalStateException ("GitCode API URL is not configured." );
145+ }
146+ }
147+
148+ /**
149+ * Get configuration summary (without sensitive information)
150+ *
151+ * @return Configuration summary string
152+ */
153+ public static String getConfigurationSummary () {
154+ StringBuilder summary = new StringBuilder ();
155+ summary .append ("GitCode Configuration:\n " );
156+ summary .append (" API URL: " ).append (GITCODE_API_URL ).append ("\n " );
157+ summary .append (" Token configured: " ).append (hasValidToken () ? "Yes" : "No" ).append ("\n " );
158+ summary .append (" Search API URL: " ).append (GITCODE_SEARCH_API_URL ).append ("\n " );
159+ summary .append (" Default timeout: " ).append (DEFAULT_TIMEOUT_SECONDS ).append ("s\n " );
160+ summary .append (" Default per page: " ).append (DEFAULT_PER_PAGE ).append ("\n " );
161+ summary .append (" Max per page: " ).append (MAX_PER_PAGE );
162+ return summary .toString ();
163+ }
164+ }
0 commit comments