-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from is2ei/support-typetalk
Support Typetalk
- Loading branch information
Showing
13 changed files
with
421 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ ignore: | |
- "main.go" | ||
- "notifier/github/github.go" | ||
- "notifier/slack/slack.go" | ||
- "notifier/typetalk/typetalk.go" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package typetalk | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/mercari/tfnotify/terraform" | ||
typetalk "github.com/nulab/go-typetalk/typetalk/v1" | ||
) | ||
|
||
// EnvToken is Typetalk API Token | ||
const EnvToken = "TYPETALK_TOKEN" | ||
|
||
// EnvTopicID is Typetalk topic ID | ||
const EnvTopicID = "TYPETALK_TOPIC_ID" | ||
|
||
// Client represents Typetalk API client. | ||
type Client struct { | ||
*typetalk.Client | ||
Config Config | ||
common service | ||
Notify *NotifyService | ||
API API | ||
} | ||
|
||
// Config is a configuration for Typetalk Client | ||
type Config struct { | ||
Token string | ||
Title string | ||
TopicID string | ||
Message string | ||
CI string | ||
Parser terraform.Parser | ||
Template terraform.Template | ||
} | ||
|
||
type service struct { | ||
client *Client | ||
} | ||
|
||
// NewClient returns Client initialized with Config | ||
func NewClient(cfg Config) (*Client, error) { | ||
token := os.ExpandEnv(cfg.Token) | ||
if token == EnvToken { | ||
token = os.Getenv(EnvToken) | ||
} | ||
if token == "" { | ||
return &Client{}, errors.New("Typetalk token is missing") | ||
} | ||
|
||
topicIDString := os.ExpandEnv(cfg.TopicID) | ||
if topicIDString == EnvTopicID { | ||
topicIDString = os.Getenv(EnvTopicID) | ||
} | ||
if topicIDString == "" { | ||
return &Client{}, errors.New("Typetalk topic ID is missing") | ||
} | ||
|
||
topicID, err := strconv.Atoi(topicIDString) | ||
if err != nil { | ||
return &Client{}, errors.New("Typetalk topic ID is not numeric value") | ||
} | ||
|
||
client := typetalk.NewClient(nil) | ||
client.SetTypetalkToken(token) | ||
c := &Client{ | ||
Config: cfg, | ||
Client: client, | ||
} | ||
c.common.client = c | ||
c.Notify = (*NotifyService)(&c.common) | ||
c.API = &Typetalk{ | ||
Client: client, | ||
TopicID: topicID, | ||
} | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package typetalk | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestNewClient(t *testing.T) { | ||
typetalkToken := os.Getenv(EnvToken) | ||
defer func() { | ||
os.Setenv(EnvToken, typetalkToken) | ||
}() | ||
os.Setenv(EnvToken, "") | ||
|
||
testCases := []struct { | ||
config Config | ||
envToken string | ||
expect string | ||
}{ | ||
{ | ||
// specify directly | ||
config: Config{Token: "abcdefg", TopicID: "12345"}, | ||
envToken: "", | ||
expect: "", | ||
}, | ||
{ | ||
// specify via env but not to be set env (part 1) | ||
config: Config{Token: "TYPETALK_TOKEN", TopicID: "12345"}, | ||
envToken: "", | ||
expect: "Typetalk token is missing", | ||
}, | ||
{ | ||
// specify via env (part 1) | ||
config: Config{Token: "TYPETALK_TOKEN", TopicID: "12345"}, | ||
envToken: "abcdefg", | ||
expect: "", | ||
}, | ||
{ | ||
// specify via env but not to be set env (part 2) | ||
config: Config{Token: "$TYPETALK_TOKEN", TopicID: "12345"}, | ||
envToken: "", | ||
expect: "typetalk token is missing", | ||
}, | ||
{ | ||
// specify via env (part 2) | ||
config: Config{Token: "$TYPETALK_TOKEN", TopicID: "12345"}, | ||
envToken: "abcdefg", | ||
expect: "", | ||
}, | ||
{ | ||
// no specification (part 1) | ||
config: Config{TopicID: "12345"}, | ||
envToken: "", | ||
expect: "Typetalk token is missing", | ||
}, | ||
{ | ||
// no specification (part 2) | ||
config: Config{TopicID: "12345"}, | ||
envToken: "abcdefg", | ||
expect: "Typetalk token is missing", | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
os.Setenv(EnvToken, testCase.envToken) | ||
_, err := NewClient(testCase.config) | ||
if err == nil { | ||
continue | ||
} | ||
if err.Error() != testCase.expect { | ||
t.Errorf("got %q but want %q", err.Error(), testCase.expect) | ||
} | ||
} | ||
} |
Oops, something went wrong.