Skip to content

Commit c14eb01

Browse files
authored
Feat: Pipeline Cron Trigger (#90)
## What * Add resource for Pipeline Cron Trigger * Fix Terraform Acceptance tests * Add Acceptance Tests to CI * Add release automation (release drafter, labeler, automatic changelog drafting) * GH Repo best practices (PULL_REQUEST_TEMPLATE, CODEOWNERS, etc) * Misc documentation fixes ## Why * Only git triggers are currently supported. * Triggers should be a standalone resource. The next minor release (`0.2.0`) will support remaining standalone triggers (registry, helm, git). ## Notes * Closes #39 * Closes #88 * #84 Co-authored-by: Yonatan Koren <[email protected]> Co-authored-by: korenyoni <[email protected]>
1 parent 6225574 commit c14eb01

37 files changed

+1210
-200
lines changed

.github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @codefresh-io/devops @yaroslav-codefresh @denis-codefresh

.github/PULL_REQUEST_TEMPLATE.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## What
2+
3+
## Why
4+
5+
## Notes

.github/labeler.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
source:
2+
- 'client/**/*'
3+
- 'codefresh/**/*'
4+
- '*.go'
5+
- '*.mod'
6+
- '*.sum'
7+
8+
docs:
9+
- README.md
10+
- docs/**/*
11+
- examples/**/*
12+
- tf_modules/**/*
13+
- CHANGELOG.md
14+
15+
automation:
16+
- scripts/**/*
17+
- .github/**/*
18+
- codefresh.yml
19+
- .goreleaser.yml
20+
- GNUmakefile

.github/release-drafter.yaml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name-template: "v$RESOLVED_VERSION"
2+
tag-template: "v$RESOLVED_VERSION"
3+
categories:
4+
- title: "🚀 Features"
5+
labels:
6+
- "feature"
7+
- "enhancement"
8+
- title: "🐛 Bug Fixes"
9+
labels:
10+
- "fix"
11+
- "bugfix"
12+
- "bug"
13+
- title: "🧰 Maintenance"
14+
label: "chore"
15+
change-template: "- $TITLE @$AUTHOR (#$NUMBER)"
16+
change-title-escapes: '\<*_&'
17+
version-resolver:
18+
major:
19+
labels:
20+
- "major"
21+
minor:
22+
labels:
23+
- "minor"
24+
- "feature"
25+
- "enhancement"
26+
patch:
27+
labels:
28+
- "patch"
29+
- "fix"
30+
- "bugfix"
31+
- "bug"
32+
default: patch
33+
template: |
34+
## Changes
35+
$CHANGES
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: "Changelog from Release"
2+
3+
on:
4+
release:
5+
types: [published, released]
6+
7+
jobs:
8+
update_changelog:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
with:
13+
ref: master
14+
- uses: rhysd/changelog-from-release/action@v3
15+
with:
16+
file: CHANGELOG.md
17+
github_token: ${{ secrets.GITHUB_TOKEN }}
18+
commit_summary_template: 'Update CHANGELOG.md for %s'

.github/workflows/draft-release.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Draft Release
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
types: [opened, reopened, synchronize]
9+
10+
jobs:
11+
update_release_draft:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: release-drafter/release-drafter@v5
15+
env:
16+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17+
with:
18+
publish: false
19+
config-name: release-drafter.yaml

.github/workflows/update-labels.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: "Pull Request Labeler"
2+
on:
3+
- pull_request_target
4+
5+
jobs:
6+
triage:
7+
permissions:
8+
contents: read
9+
pull-requests: write
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/labeler@v4
13+
with:
14+
repo-token: "${{ secrets.GITHUB_TOKEN }}"

.goreleaser.yml

-8
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,4 @@ signs:
3232
- "${signature}"
3333
- "--detach-sign"
3434
- "${artifact}"
35-
release:
36-
# draft: true
37-
changelog:
38-
sort: asc
39-
filters:
40-
exclude:
41-
- '^docs:'
42-
- '^test:'
4335

CHANGELOG.md

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
# Changelog
21

3-
## [1.0.0 (Unreleased)](https://github.com/codefresh-io/terraform-provider-codefresh/tree/HEAD)
4-
5-
[Full Changelog](https://github.com/codefresh-io/terraform-provider-codefresh/compare/17fe7e1b0003bda492682d06ba1917cae91d6faf...HEAD)

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ terraform {
4848
```
4949

5050

51-
## [Documentations](./docs)
51+
## [Documentation](./docs)
5252

5353
## [Examples](./examples)
5454

@@ -77,9 +77,14 @@ export CODEFRESH_API_KEY='xyz'
7777

7878
## Testing the Provider
7979

80+
```bash
81+
export TF_ACC="test"
82+
export CODEFRESH_API_KEY=[YOUR API TOKEN]
83+
go test -v ./...
84+
```
85+
8086
## License
8187

82-
Copyright 2020 Codefresh.
88+
Copyright 2022 Codefresh.
8389

8490
The Codefresh Provider is available under [MPL2.0 license](./LICENSE).
85-

client/hermes_trigger.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package client
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type HermesTrigger struct {
8+
Event string `json:"event,omitempty"`
9+
PipelineID string `json:"pipeline,omitempty"`
10+
EventData EventData `json:"event-data,omitempty"`
11+
}
12+
13+
type EventData struct {
14+
Uri string `json:"uri"`
15+
Type string `json:"type"`
16+
Kind string `json:"kind"`
17+
Account string `json:"account"`
18+
Secret string `json:"secret"`
19+
}
20+
21+
func (client *Client) GetHermesTriggerByEventAndPipeline(event string, pipeline string) (*HermesTrigger, error) {
22+
23+
fullPath := fmt.Sprintf("/hermes/triggers/event/%s", UriEncodeEvent(event))
24+
opts := RequestOptions{
25+
Path: fullPath,
26+
Method: "GET",
27+
}
28+
29+
resp, err := client.RequestAPI(&opts)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
var hermesTriggerList []HermesTrigger
35+
36+
err = DecodeResponseInto(resp, &hermesTriggerList)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
var hermesTrigger HermesTrigger
42+
for _, trigger := range hermesTriggerList {
43+
if trigger.PipelineID == pipeline {
44+
hermesTrigger = trigger
45+
}
46+
}
47+
if hermesTrigger.Event == "" {
48+
return nil, fmt.Errorf("No trigger found for event %s and pipeline %s", event, pipeline)
49+
}
50+
51+
return &hermesTrigger, nil
52+
}
53+
54+
func (client *Client) CreateHermesTriggerByEventAndPipeline(event string, pipeline string) error {
55+
56+
fullPath := fmt.Sprintf("/hermes/triggers/%s/%s", UriEncodeEvent(event), pipeline)
57+
opts := RequestOptions{
58+
Path: fullPath,
59+
Method: "POST",
60+
}
61+
62+
_, err := client.RequestAPI(&opts)
63+
return err
64+
}
65+
66+
func (client *Client) DeleteHermesTriggerByEventAndPipeline(event string, pipeline string) error {
67+
fullPath := fmt.Sprintf("/hermes/triggers/%s/%s", UriEncodeEvent(event), pipeline)
68+
opts := RequestOptions{
69+
Path: fullPath,
70+
Method: "DELETE",
71+
}
72+
73+
_, err := client.RequestAPI(&opts)
74+
75+
if err != nil {
76+
return err
77+
}
78+
79+
return nil
80+
}

client/hermes_trigger_event.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package client
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type HermesTriggerEvent struct {
8+
Type string `json:"type,omitempty"`
9+
Kind string `json:"kind,omitempty"`
10+
Filter string `json:"filter,omitempty"`
11+
Secret string `json:"secret,omitempty"`
12+
Values map[string]string `json:"values,omitempty"`
13+
}
14+
15+
func (client *Client) GetHermesTriggerEvent(event string) (*HermesTriggerEvent, error) {
16+
fullPath := fmt.Sprintf("/hermes/triggers/%s", UriEncodeEvent(event))
17+
18+
opts := RequestOptions{
19+
Path: fullPath,
20+
Method: "GET",
21+
}
22+
23+
resp, err := client.RequestAPI(&opts)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
var hermesTriggerEvent HermesTriggerEvent
29+
err = DecodeResponseInto(resp, &hermesTriggerEvent)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
return &hermesTriggerEvent, nil
35+
}
36+
37+
func (client *Client) CreateHermesTriggerEvent(event *HermesTriggerEvent) (string, error) {
38+
39+
body, err := EncodeToJSON(event)
40+
if err != nil {
41+
return "", err
42+
}
43+
44+
fullPath := "/hermes/events/"
45+
opts := RequestOptions{
46+
Path: fullPath,
47+
Method: "POST",
48+
Body: body,
49+
}
50+
51+
resp, err := client.RequestAPI(&opts)
52+
53+
var eventString string
54+
err = DecodeResponseInto(resp, &eventString)
55+
if err != nil {
56+
return "", err
57+
}
58+
59+
return eventString, err
60+
}

client/user.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ func (client *Client) DeleteUser(userName string) error {
264264
Method: "DELETE",
265265
}
266266

267-
// The API will return a 500 error if the user cannot be found
268-
// In this case the DeleteUser function should not return an error.
269-
// Return error only if the body of the return message does not contain "User does not exist"
270-
res, err := client.RequestAPI(&opts)
271-
if err != nil {
272-
if !strings.Contains(string(res), "User does not exist") {
273-
return err
274-
}
275-
}
267+
// The API will return a 500 error if the user cannot be found
268+
// In this case the DeleteUser function should not return an error.
269+
// Return error only if the body of the return message does not contain "User does not exist"
270+
res, err := client.RequestAPI(&opts)
271+
if err != nil {
272+
if !strings.Contains(string(res), "User does not exist") {
273+
return err
274+
}
275+
}
276276

277277
return nil
278278
}

client/utils.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package client
22

3+
import (
4+
"net/url"
5+
"strings"
6+
)
7+
38
// Variable spec
49
type Variable struct {
510
Key string `json:"key"`
@@ -19,3 +24,14 @@ func FindInSlice(slice []string, val string) bool {
1924
}
2025
return false
2126
}
27+
28+
func uriEncode(path string) string {
29+
replacer := strings.NewReplacer("+", "%20", "%2A", "*") // match Javascript's encodeURIComponent()
30+
return replacer.Replace(url.QueryEscape(path))
31+
}
32+
33+
func UriEncodeEvent(event string) string {
34+
// The following is odd, but it's intentional. The event is URI encoded twice because
35+
// the Codefresh API expects it to be encoded twice.
36+
return uriEncode(uriEncode(event))
37+
}

codefresh.yml

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,22 @@ steps:
1616
go_fmt:
1717
title: 'Formatting'
1818
stage: test
19-
image: goreleaser/goreleaser:v1.6.3
19+
image: goreleaser/goreleaser:v1.11.3
2020
commands:
2121
- go fmt
22+
23+
go_test:
24+
title: 'Run tests'
25+
stage: test
26+
image: goreleaser/goreleaser:v1.11.3
27+
environment:
28+
- TF_ACC="test"
29+
commands:
30+
- go test -v ./...
31+
retry:
32+
maxAttempts: 3
33+
delay: 5
34+
exponentialFactor: 2
2235

2336
prepare_env_vars:
2437
title: 'Preparing environment variables...'
@@ -40,7 +53,7 @@ steps:
4053

4154
release_binaries:
4255
title: Create release in Github
43-
image: goreleaser/goreleaser:v1.6.3
56+
image: goreleaser/goreleaser:v1.11.3
4457
stage: release
4558
environment:
4659
- GPG_FINGERPRINT=${{GPG_FINGERPRINT}}

0 commit comments

Comments
 (0)