11package org .jenkinsci .plugins .gogs ;
22
3- import net .sf .json .JSONObject ;
4- import net .sf .json .JSONSerializer ;
5- import org .apache .http .HttpHost ;
6- import org .apache .http .client .fluent .Executor ;
7- import org .apache .http .client .fluent .Form ;
8- import org .apache .http .client .fluent .Request ;
9- import org .apache .http .entity .ContentType ;
10-
113import java .io .File ;
124import java .io .IOException ;
135import java .net .MalformedURLException ;
179import java .util .concurrent .TimeUnit ;
1810import java .util .concurrent .TimeoutException ;
1911
12+ import net .sf .json .JSONArray ;
13+ import net .sf .json .JSONObject ;
14+ import net .sf .json .JSONSerializer ;
15+ import org .apache .http .HttpHost ;
16+ import org .apache .http .client .fluent .Executor ;
17+ import org .apache .http .client .fluent .Form ;
18+ import org .apache .http .client .fluent .Request ;
19+ import org .apache .http .entity .ContentType ;
20+
2021/**
2122 * A class to handle the configuration of the Gogs server used during the integration tests.
2223 */
@@ -28,7 +29,7 @@ public class GogsConfigHandler {
2829 private String gogsServer_password ;
2930 private Executor executor = null ;
3031 private String gogsServer_apiUrl = null ;
31-
32+ private String gogsAccessToken ;
3233
3334 /**
3435 * Instantiates an object used to handle operations on a Gogs server.
@@ -46,6 +47,7 @@ public GogsConfigHandler(String gogsUrl, String user, String password) throws Ma
4647 this .gogsServer_port = aURL .getPort ();
4748 this .gogsServer_user = user ;
4849 this .gogsServer_password = password ;
50+ this .gogsAccessToken = getGogsAccessToken ();
4951 }
5052
5153 /**
@@ -60,10 +62,10 @@ void waitForServer(int retries, int retryDelay) throws TimeoutException, Interru
6062 String testUrl = this .getGogsUrl () + "/" ;
6163
6264 for (int i = 0 ; i < retries ; i ++) {
63- int status = 0 ;
65+ int status ;
6466 try {
6567 status = Request .Get (testUrl )
66- .execute ().returnResponse ().getStatusLine ().getStatusCode ();
68+ .execute ().returnResponse ().getStatusLine ().getStatusCode ();
6769 } catch (IOException e ) {
6870 TimeUnit .SECONDS .sleep (retryDelay );
6971 continue ;
@@ -100,14 +102,17 @@ int createWebHook(String jsonCommand, String projectName) throws IOException {
100102 + "/" + projectName + "/hooks" ;
101103
102104 Executor executor = getExecutor ();
105+ Request request = Request .Post (gogsHooksConfigUrl );
106+
107+ if (gogsAccessToken != null ) {
108+ request .addHeader ("Authorization" , "token " + gogsAccessToken );
109+ }
103110
104111 String result = executor
105- .execute (Request . Post ( gogsHooksConfigUrl ) .bodyString (jsonCommand , ContentType .APPLICATION_JSON ))
112+ .execute (request .bodyString (jsonCommand , ContentType .APPLICATION_JSON ))
106113 .returnContent ().asString ();
107114 JSONObject jsonObject = (JSONObject ) JSONSerializer .toJSON ( result );
108- int id = jsonObject .getInt ("id" );
109-
110- return id ;
115+ return jsonObject .getInt ("id" );
111116 }
112117
113118 /**
@@ -138,9 +143,14 @@ void removeHook(String projectName, int hookId) throws IOException {
138143 + "/" + projectName + "/hooks/" + hookId ;
139144
140145 Executor executor = getExecutor ();
146+ Request request = Request .Delete (gogsHooksConfigUrl );
147+
148+ if (gogsAccessToken != null ) {
149+ request .addHeader ("Authorization" , "token " + gogsAccessToken );
150+ }
141151
142152 int result = executor
143- .execute (Request . Delete ( gogsHooksConfigUrl ) )
153+ .execute (request )
144154 .returnResponse ().getStatusLine ().getStatusCode ();
145155
146156 if (result != 204 ) {
@@ -159,16 +169,20 @@ void createEmptyRepo(String projectName) throws IOException {
159169
160170 Executor executor = getExecutor ();
161171 String gogsHooksConfigUrl = getGogsServer_apiUrl () + "user/repos" ;
172+ Request request = Request .Post (gogsHooksConfigUrl );
173+
174+ if (this .gogsAccessToken != null ) {
175+ request .addHeader ("Authorization" , "token " + this .gogsAccessToken );
176+ }
162177
163178 int result = executor
164- .execute (Request
165- .Post (gogsHooksConfigUrl )
179+ .execute (request
166180 .bodyForm (Form .form ()
167- .add ("name" , projectName )
168- .add ("description" , "API generated repository" )
169- .add ("private" , "true" )
170- .add ("auto_init" , "false" )
171- .build ()
181+ .add ("name" , projectName )
182+ .add ("description" , "API generated repository" )
183+ .add ("private" , "true" )
184+ .add ("auto_init" , "false" )
185+ .build ()
172186 )
173187 )
174188 .returnResponse ().getStatusLine ().getStatusCode ();
@@ -190,49 +204,37 @@ private Executor getExecutor() {
190204 if (this .executor == null ) {
191205 HttpHost httpHost = new HttpHost (this .gogsServer_nodeName , this .gogsServer_port );
192206 this .executor = Executor .newInstance ()
193- .auth (httpHost , this .gogsServer_user , this .gogsServer_password )
194- .authPreemptive (httpHost );
207+ .auth (httpHost , this .gogsServer_user , this .gogsServer_password )
208+ .authPreemptive (httpHost );
195209 }
196210 return this .executor ;
197211 }
198212
213+ /**
214+ * Get Access token of the user.
215+ *
216+ * @return an access token of the user
217+ */
218+ public String getGogsAccessToken () {
219+ String resp ;
220+ String sha1 = null ;
221+ Executor executor = getExecutor ();
222+ try {
223+ resp = executor .execute (
224+ Request .Get (this .getGogsUrl () + "/api/v1/users/" + this .gogsServer_user + "/tokens" )
225+ ).returnContent ().toString ();
226+ JSONArray jsonArray = JSONArray .fromObject (resp );
227+ if (!jsonArray .isEmpty ()) {
228+ sha1 = ((JSONObject ) jsonArray .get (0 )).getString ("sha1" );
229+ }
230+ } catch (IOException e ) { }
231+ return sha1 ;
232+ }
233+
199234 public String getGogsServer_apiUrl () {
200235 if (this .gogsServer_apiUrl == null ) {
201236 this .gogsServer_apiUrl = this .getGogsUrl () + "/api/v1/" ;
202237 }
203238 return gogsServer_apiUrl ;
204239 }
205-
206- public String getGogsServer_nodeName () {
207- return gogsServer_nodeName ;
208- }
209-
210- public void setGogsServer_nodeName (String gogsServer_nodeName ) {
211- this .gogsServer_nodeName = gogsServer_nodeName ;
212- }
213-
214- public int getGogsServer_port () {
215- return gogsServer_port ;
216- }
217-
218- public void setGogsServer_port (int gogsServer_port ) {
219- this .gogsServer_port = gogsServer_port ;
220- }
221-
222- public String getGogsServer_user () {
223- return gogsServer_user ;
224- }
225-
226- public void setGogsServer_user (String gogsServer_user ) {
227- this .gogsServer_user = gogsServer_user ;
228- }
229-
230- public String getGogsServer_password () {
231- return gogsServer_password ;
232- }
233-
234- public void setGogsServer_password (String gogsServer_password ) {
235- this .gogsServer_password = gogsServer_password ;
236- }
237-
238240}
0 commit comments