Skip to content

Commit

Permalink
feat: add test case and the workflow 'Publish Specified Packages'
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Oct 31, 2023
1 parent d24910a commit 58d82f2
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 37 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test Generate Metadata

on:
push:

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: 1.21

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: go get -v -t -d ./...

- name: Test
run: go test -v ./...
10 changes: 5 additions & 5 deletions .github/workflows/update_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.19
go-version: 1.21

- name: Install kpm
run: go install kcl-lang.io/kpm@latest

- name: login
- name: Login
run: kpm login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} docker.io

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v39

- name: publish to docker.io
- name: Publish to docker.io
run: |
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
./scripts/push_pkg_from.sh $file
done
- name: publish to ghcr.io
- name: Publish to ghcr.io
run: |
kpm login -u ${{ secrets.DEPLOY_ACCESS_NAME }} -p ${{ secrets.DEPLOY_ACCESS_TOKEN }} ghcr.io
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
Expand All @@ -54,7 +54,7 @@ jobs:
KPM_REG: "ghcr.io"
KPM_REPO: "kcl-lang"

- name: update artifacthub-pkg.yaml
- name: Update artifacthub-pkg.yaml
run: |
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
go run main.go $file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
name: Republish All Packages
name: Republish Specified Packages

on:
workflow_dispatch:
inputs:
myInput:
description: 'Enter the package path'
required: true
default: ''

env:
KPM_REG: "docker.io"
Expand All @@ -24,29 +29,29 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.19
go-version: 1.21

- name: Install kpm
run: go install kcl-lang.io/kpm@latest

- name: login
- name: Login
run: kpm login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} docker.io

- name: republish to docker.io
- name: Republish to docker.io
run: |
find . -name "kcl.mod" -execdir bash -c 'echo Pushing in directory: "$PWD" && kpm push || echo Failed to push in directory: "$PWD"' \;
find ${{ github.event.inputs.myInput }} -name "kcl.mod" -execdir bash -c 'echo Pushing in directory: "$PWD" && kpm push || echo Failed to push in directory: "$PWD"' \;
- name: republish to ghcr.io
- name: Republish to ghcr.io
run: |
kpm login -u ${{ secrets.DEPLOY_ACCESS_NAME }} -p ${{ secrets.DEPLOY_ACCESS_TOKEN }} ghcr.io
find . -name "kcl.mod" -execdir bash -c 'echo Pushing in directory: "$PWD" && kpm push || echo Failed to push in directory: "$PWD"' \;
find ${{ github.event.inputs.myInput }} -name "kcl.mod" -execdir bash -c 'echo Pushing in directory: "$PWD" && kpm push || echo Failed to push in directory: "$PWD"' \;
env:
KPM_REG: "ghcr.io"
KPM_REPO: "kcl-lang"

- name: regenerate artifacthub-pkg.yaml
- name: Regenerate artifacthub-pkg.yaml
run: |
find . -name "kcl.mod" -exec bash -c 'file="{}" && echo Running for file: "$file" && go run main.go "$file" || echo Failed to run for file: "$file"' \;
find ${{ github.event.inputs.myInput }} -name "kcl.mod" -exec bash -c 'file="{}" && echo Running for file: "$file" && go run main.go "$file" || echo Failed to run for file: "$file"' \;
- name: Commit and push changes
run: |
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ require (
oras.land/oras-go/v2 v2.3.0
)

require github.com/google/go-cmp v0.5.9 // indirect

require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
Expand All @@ -26,7 +28,7 @@ require (
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/docker/cli v23.0.1+incompatible // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/docker/docker v23.0.7+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
Expand Down Expand Up @@ -82,6 +84,7 @@ require (
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools v2.2.0+incompatible
kcl-lang.io/kcl-artifact-go v0.6.0 // indirect
kcl-lang.io/kcl-go v0.6.0 // indirect
oras.land/oras-go v1.2.3 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ github.com/docker/cli v23.0.1+incompatible h1:LRyWITpGzl2C9e9uGxzisptnxAn1zfZKXy
github.com/docker/cli v23.0.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8=
github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v24.0.7+incompatible h1:Wo6l37AuwP3JaMnZa226lzVXGA3F9Ig1seQen0cKYlM=
github.com/docker/docker v24.0.7+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v23.0.7+incompatible h1:6RD6ZfR9LYacFt90L41R/6/SyO+RZ1zWU7+bt6tdRNk=
github.com/docker/docker v23.0.7+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker-credential-helpers v0.7.0 h1:xtCHsjxogADNZcdv1pKUHXryefjlVRqWqIhk/uXJp0A=
github.com/docker/docker-credential-helpers v0.7.0/go.mod h1:rETQfLdHNT3foU5kuNkFR1R1V12OJRRO5lzt2D1b5X0=
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
Expand Down Expand Up @@ -735,6 +735,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
47 changes: 27 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,21 @@ type Person struct {
Email string `yaml:"email"`
}

func main() {
if len(os.Args) < 2 {
fmt.Println("error: expected kcl package path")
os.Exit(1)
}

// 1. load the kcl package
pkgPath := os.Args[1]
func UpdateReadmeAndMetadata(pkgPath string) error {
fileName := filepath.Base(pkgPath)
if fileName != "kcl.mod" {
return
return nil
}

kpmClient, err := client.NewKpmClient()
if err != nil {
log.Fatalf("error: %v", err)
return err
}

pkgPath = filepath.Dir(pkgPath)
kclPkg, err := pkg.LoadKclPkg(pkgPath)
if err != nil {
log.Fatalf("error: %v", err)
return err
}

pkgName := kclPkg.GetPkgName()
Expand All @@ -97,12 +90,12 @@ func main() {
},
})
if err != nil {
log.Fatalf("error: %v", err)
return err
}

err = json.Unmarshal([]byte(jsonDesc), &manifest)
if err != nil {
log.Fatalf("error: %v", err)
return err
}

name := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_NAME]
Expand All @@ -116,21 +109,21 @@ func main() {
// 2. generate the install command from the markdown template
installationTemplate, err := os.ReadFile("./templates/install.md")
if err != nil {
log.Fatalf("error: %v", err)
return err
}
installDoc := strings.Replace(string(installationTemplate), MdFlagPackageName, pkgName, -1)
installDoc = strings.Replace(string(installDoc), MdFlagPackageTag, pkgTag, -1)

// 3. load the artifacthub-pkg.yaml template
data, err := os.ReadFile("./templates/ah.yaml")
if err != nil {
log.Fatalf("error: %v", err)
return err
}

var metadata Metadata
err = yaml.Unmarshal(data, &metadata)
if err != nil {
log.Fatalf("error: %v", err)
return err
}

metadata.Name = name
Expand All @@ -143,27 +136,41 @@ func main() {
// 4. generate new artifacthub-pkg.yaml
data, err = yaml.Marshal(&metadata)
if err != nil {
log.Fatalf("error: %v", err)
return err
}

ahDir := filepath.Join(pkgPath, pkgTag)

err = os.MkdirAll(ahDir, 0755)
if err != nil {
log.Fatalf("error: %v", err)
return err
}

err = copy.Copy(filepath.Join(pkgPath, README), filepath.Join(ahDir, README))
if err != nil {
if !os.IsNotExist(err) {
log.Fatalf("error: %v", err)
return err
}
}

err = os.WriteFile(filepath.Join(ahDir, AHConfFile), data, 0644)
if err != nil {
log.Fatalf("error: %v", err)
return err
}

fmt.Printf("generate artifacthub-pkg.yaml for %s succeed\n", pkgName)
return nil
}

func main() {
if len(os.Args) < 2 {
fmt.Println("error: expected kcl package path")
os.Exit(1)
}

pkgModPath := os.Args[1]
err := UpdateReadmeAndMetadata(pkgModPath)
if err != nil {
log.Fatalf("error: %v", err)
}
}
56 changes: 56 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"os"
"path/filepath"
"strings"
"testing"

"gopkg.in/yaml.v2"
"gotest.tools/assert"
"kcl-lang.io/kpm/pkg/utils"
)

func TestUpdateReadmeAndMetadata(t *testing.T) {
pwd, err := os.Getwd()
assert.Equal(t, nil, err)
testDir := filepath.Join(pwd, "helloworld")
modPath := filepath.Join(testDir, "kcl.mod")
ahPath := filepath.Join(testDir, "0.1.0", "artifacthub-pkg.yaml")

if utils.DirExists(ahPath) {
err = os.Remove(ahPath)
assert.Equal(t, nil, err)
}
assert.Equal(t, false, utils.DirExists(ahPath))
err = UpdateReadmeAndMetadata(modPath)
assert.Equal(t, nil, err)
assert.Equal(t, true, utils.DirExists(ahPath))

buf, err := os.ReadFile(ahPath)
assert.Equal(t, nil, err)

var metadata Metadata
err = yaml.Unmarshal(buf, &metadata)
assert.Equal(t, nil, err)

assert.Equal(t, "helloworld", metadata.Name)
assert.Equal(t, "helloworld", metadata.DisplayName)
assert.Equal(t, "0.1.0", metadata.Version)
assert.Equal(t, "This is a KCL package", metadata.Description)
assert.Equal(t, len(metadata.Links), 2)
assert.Equal(t, metadata.Links[0].Name, "KCL homepage")
assert.Equal(t, metadata.Links[0].URL, "https://kcl-lang.io/")
assert.Equal(t, metadata.Links[1].Name, "KCL repo")
assert.Equal(t, metadata.Links[1].URL, "https://github.com/kcl-lang/kcl")
assert.Equal(t, metadata.Provider.Name, "kcl-lang.io")
assert.Equal(t, len(metadata.Maintainers), 1)
assert.Equal(t, metadata.Maintainers[0].Name, "kcl-lang.io")
assert.Equal(t, metadata.Maintainers[0].Email, "[email protected]")

installationTemplate, err := os.ReadFile("./templates/install.md")
assert.Equal(t, nil, err)
installDoc := strings.Replace(string(installationTemplate), MdFlagPackageName, "helloworld", -1)
installDoc = strings.Replace(string(installDoc), MdFlagPackageTag, "0.1.0", -1)
assert.Equal(t, installDoc, metadata.Install)
}

0 comments on commit 58d82f2

Please sign in to comment.