github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/integration/integration_github_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  // can be executed with
     5  // go test -v -tags integration -run TestGitHubIntegration ./integration/...
     6  
     7  package main
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  
    17  	"github.com/SAP/jenkins-library/pkg/command"
    18  	pipergithub "github.com/SAP/jenkins-library/pkg/github"
    19  	"github.com/SAP/jenkins-library/pkg/piperenv"
    20  )
    21  
    22  func TestGitHubIntegrationPiperPublishRelease(t *testing.T) {
    23  	t.Parallel()
    24  	token := os.Getenv("PIPER_INTEGRATION_GITHUB_TOKEN")
    25  	if len(token) == 0 {
    26  		t.Fatal("No GitHub token maintained")
    27  	}
    28  	owner := os.Getenv("PIPER_INTEGRATION_GITHUB_OWNER")
    29  	if len(owner) == 0 {
    30  		owner = "OliverNocon"
    31  	}
    32  	repository := os.Getenv("PIPER_INTEGRATION_GITHUB_REPOSITORY")
    33  	if len(repository) == 0 {
    34  		repository = "piper-integration"
    35  	}
    36  	dir := t.TempDir()
    37  
    38  	testAsset := filepath.Join(dir, "test.txt")
    39  	err := os.WriteFile(testAsset, []byte("Test"), 0644)
    40  	assert.NoError(t, err, "Error when writing temporary file")
    41  	test2Asset := filepath.Join(dir, "test2.txt")
    42  	err = os.WriteFile(test2Asset, []byte("Test"), 0644)
    43  	assert.NoError(t, err, "Error when writing temporary file")
    44  
    45  	t.Run("test single asset - success", func(t *testing.T) {
    46  		//prepare pipeline environment
    47  		now := time.Now()
    48  		piperenv.SetResourceParameter(filepath.Join(dir, ".pipeline"), "commonPipelineEnvironment", "artifactVersion", now.Format("20060102150405"))
    49  
    50  		cmd := command.Command{}
    51  		cmd.SetDir(dir)
    52  
    53  		piperOptions := []string{
    54  			"githubPublishRelease",
    55  			"--owner",
    56  			owner,
    57  			"--repository",
    58  			repository,
    59  			"--token",
    60  			token,
    61  			"--assetPath",
    62  			testAsset,
    63  			"--noTelemetry",
    64  		}
    65  
    66  		err = cmd.RunExecutable(getPiperExecutable(), piperOptions...)
    67  		assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions)
    68  	})
    69  	t.Run("test multiple assets - success", func(t *testing.T) {
    70  		//prepare pipeline environment
    71  		now := time.Now()
    72  		piperenv.SetResourceParameter(filepath.Join(dir, ".pipeline"), "commonPipelineEnvironment", "artifactVersion", now.Format("20060102150405"))
    73  
    74  		cmd := command.Command{}
    75  		cmd.SetDir(dir)
    76  
    77  		piperOptions := []string{
    78  			"githubPublishRelease",
    79  			"--owner",
    80  			owner,
    81  			"--repository",
    82  			repository,
    83  			"--token",
    84  			token,
    85  			"--assetPathList",
    86  			testAsset,
    87  			"--assetPathList",
    88  			test2Asset,
    89  			"--noTelemetry",
    90  		}
    91  
    92  		err = cmd.RunExecutable(getPiperExecutable(), piperOptions...)
    93  		assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions)
    94  	})
    95  }
    96  
    97  func TestGitHubIntegrationFetchCommitStatistics(t *testing.T) {
    98  	t.Parallel()
    99  	// prepare
   100  	token := os.Getenv("PIPER_INTEGRATION_GITHUB_TOKEN")
   101  	if len(token) == 0 {
   102  		t.Fatal("No GitHub token maintained")
   103  	}
   104  
   105  	owner := os.Getenv("PIPER_INTEGRATION_GITHUB_OWNER")
   106  	if len(owner) == 0 {
   107  		owner = "OliverNocon"
   108  	}
   109  	repository := os.Getenv("PIPER_INTEGRATION_GITHUB_REPOSITORY")
   110  	if len(repository) == 0 {
   111  		repository = "piper-integration"
   112  	}
   113  	// test
   114  	result, err := pipergithub.FetchCommitStatistics(&pipergithub.FetchCommitOptions{
   115  		Owner: owner, Repository: repository, APIURL: "https://api.github.com", Token: token, SHA: "3601ed6"})
   116  
   117  	// assert
   118  	assert.NoError(t, err)
   119  	assert.Equal(t, 2, result.Additions)
   120  	assert.Equal(t, 0, result.Deletions)
   121  	assert.Equal(t, 2, result.Total)
   122  	assert.Equal(t, 1, result.Files)
   123  }