3030import edu .umd .cs .findbugs .annotations .NonNull ;
3131import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
3232import org .apache .commons .lang3 .StringUtils ;
33+ import org .kohsuke .github .function .InputStreamFunction ;
3334
3435import java .io .FileNotFoundException ;
3536import java .io .IOException ;
4950import java .util .LinkedHashSet ;
5051import java .util .List ;
5152import java .util .Map ;
52- import java .util .Objects ;
5353import java .util .Set ;
5454import java .util .TreeMap ;
5555import java .util .WeakHashMap ;
5656
5757import javax .annotation .Nonnull ;
5858
5959import static java .util .Arrays .*;
60- import static org .kohsuke .github .internal .Previews .ANTIOPE ;
61- import static org .kohsuke .github .internal .Previews .ANT_MAN ;
62- import static org .kohsuke .github .internal .Previews .BAPTISTE ;
63- import static org .kohsuke .github .internal .Previews .FLASH ;
64- import static org .kohsuke .github .internal .Previews .INERTIA ;
65- import static org .kohsuke .github .internal .Previews .MERCY ;
66- import static org .kohsuke .github .internal .Previews .SHADOW_CAT ;
60+ import static java .util .Objects .requireNonNull ;
61+ import static org .kohsuke .github .internal .Previews .*;
6762
6863/**
6964 * A repository on GitHub.
@@ -1788,7 +1783,7 @@ public InputStream readBlob(String blobSha) throws IOException {
17881783 return root .createRequest ()
17891784 .withHeader ("Accept" , "application/vnd.github.v3.raw" )
17901785 .withUrlPath (target )
1791- .fetchStream ();
1786+ .fetchStream (Requester :: copyInputStream );
17921787 }
17931788
17941789 /**
@@ -2815,7 +2810,7 @@ public Reader renderMarkdown(String text, MarkdownMode mode) throws IOException
28152810 .with ("mode" , mode == null ? null : mode .toString ())
28162811 .with ("context" , getFullName ())
28172812 .withUrlPath ("/markdown" )
2818- .fetchStream (),
2813+ .fetchStream (Requester :: copyInputStream ),
28192814 "UTF-8" );
28202815 }
28212816
@@ -2969,6 +2964,52 @@ public GHTagObject createTag(String tag, String message, String object, String t
29692964 .wrap (this );
29702965 }
29712966
2967+ /**
2968+ * Streams a zip archive of the repository, optionally at a given <code>ref</code>.
2969+ *
2970+ * @param <T>
2971+ * the type of result
2972+ * @param streamFunction
2973+ * The {@link InputStreamFunction} that will process the stream
2974+ * @param ref
2975+ * if <code>null</code> the repository's default branch, usually <code>master</code>,
2976+ * @throws IOException
2977+ * The IO exception.
2978+ * @return the result of reading the stream.
2979+ */
2980+ public <T > T readZip (InputStreamFunction <T > streamFunction , String ref ) throws IOException {
2981+ return downloadArchive ("zip" , ref , streamFunction );
2982+ }
2983+
2984+ /**
2985+ * Streams a tar archive of the repository, optionally at a given <code>ref</code>.
2986+ *
2987+ * @param <T>
2988+ * the type of result
2989+ * @param streamFunction
2990+ * The {@link InputStreamFunction} that will process the stream
2991+ * @param ref
2992+ * if <code>null</code> the repository's default branch, usually <code>master</code>,
2993+ * @throws IOException
2994+ * The IO exception.
2995+ * @return the result of reading the stream.
2996+ */
2997+ public <T > T readTar (InputStreamFunction <T > streamFunction , String ref ) throws IOException {
2998+ return downloadArchive ("tar" , ref , streamFunction );
2999+ }
3000+
3001+ private <T > T downloadArchive (@ Nonnull String type ,
3002+ @ CheckForNull String ref ,
3003+ @ Nonnull InputStreamFunction <T > streamFunction ) throws IOException {
3004+ requireNonNull (streamFunction , "Sink must not be null" );
3005+ String tailUrl = getApiTailUrl (type + "ball" );
3006+ if (ref != null ) {
3007+ tailUrl += "/" + ref ;
3008+ }
3009+ final Requester builder = root .createRequest ().method ("GET" ).withUrlPath (tailUrl );
3010+ return builder .fetchStream (streamFunction );
3011+ }
3012+
29723013 /**
29733014 * Populate this object.
29743015 *
@@ -2980,7 +3021,7 @@ void populate() throws IOException {
29803021 return ; // can't populate if the root is offline
29813022 }
29823023
2983- final URL url = Objects . requireNonNull (getUrl (), "Missing instance URL!" );
3024+ final URL url = requireNonNull (getUrl (), "Missing instance URL!" );
29843025
29853026 try {
29863027 // IMPORTANT: the url for repository records does not reliably point to the API url.
0 commit comments