Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artifactory #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ android {
versionCode getGitCommitCount()
versionName "1.0"

minSdkVersion 21
minSdkVersion 26
targetSdkVersion 33

buildConfigField "String", 'GIT_REPOSITORY', "\"" + getGitOriginRemote() + "\""
}
packagingOptions {
resources {
pickFirsts += ['META-INF/atomicfu.kotlin_module']
pickFirsts += ['META-INF/atomicfu.kotlin_module', 'META-INF/DEPENDENCIES']
}
}

Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/info/hannes/github/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,23 @@ class MainActivity : AppCompatActivity() {
BuildConfig.GIT_REPOSITORY
)

binding.button.setOnClickListener {
binding.buttonGithub.setOnClickListener {
AppUpdateHelper.checkWithDialog(
this,
BuildConfig.GIT_REPOSITORY,
{ msg -> Log.d("result", msg) },
force = true // just to enable debugging, without you can only debug once a day
)
}

binding.buttonArtifactory.setOnClickListener {
AppUpdateHelper.checkArtifactoryDialog(
this,
"https://artifactory.myserver.info",
{ msg -> Log.d("result", msg) },
force = true // just to enable debugging, without you can only debug once a day
)
}
}

}
17 changes: 13 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
<LinearLayout
android:id="@+id/fragment_main"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">

<Button
android:id="@+id/button"
android:id="@+id/buttonGithub"
android:layout_gravity="center|center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
android:text="check Github"
tools:ignore="HardcodedText" />
</FrameLayout>

<Button
android:id="@+id/buttonArtifactory"
android:layout_gravity="center|center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check Artifactory"
tools:ignore="HardcodedText" />
</LinearLayout>

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ allprojects {
repositories {
google()
mavenCentral()
jcenter()
}
}

Expand Down
9 changes: 8 additions & 1 deletion githubAppUpdate/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

defaultConfig {
targetSdkVersion 33
minSdkVersion 21
minSdkVersion 26
project.archivesBaseName = "githubAppUpdate"
}
namespace 'info.hannes.github'
Expand All @@ -19,6 +19,10 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.google.code.gson:gson:2.10.1'
implementation "androidx.work:work-runtime-ktx:2.7.1"
implementation 'org.jfrog.artifactory.client:artifactory-java-client-services:2.13.1'
// implementation 'org.apache.httpcomponents:httpclient:4.5.13'
// api 'org.apache.httpcomponents:httpcore:4.4.14'
// api 'org.apache.httpcomponents:httpmime:4.5.6'

implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "androidx.appcompat:appcompat:1.6.0"
Expand All @@ -29,6 +33,9 @@ dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
api "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1"
implementation "com.squareup.okhttp3:logging-interceptor:4.10.0"

testImplementation 'junit:junit:4.13.2'
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4"
}

project.afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.logging.HttpLoggingInterceptor
import org.jfrog.artifactory.client.Artifactory
import org.jfrog.artifactory.client.ArtifactoryClientBuilder
import retrofit2.Response
import java.util.concurrent.TimeUnit

import org.jfrog.artifactory.client.model.impl.RepositoryTypeImpl.LOCAL
import org.jfrog.artifactory.client.model.impl.RepositoryTypeImpl.REMOTE
import org.jfrog.artifactory.client.model.impl.RepositoryTypeImpl.VIRTUAL
import org.jfrog.artifactory.client.model.impl.RepositoryTypeImpl.FEDERATED


object AppUpdateHelper {

Expand All @@ -43,11 +50,46 @@ object AppUpdateHelper {
}

// silently in background
fun checkForNewVersion(activity: AppCompatActivity, gitRepoUrl: String, repeatTime : Long = 6, timeUnit: TimeUnit = TimeUnit.HOURS) {
fun checkForNewVersion(activity: AppCompatActivity, gitRepoUrl: String, repeatTime: Long = 6, timeUnit: TimeUnit = TimeUnit.HOURS) {
val currentVersionName = activity.getVersionName()
DownloadWorker.run(activity, currentVersionName, gitRepoUrl, repeatTime, timeUnit)
}

fun checkArtifactoryDialog(
activity: AppCompatActivity,
gitRepoUrl: String,
callback: ((String) -> Unit)? = null,
force: Boolean = false
) = activity.lifecycle.coroutineScope.launch(Dispatchers.Main) {

val currentVersionName = activity.getVersionName()

val key = "LAST_VERSION_CHECK"
val prefs = PreferenceManager.getDefaultSharedPreferences(activity)

if (force || prefs.getLong(key, 0) < System.currentTimeMillis() - 1000 * 3600 * 24 / 24 / 60 * 5) {
try {
val versionList = requestArtifactoryVersions(gitRepoUrl)
prefs.edit().putLong(key, System.currentTimeMillis()).apply()

versionList?.body()?.firstOrNull()?.let { release ->
val assetApk = release.assets.find { it.name.endsWith("release.apk") }

Log.d("AppUpdateHelper", release.tagName + " > " + currentVersionName + " " + (release.tagName > currentVersionName))
callback?.invoke(release.tagName)
if (release.tagName > currentVersionName) {
askUser(activity, currentVersionName, release, assetApk)
} else {
callback?.invoke("Nothing to do with ${release.tagName}")
}
}
} catch (e: Exception) {
Log.e("AppUpdateHelper", "git check deliver: ${e.message}")
Toast.makeText(activity, "git check delivers: ${e.message}", Toast.LENGTH_LONG).show()
}
}
}

fun checkWithDialog(
activity: AppCompatActivity,
gitRepoUrl: String,
Expand Down Expand Up @@ -87,7 +129,7 @@ object AppUpdateHelper {
appContext: Context,
currentVersionName: String,
gitRepoUrl: String
){
) {
try {
val versionList = requestVersionsSync(gitRepoUrl)

Expand All @@ -111,6 +153,37 @@ object AppUpdateHelper {
return client.github.getGithubVersions(gitRepoUrl.user(), gitRepoUrl.repo()).execute()
}

suspend fun requestArtifactoryVersions(artifactoryRepoUrl: String): Response<MutableList<GithubVersion>>? {
val artifactory: Artifactory = ArtifactoryClientBuilder.create()
.setUrl(artifactoryRepoUrl)
//.setUsername("username")
//.setPassword("password")
.build() // TODO runtime error with "No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier"
val versionList = withContext(Dispatchers.Default) {
val repositories = artifactory.repositories()

// val allBuilds = artifactory.builds().allBuilds
// Log.d("allBuilds", allBuilds.toString())

val localRepoList = repositories.list(LOCAL)
val remoteRepoList = repositories.list(REMOTE)
val virtualRepoList = repositories.list(VIRTUAL)
val federatedRepoList = repositories.list(FEDERATED)

Log.d("data", localRepoList.toString())
// val list = artifactory.storage().storageInfo.repositoriesSummaryList
// Log.d("list", list.toString())
val dataA = artifactory.repository("AppStore")
.download("/AppStore/AutonomousCar/app/1.2/app-1.2.pom")
.doDownload()
Log.d("dataA", dataA.toString())
Log.d("data", repositories.toString())
Log.d("data", repositories.replicationApi)
}
Log.d("data", versionList.toString())
return null
}

private suspend fun requestGithubVersions(gitRepoUrl: String): Response<MutableList<GithubVersion>> {
val versionList = withContext(Dispatchers.Default) {
val client = GithubClient(HttpLoggingInterceptor.Level.BODY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package info.hannes.github

import junit.framework.Assert.assertEquals
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class AppUpdateHelperTest {

@org.junit.Before
fun setUp() {
}

@org.junit.After
fun tearDown() {
}

@org.junit.Test
fun getPackageInfo() {
}

@org.junit.Test
fun requestArtifactoryVersions() = runTest {
val data = AppUpdateHelper.requestArtifactoryVersions("https://artifactory.mxtracks.info")
assertEquals("Hello world", data)
}

@Test
fun dataShouldBeHelloWorld() = runTest {
val data = fetchData()
assertEquals("Hello world", data)
}

suspend fun fetchData(): String {
delay(1000L)
return "Hello world"
}

}