Skip to content

Commit b513b07

Browse files
committed
[feat] Search project by project url and set branch for linting.
1 parent 5b2c703 commit b513b07

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

src/main/kotlin/com/github/blarc/gitlab/template/lint/plugin/GitlabLintRunner.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package com.github.blarc.gitlab.template.lint.plugin
22

33
import com.github.blarc.gitlab.template.lint.plugin.gitlab.GitLabFactory
44
import com.intellij.dvcs.DvcsUtil
5-
import com.intellij.notification.NotificationGroupManager
6-
import com.intellij.notification.NotificationType
75
import com.intellij.openapi.components.Service
86
import com.intellij.openapi.project.Project
97
import git4idea.GitUtil
@@ -19,26 +17,28 @@ class GitlabLintRunner(private val project: Project) {
1917
val repository = DvcsUtil.guessCurrentRepositoryQuick(project, repositoryManager, filePath)
2018

2119
if (repository != null) {
22-
val url = repository.remotes.first().urls[0].toHttpUrlOrNull()
23-
val projectName = repository.project.name
24-
val gitlab = GitLabFactory.getInstance(project)!!.getGitLab("${url?.scheme}://${url?.host}")
25-
2620
var result: GitlabLintResponse? = null
27-
gitlab.searchProject(projectName).thenAccept {
28-
val projectId = it[0].id
29-
gitlab.lintContent(content, projectId).thenAccept { gitlabLintResponse ->
30-
result = gitlabLintResponse
21+
try {
22+
23+
val url = repository.remotes.first().firstUrl?.toHttpUrlOrNull()
24+
val gitlab = GitLabFactory.getInstance(project)!!.getGitLab("${url?.scheme}://${url?.host}")
25+
26+
gitlab.searchProject(url.toString().removeSuffix(".git")).thenAccept {
27+
val projectId = it.id
28+
val branch = repository.currentBranch!!.name
29+
gitlab.lintContent(content, projectId, branch).thenAccept { gitlabLintResponse ->
30+
result = gitlabLintResponse
31+
}.get()
3132
}.get()
32-
}.get()
33+
}
34+
catch (e: Exception) {
35+
GitlabLintUtils.createNotification(project, e.localizedMessage)
36+
}
3337

3438
return result
3539
}
3640

37-
NotificationGroupManager.getInstance()
38-
.getNotificationGroup("GitlabLint")
39-
.createNotification("Gitlab Lint", "Project is not a gitlab repo.", NotificationType.ERROR)
40-
.notify(project)
41-
41+
GitlabLintUtils.createNotification(project, "Project is not a gitlab repository.")
4242
return null
4343
}
4444
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.blarc.gitlab.template.lint.plugin
2+
3+
import com.intellij.notification.NotificationGroupManager
4+
import com.intellij.notification.NotificationType
5+
import com.intellij.openapi.project.Project
6+
7+
class GitlabLintUtils {
8+
companion object{
9+
fun createNotification(project: Project, message: String, type: NotificationType = NotificationType.ERROR) {
10+
NotificationGroupManager.getInstance()
11+
.getNotificationGroup("GitlabLint")
12+
.createNotification("Gitlab Lint", message, type)
13+
.notify(project)
14+
}
15+
}
16+
}

src/main/kotlin/com/github/blarc/gitlab/template/lint/plugin/gitlab/Gitlab.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ open class GitLab @JvmOverloads constructor(
7171
}
7272

7373
@OptIn(ExperimentalSerializationApi::class)
74-
fun searchProject(projectName: String): CompletableFuture<Array<GitlabProject>> {
74+
fun searchProject(projectUrl: String): CompletableFuture<GitlabProject> {
7575

76+
val projectName = projectUrl.split("/").last()
7677
val request: Request = prepareRequest("/projects?search=$projectName")
7778
.get()
7879
.build()
7980

80-
val result = CompletableFuture<Array<GitlabProject>>()
81+
val result = CompletableFuture<GitlabProject>()
8182

8283
httpClient.newCall(request)
8384
.enqueue(object: Callback{
@@ -89,8 +90,9 @@ open class GitLab @JvmOverloads constructor(
8990
response.use {
9091
if (it.isSuccessful) {
9192
val responseString = it.body!!.string()
92-
val decodeFromString = json.decodeFromString<Array<GitlabProject>>(responseString)
93-
result.complete(decodeFromString)
93+
val gitlabProjects = json.decodeFromString<Array<GitlabProject>>(responseString)
94+
val gitlabProject = gitlabProjects.find { gitlabProject -> gitlabProject.webUrl.equals(projectUrl, true) }
95+
result.complete(gitlabProject)
9496
}
9597
else {
9698
result.completeExceptionally(RuntimeException(it.body?.string() ?: "Request was unsuccessful!"))
@@ -103,10 +105,12 @@ open class GitLab @JvmOverloads constructor(
103105
}
104106

105107
@OptIn(ExperimentalSerializationApi::class)
106-
fun lintContent(content: String, projectId: Long): CompletableFuture<GitlabLintResponse> {
108+
fun lintContent(content: String, projectId: Long, branch: String): CompletableFuture<GitlabLintResponse> {
107109

108110
val formBody = FormBody.Builder()
109111
.add("content", content)
112+
.add("ref", branch)
113+
.add("dry_run", "true")
110114
.build()
111115

112116
val request = prepareRequest("/projects/${projectId}/ci/lint")
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.github.blarc.gitlab.template.lint.plugin.gitlab
22

3-
@kotlinx.serialization.Serializable
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
47
data class GitlabProject(
5-
val id: Long
8+
val id: Long,
9+
@SerialName("web_url")
10+
val webUrl: String
611
)

0 commit comments

Comments
 (0)