Skip to content

Commit

Permalink
Merge pull request #27 from LinuxSuRen/fix-remote-tag-check
Browse files Browse the repository at this point in the history
Fix the wrong remote tag checking
  • Loading branch information
LinuxSuRen authored Nov 6, 2021
2 parents d4c188f + f7472f0 commit 8edf741
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 35 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Pull Request Build

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
name: Build
runs-on: ubuntu-20.04
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v2.1.4
with:
go-version: 1.16
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2.3.4
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2.7.0
with:
version: latest
args: release --skip-publish --rm-dist

UnitTest:
name: Test
runs-on: ubuntu-20.04
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v2.1.3
with:
go-version: 1.16
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2.3.4
- name: Test
run: |
go test ./... -coverprofile coverage.out
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.out
flags: unittests
name: codecov-umbrella
fail_ci_if_error: true
24 changes: 0 additions & 24 deletions .github/workflows/pull-request.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ test: manifests generate fmt vet ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
test-int:
go test --tags=integration ./... -coverprofile cover.out

##@ Build

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![codecov](https://codecov.io/gh/kubesphere-sigs/ks-releaser/branch/master/graph/badge.svg?token=p7KBgnuIxn)](https://codecov.io/gh/kubesphere-sigs/ks-releaser)

This project aims to help to release a project which especially has multiple git repositories.

## Features
Expand Down
2 changes: 2 additions & 0 deletions api/v1alpha1/webhook_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build integration

/*
Copyright 2021 The KubeSphere Authors.
Expand Down
22 changes: 11 additions & 11 deletions controllers/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func release(repo devopsv1alpha1.Repository, secret *v1.Secret, user string) (er
}

func getAuth(secret *v1.Secret) (auth transport.AuthMethod) {
if secret == nil {
return
}

switch secret.Type {
case v1.SecretTypeBasicAuth:
auth = &githttp.BasicAuth{
Expand Down Expand Up @@ -168,30 +172,26 @@ func clone(gitRepo, branch string, auth transport.AuthMethod, cacheDir string) (
return
}

func tagExists(tag string, r *git.Repository) bool {
func remoteTagExists(tag string, r *git.Repository) bool {
var err error
var tags storer.ReferenceIter

if tags, err = r.Tags(); err == nil {
var ref *plumbing.Reference
for ref, _ = tags.Next(); ref != nil; ref, _ = tags.Next() {
if !ref.Target().IsTag() {
continue
}

if ref.Target().String() == tag {
if !ref.Target().IsRemote() {
_ = r.DeleteTag(tag)
return false
if ref.Name().IsTag() && ref.Name().Short() == tag {
if _, err = r.TagObject(ref.Hash()); err == nil {
return true
}
return true
break
}
}
}
return false
}

func setTag(r *git.Repository, tag, message, user string) (bool, error) {
if tagExists(tag, r) {
if remoteTagExists(tag, r) {
fmt.Printf("tag %s already exists\n", tag)
return false, nil
}
Expand Down
42 changes: 42 additions & 0 deletions controllers/git_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// +build integration

package controllers

import (
"github.com/stretchr/testify/assert"
"math/rand"
"strconv"
"testing"
"time"
)

func TestRemoteTagExists(t *testing.T) {
repo, err := clone("https://gitee.com/linuxsuren/test", "master", nil, "bin/tmp")
assert.Nil(t, err)

result := remoteTagExists("v0.0.7", repo)
assert.True(t, result)

result = remoteTagExists("v0.0.8", repo)
assert.True(t, result)

def := rand.New(rand.NewSource(time.Now().UnixNano()))

// not existing
fakeTag := strconv.Itoa(def.Int())
result = remoteTagExists(fakeTag, repo)
assert.False(t, result)

result, err = setTag(repo, fakeTag, "message", "user")
assert.True(t, result)
assert.Nil(t, err)

result = remoteTagExists(fakeTag, repo)
assert.True(t, result)

err = pushTags(repo, "xx", nil)
assert.NotNil(t, err)

_, err = clone("xx://wrong url format", "xxx", nil, "bin/tmp")
assert.NotNil(t, err)
}
44 changes: 44 additions & 0 deletions controllers/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package controllers

import (
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"testing"
)

func TestGetAuth(t *testing.T) {
var secret *v1.Secret
var auth transport.AuthMethod

// secret is nil
auth = getAuth(secret)
assert.Nil(t, auth)

// empty secret
secret = &v1.Secret{}
auth = getAuth(secret)
assert.Nil(t, auth)

// basic auth
secret = &v1.Secret{
Type: v1.SecretTypeBasicAuth,
Data: map[string][]byte{
v1.BasicAuthUsernameKey: []byte("username"),
v1.BasicAuthPasswordKey: []byte("password"),
},
}
auth = getAuth(secret)
assert.NotNil(t, auth)
assert.Contains(t, auth.String(), "username")
assert.Equal(t, "http-basic-auth", auth.Name())

// ssh auth
secret = &v1.Secret{
Type: v1.SecretTypeSSHAuth,
}
auth = getAuth(secret)
assert.NotNil(t, auth)
assert.Equal(t, ssh.PublicKeysName, auth.Name())
}
2 changes: 2 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build integration

/*
Copyright 2021 The KubeSphere Authors.
Expand Down

0 comments on commit 8edf741

Please sign in to comment.