From fae783dfe4e524e5571c73c06fbc7c10bef37feb Mon Sep 17 00:00:00 2001
From: Greg Furman <gregfurman99@gmail.com>
Date: Sat, 27 Apr 2024 11:47:22 +0200
Subject: [PATCH] Add github build and release workflows; Fix status logging
 when polling

---
 .github/workflows/build.yml   | 38 ++++++++++++++++++
 .github/workflows/release.yml | 76 +++++++++++++++++++++++++++++++++++
 main.go                       |  4 +-
 3 files changed, 117 insertions(+), 1 deletion(-)
 create mode 100644 .github/workflows/build.yml
 create mode 100644 .github/workflows/release.yml

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
new file mode 100644
index 0000000..6127425
--- /dev/null
+++ b/.github/workflows/build.yml
@@ -0,0 +1,38 @@
+name: Go package
+
+on: [push]
+
+jobs:
+  golangci:
+    name: lint
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v4
+      - uses: actions/setup-go@v5
+        with:
+          go-version: '1.21'
+    
+
+  build:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v3
+
+      - name: Set up Go
+        uses: actions/setup-go@v4
+        with:
+          go-version: 1.21.x
+
+      - name: Lint
+        uses: golangci/golangci-lint-action@v5
+        with:
+          version: v1.57
+
+      - name: Install Dependencies
+        run: go get .
+
+      - name: Test
+        run: go test -v ./...
+
+      - name: Build
+        run: go build -o service main.go
\ No newline at end of file
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
new file mode 100644
index 0000000..af00c88
--- /dev/null
+++ b/.github/workflows/release.yml
@@ -0,0 +1,76 @@
+name: tag-release
+
+on:
+  push:
+    tags:
+      - 'v[0-9]+.[0-9]+.[0-9]+'
+
+jobs:
+  tag:
+    if: ${{ github.repository == 'gregfurman/pipescope' }}
+    runs-on: ubuntu-latest
+    permissions:
+      contents: write
+    steps:
+      - uses: actions/checkout@v3
+        with:
+          fetch-depth: 0
+      - run: /usr/bin/git config --global user.email actions@github.com
+      - run: /usr/bin/git config --global user.name 'GitHub Actions Release Tagger'
+      - run: hack/tag-release.sh
+        id: tag_release
+    outputs:
+      release_tag: ${{ steps.tag_release.outputs.release_tag }}
+
+  release:
+    needs: tag
+    runs-on: ubuntu-latest
+    permissions:
+        contents: write
+    steps:
+    - uses: actions/checkout@v3
+
+    - name: Get previous tag
+      id: prev_tag
+      run: echo "::set-output name=PREV_TAG::$(git describe --abbrev=0 --tags $(git rev-list --tags --skip=1 --max-count=1))"
+
+    - name: Get commit log
+      id: commit_log
+      run: echo "::set-output name=COMMIT_LOG::$(git log --pretty=format:'- %s (%h)' ${PREV_TAG}..HEAD)"
+
+    - name: Setup Golang
+      uses: actions/setup-go@v2
+      with:
+        go-version: 1.21.x
+
+    - name: Build Go Binary
+      run: |
+        GOOS=linux GOARCH=amd64 go build -o pipescope main.go
+    - uses: actions/upload-artifact@v2
+      env:
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+      with:
+        name: ecr-credential-provider
+        path: ecr-credential-provider
+
+    - uses: actions/create-release@v1
+      id: create_release
+      env:
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+      with:
+        tag_name: ${{ needs.tag.outputs.release_tag }}
+        release_name: ${{ needs.tag.outputs.release_tag }}
+        body: |
+          Changes in this release:
+            ${{ steps.commit_log.outputs.COMMIT_LOG }}
+        draft: false
+        prerelease: false
+    
+    - uses: actions/upload-release-asset@v1
+      env:
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+      with:
+        upload_url: ${{ steps.create_release.outputs.upload_url }}
+        asset_path: ./pipescope
+        asset_name: pipescope
+        asset_content_type: application/octet-stream
\ No newline at end of file
diff --git a/main.go b/main.go
index 449f212..cb963de 100644
--- a/main.go
+++ b/main.go
@@ -68,6 +68,7 @@ func run(svc *checker.Service, pollFrequency time.Duration) error {
 	if err != nil {
 		return fmt.Errorf("checker Service GET pipeline failed: %w", err)
 	}
+
 	status := pipeline.Status
 
 	slog.Group("pipeline")
@@ -80,9 +81,10 @@ func run(svc *checker.Service, pollFrequency time.Duration) error {
 	)
 
 	logger.Info(fmt.Sprintf("Polled Pipeline [status=%s]", pipeline.Status))
+
 	statusCh, _ := svc.PollPipelineStatus(pipeline.ProjectID, pipeline.ID, pollFrequency)
 	for s := range statusCh {
-		if status == "" {
+		if s == "" {
 			continue
 		}