Skip to content

Commit

Permalink
chore: check category value and show it in actions summary (#38)
Browse files Browse the repository at this point in the history
* chore: check category value and show it in actions summary

Signed-off-by: Hiroshi Miura <[email protected]>

* chore: validate version as Semamtic version

Signed-off-by: Hiroshi Miura <[email protected]>

---------

Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr authored Aug 25, 2023
1 parent 52d77d6 commit 30e02e1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/check-plugin-addition-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ jobs:
- run: |
sudo apt install -y gpg
- name: check manifest
run: kotlin ci/check-manifest.main.kts ${{ steps.changed-jar-files.outputs.added_files }}
run: kotlin ci/check-manifest.main.kts ${{ steps.changed-jar-files.outputs.added_files }} | tee -a $GITHUB_STEP_SUMMARY
- name: download public key
run: |
kotlin ci/import-public-key.main.kts ${GITHUB_ACTOR}
- name: check signature
run: |
gpg --verify --keyring "/tmp/trusteddb.gpg" ${{ steps.changed-sign-files.outputs.added_files }}
gpg --verify --keyring "/tmp/trusteddb.gpg" ${{ steps.changed-sign-files.outputs.added_files }} | tee -a $GITHUB_STEP_SUMMARY
63 changes: 63 additions & 0 deletions ci/check-manifest.main.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
@file:JvmName("CheckManifest")
@file:CompilerOptions("-jvm-target", "11")

import java.util.jar.Attributes
import java.util.jar.JarFile


/**
* Semver.
* Borrowed from https://github.com/musk/semver-tool
* Defines a semantic version according to [Semantic Version 2.0](https://semver.org)
*/
val NAT = "0|[1-9][0-9]*"
val ALPHANUM = "[0-9]*[A-Za-z-][0-9A-Za-z-]*"
val IDENT = "$NAT|$ALPHANUM"
val FIELD = "[0-9A-Za-z-]+"
val PREREL_REGEX = "((?:$IDENT)(?:\\.(?:$IDENT))*)"
val BUILD_REGEX = "($FIELD(?:\\.$FIELD)*)"
val SEMVER_REGEX =
"^[vV]?($NAT)\\.($NAT)\\.($NAT)(?:-$PREREL_REGEX)?(?:\\+$BUILD_REGEX)?$"

fun matchSemver(version: String): MatchResult? = Regex(SEMVER_REGEX).find(version)

/**
* Checks if the [String] contains a valid semantic version
*/
fun String.isSemver(): Boolean = validateSemver(this)

/**
* Validates the [String] whether it is a valid semantic version.
*/
fun validateSemver(version: String): Boolean = matchSemver(version) != null

// Mapping from Jar attributes to plugin attribute keys
val terms = mapOf("Plugin-Name" to "Name", "Bundle-Name" to "Name", "Implementation-Title" to "Name",
"Plugin-Version" to "Version", "Bundle-Version" to "Version", "Implementation-Version" to "Version",
Expand All @@ -15,6 +43,23 @@ val terms = mapOf("Plugin-Name" to "Name", "Bundle-Name" to "Name", "Implementat
val checkList = mutableMapOf("Name" to false, "Version" to false, "Author" to false, "Category" to false,
"Class-Name" to false)

// Check acceptable category values
fun getAttr(attr: Attributes, term: String): String {
for (entry in attr) {
when (terms.get(entry.key.toString())) {
term -> return entry.value.toString()
}
}
return "unknown"
}

fun getCategory(attr: Attributes): String {
return getAttr(attr, "Category")
}

val categoryList = mutableListOf("filter", "tokenizer", "marker", "machinetranslator", "base", "glossary", "dictionary",
"theme", "miscellaneous")

// Accept only one argument as jar file
if (args.size != 1) {
System.err.println("Multiple jar files seems to be added...")
Expand All @@ -33,3 +78,21 @@ if (result != null) {
System.err.println(String.format("%s is missing in MANIFEST.MF", result))
System.exit(1)
}

val category = getCategory(mainAttr)
if (category !in categoryList) {
System.err.println(String.format("Category %s is invalid value in MANIFEST.MF", category))
System.exit(1)
}

val version = getAttr(mainAttr, "Version")
if (version.isSemver()) {
System.err.println(String.format("Version %s is invalid as a Semantic Version in MANIFEST.MF", version))
}

// print manifest summary
System.out.println("Manifest summary")
System.out.println(String.format("Name: %s", getAttr(mainAttr, "Name")))
System.out.println(String.format("Version: %s", version))
System.out.println(String.format("Author: %s", getAttr(mainAttr, "Author")))
System.out.println(String.format("Category: %s", category))
21 changes: 21 additions & 0 deletions ci/semver.license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 musk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0 comments on commit 30e02e1

Please sign in to comment.