Skip to content

Commit

Permalink
style: use golint for linting
Browse files Browse the repository at this point in the history
  • Loading branch information
pjsier committed Jan 9, 2020
1 parent 4224a75 commit 47a6abe
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 95 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: install test build deploy clean
.PHONY: install test format lint build deploy clean

functions := $(shell find functions -name \*main.go | awk -F'/' '{print $$2}')

Expand All @@ -8,6 +8,12 @@ install:
test:
go test ./...

format:
test -z $$(gofmt -l .)

lint:
golint -set_exit_status ./...

build:
@for function in $(functions) ; do \
env GOOS=linux go build -ldflags="-s -w" -o bin/$$function functions/$$function/main.go ; \
Expand Down
58 changes: 29 additions & 29 deletions functions/handle_tweet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ import (
_ "github.com/jinzhu/gorm/dialects/mysql"
)

func SaveBillAndTweet(text string, bill *models.Bill, snsClient svc.SNSType) error {
billJson, err := json.Marshal(bill)
func saveBillAndTweet(text string, bill *models.Bill, snsClient svc.SNSType) error {
billJSON, err := json.Marshal(bill)
if err != nil {
return err
}
data := svc.TweetData{
Text: fmt.Sprintf("@%s %s", bill.TweetUser, text),
Params: twitter.StatusUpdateParams{InReplyToStatusID: *bill.TweetID},
}
tweetJson, err := json.Marshal(data)
tweetJSON, err := json.Marshal(data)
if err != nil {
return err
}
err = snsClient.Publish(string(tweetJson), os.Getenv("SNS_TOPIC_ARN"), "post_tweet")
err = snsClient.Publish(string(tweetJSON), os.Getenv("SNS_TOPIC_ARN"), "post_tweet")
if err != nil {
return err
}
return snsClient.Publish(string(billJson), os.Getenv("SNS_TOPIC_ARN"), "update_bill")
return snsClient.Publish(string(billJSON), os.Getenv("SNS_TOPIC_ARN"), "update_bill")
}

func HandleTweet(bill *models.Bill, db *gorm.DB, snsClient svc.SNSType) error {
func handleTweet(bill *models.Bill, db *gorm.DB, snsClient svc.SNSType) error {
var billForTweet models.Bill

if !db.Where(&models.Bill{TweetID: bill.TweetID}).Take(&billForTweet).RecordNotFound() {
Expand All @@ -46,8 +46,8 @@ func HandleTweet(bill *models.Bill, db *gorm.DB, snsClient svc.SNSType) error {
if bill.BillID == "" {
bill.Active = false
// Don't post a tweet for unmatched bill, otherwise will go for all tweets with mentions
billJson, _ := json.Marshal(bill)
return snsClient.Publish(string(billJson), os.Getenv("SNS_TOPIC_ARN"), "save_bill")
billJSON, _ := json.Marshal(bill)
return snsClient.Publish(string(billJSON), os.Getenv("SNS_TOPIC_ARN"), "save_bill")
}

var existingBill models.Bill
Expand All @@ -59,14 +59,14 @@ func HandleTweet(bill *models.Bill, db *gorm.DB, snsClient svc.SNSType) error {
if bill.URL == "" {
// Tweet that a valid bill wasn't found
bill.Active = false
return SaveBillAndTweet(
return saveBillAndTweet(
"We couldn't find a Chicago City Council bill with that ID",
bill,
snsClient,
)
}
// Tweet that the new bill is now being tracked, save
return SaveBillAndTweet(
return saveBillAndTweet(
fmt.Sprintf(
"We're now tracking Chicago City Council %s%s. You can follow along with #%s—we'll tweet when this legislation moves. %s",
billCls,
Expand All @@ -77,25 +77,25 @@ func HandleTweet(bill *models.Bill, db *gorm.DB, snsClient svc.SNSType) error {
bill,
snsClient,
)
} else {
// Tweet standard reply about already being able to follow it with hashtag
existingBill.LastTweetID = bill.LastTweetID
billCls := bill.Classification
if billCls != "" {
billCls = fmt.Sprintf("%s ", billCls)
}
return SaveBillAndTweet(
fmt.Sprintf(
"We're already tracking %s%s. You can follow along with #%s—we'll tweet when this legislation moves. %s",
billCls,
bill.GetCleanBillID(),
existingBill.BillID,
bill.URL,
),
&existingBill,
snsClient,
)
}

// Tweet standard reply about already being able to follow it with hashtag
existingBill.LastTweetID = bill.LastTweetID
billCls := bill.Classification
if billCls != "" {
billCls = fmt.Sprintf("%s ", billCls)
}
return saveBillAndTweet(
fmt.Sprintf(
"We're already tracking %s%s. You can follow along with #%s—we'll tweet when this legislation moves. %s",
billCls,
bill.GetCleanBillID(),
existingBill.BillID,
bill.URL,
),
&existingBill,
snsClient,
)
}

func handler(request events.SNSEvent) error {
Expand Down Expand Up @@ -124,7 +124,7 @@ func handler(request events.SNSEvent) error {
log.Fatal(err)
return err
}
err = HandleTweet(&bill, db, snsClient)
err = handleTweet(&bill, db, snsClient)
if err != nil {
log.Fatal(err)
return err
Expand Down
12 changes: 6 additions & 6 deletions functions/handle_tweet/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ func TestHandleTweetExits(t *testing.T) {

snsMock := new(mocks.SNSClientMock)
var bill models.Bill
var tweetId int64 = 1234
var tweetID int64 = 1234
bill = models.Bill{
BillID: "O20101",
TweetID: &tweetId,
TweetID: &tweetID,
TweetText: "O20101",
}
dbMock.ExpectQuery("SELECT (.+) FROM (.+) WHERE (.+) LIMIT 1").
WithArgs(bill.TweetID).
WillReturnRows(sqlmock.NewRows([]string{"pk", "tweet_id"}).AddRow(1, 1234))
HandleTweet(&bill, DB, snsMock)
handleTweet(&bill, DB, snsMock)
if err := dbMock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
Expand All @@ -39,16 +39,16 @@ func TestHandleTweetEmptyBillID(t *testing.T) {

snsMock := new(mocks.SNSClientMock)
var bill models.Bill
var tweetId int64 = 1
var tweetID int64 = 1
bill = models.Bill{
TweetID: &tweetId,
TweetID: &tweetID,
TweetText: "",
}
dbMock.ExpectQuery("SELECT (.+) FROM (.+) WHERE (.+) LIMIT 1").
WithArgs(bill.TweetID).
WillReturnError(gorm.ErrRecordNotFound)
snsMock.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
HandleTweet(&bill, DB, snsMock)
handleTweet(&bill, DB, snsMock)
if err := dbMock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions functions/query_bills/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func handler(request events.CloudWatchEvent) error {
for _, bill := range bills {
log.Println(bill.BillID)
// Log errors but don't exit since we can just ignore them here
billJson, err := json.Marshal(bill)
billJSON, err := json.Marshal(bill)
if err != nil {
log.Println(err)
}
err = snsClient.Publish(string(billJson), os.Getenv("SNS_TOPIC_ARN"), "update_bill")
err = snsClient.Publish(string(billJSON), os.Getenv("SNS_TOPIC_ARN"), "update_bill")
if err != nil {
log.Println(err)
}
Expand Down
20 changes: 10 additions & 10 deletions functions/query_mentions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/dghubble/go-twitter/twitter"
)

func QueryMentions(twttr svc.Twitter, snsClient svc.SNSType) error {
func queryMentions(twttr svc.Twitter, snsClient svc.SNSType) error {
tweets, err := twttr.GetMentions(&twitter.MentionTimelineParams{})
if err != nil {
return err
Expand All @@ -24,7 +24,7 @@ func QueryMentions(twttr svc.Twitter, snsClient svc.SNSType) error {
return nil
}
// Get the last tweet in the list of tweets, assign that to all
lastTweetId := tweets[len(tweets)-1].ID
lastTweetID := tweets[len(tweets)-1].ID

// Iterate through mentions, publishing each to SNS topic
for _, tweet := range tweets {
Expand All @@ -38,7 +38,7 @@ func QueryMentions(twttr svc.Twitter, snsClient svc.SNSType) error {
TweetID: &tweet.ID,
TweetText: tweet.Text,
TweetUser: tweet.User.ScreenName,
LastTweetID: &lastTweetId,
LastTweetID: &lastTweetID,
}

// Load bill data from tweet
Expand All @@ -48,15 +48,15 @@ func QueryMentions(twttr svc.Twitter, snsClient svc.SNSType) error {
if tweetBill.BillID == "" {
continue
}
billUrl, _ := tweetBill.SearchBill()
tweetBill.URL = billUrl
billURL, _ := tweetBill.SearchBill()
tweetBill.URL = billURL
title, cls, actions, _ := tweetBill.FetchBillData()
tweetBill.Title = title
tweetBill.Classification = cls
actionJson, _ := json.Marshal(actions)
tweetBill.Data = string(actionJson)
tweetBillJson, _ := json.Marshal(tweetBill)
err = snsClient.Publish(string(tweetBillJson), os.Getenv("SNS_TOPIC_ARN"), "handle_tweet")
actionJSON, _ := json.Marshal(actions)
tweetBill.Data = string(actionJSON)
tweetBillJSON, _ := json.Marshal(tweetBill)
err = snsClient.Publish(string(tweetBillJSON), os.Getenv("SNS_TOPIC_ARN"), "handle_tweet")
if err != nil {
return err
}
Expand All @@ -65,7 +65,7 @@ func QueryMentions(twttr svc.Twitter, snsClient svc.SNSType) error {
}

func handler(request events.CloudWatchEvent) error {
err := QueryMentions(svc.NewTwitterClient(), svc.NewSNSClient())
err := queryMentions(svc.NewTwitterClient(), svc.NewSNSClient())
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions functions/query_mentions/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestQueryMentionsIgnoresEmptyBillID(t *testing.T) {
snsMock := new(mocks.SNSClientMock)
twttrMock.On("GetMentions", mock.Anything).Return(tweets, nil)
snsMock.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
QueryMentions(twttrMock, snsMock)
queryMentions(twttrMock, snsMock)
snsMock.AssertNotCalled(t, "Publish", mock.Anything, mock.Anything, "handle_tweet")
}

Expand All @@ -40,7 +40,7 @@ func TestQueryMentionsIgnoresOldTweet(t *testing.T) {
snsMock := new(mocks.SNSClientMock)
twttrMock.On("GetMentions", mock.Anything).Return(tweets, nil)
snsMock.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
QueryMentions(twttrMock, snsMock)
queryMentions(twttrMock, snsMock)
snsMock.AssertNotCalled(t, "Publish", mock.Anything, mock.Anything, "handle_tweet")
}

Expand All @@ -58,6 +58,6 @@ func TestQueryMentionsTweetsBill(t *testing.T) {
snsMock := new(mocks.SNSClientMock)
twttrMock.On("GetMentions", mock.Anything).Return(tweets, nil)
snsMock.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
QueryMentions(twttrMock, snsMock)
queryMentions(twttrMock, snsMock)
snsMock.AssertCalled(t, "Publish", mock.Anything, mock.Anything, "handle_tweet")
}
20 changes: 10 additions & 10 deletions functions/update_bill/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ import (
"github.com/aws/aws-lambda-go/lambda"
)

func UpdateBill(bill models.Bill, actions []models.LegistarAction, snsClient svc.SNSType) error {
func updateBill(bill models.Bill, actions []models.LegistarAction, snsClient svc.SNSType) error {
billActions := bill.GetActions()

// If the bill is new or has changed tweet it out
if bill.NextRun == nil || len(actions) > len(billActions) {
actionJson, err := json.Marshal(actions)
actionJSON, err := json.Marshal(actions)
if err != nil {
return err
}
bill.Data = string(actionJson)
billUrl := bill.GetTweetURL()
data := svc.TweetData{Text: bill.CreateTweet(billUrl)}
tweetJson, err := json.Marshal(data)
bill.Data = string(actionJSON)
billURL := bill.GetTweetURL()
data := svc.TweetData{Text: bill.CreateTweet(billURL)}
tweetJSON, err := json.Marshal(data)
if err != nil {
return err
}
err = snsClient.Publish(string(tweetJson), os.Getenv("SNS_TOPIC_ARN"), "post_tweet")
err = snsClient.Publish(string(tweetJSON), os.Getenv("SNS_TOPIC_ARN"), "post_tweet")
if err != nil {
return err
}
}
bill.SetNextRun()
billJson, err := json.Marshal(bill)
billJSON, err := json.Marshal(bill)
if err != nil {
return err
}
// Return potential errors from saving last, because if the tweet failed then it will
// still be retried if there's a difference from what's in the database
return snsClient.Publish(string(billJson), os.Getenv("SNS_TOPIC_ARN"), "save_bill")
return snsClient.Publish(string(billJSON), os.Getenv("SNS_TOPIC_ARN"), "save_bill")
}

func handler(request events.SNSEvent) error {
Expand All @@ -67,7 +67,7 @@ func handler(request events.SNSEvent) error {
bill.Title = title
bill.Classification = cls

err = UpdateBill(bill, actions, snsClient)
err = updateBill(bill, actions, snsClient)
// Only log this error since it just prevented
if err != nil {
snsClient.Publish(message, os.Getenv("SNS_TOPIC_ARN"), "update_bill")
Expand Down
6 changes: 3 additions & 3 deletions functions/update_bill/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestUpdateBillIgnoresNoChanges(t *testing.T) {
Data: `[]`,
}
snsMock.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
UpdateBill(bill, []models.LegistarAction{}, snsMock)
updateBill(bill, []models.LegistarAction{}, snsMock)
snsMock.AssertCalled(t, "Publish", mock.Anything, mock.Anything, "save_bill")
}

Expand All @@ -29,7 +29,7 @@ func TestUpdateBillNilNextRunSendsTweet(t *testing.T) {
Data: `[]`,
}
snsMock.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
UpdateBill(bill, []models.LegistarAction{}, snsMock)
updateBill(bill, []models.LegistarAction{}, snsMock)
snsMock.AssertExpectations(t)
snsMock.AssertNumberOfCalls(t, "Publish", 2)
}
Expand All @@ -47,7 +47,7 @@ func TestUpdateBillNewActionsSendsTweet(t *testing.T) {
models.LegistarAction{Date: actionDate, Action: "introduction", Actor: "Test"},
}
snsMock.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
UpdateBill(bill, actions, snsMock)
updateBill(bill, actions, snsMock)
snsMock.AssertExpectations(t)
snsMock.AssertNumberOfCalls(t, "Publish", 2)
}
2 changes: 2 additions & 0 deletions pkg/mocks/sns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"github.com/stretchr/testify/mock"
)

// SNSClientMock is a mock for SNS
type SNSClientMock struct {
mock.Mock
}

// Publish mocks publishing on SNS
func (m *SNSClientMock) Publish(message string, topicArn string, feed string) error {
m.Called(message, topicArn, feed)
return nil
Expand Down
3 changes: 3 additions & 0 deletions pkg/mocks/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import (
"github.com/stretchr/testify/mock"
)

// TwitterMock is a struct for mocking the Twitter service
type TwitterMock struct {
mock.Mock
}

// PostTweet mocks posting a tweet on Twitter
func (m *TwitterMock) PostTweet(tweet string, params *twitter.StatusUpdateParams) error {
m.Called(tweet, params)
return nil
}

// GetMentions mocks GetMentions on the Twitter service
func (m *TwitterMock) GetMentions(params *twitter.MentionTimelineParams) ([]twitter.Tweet, error) {
args := m.Called(params)
tweets := args.Get(0).([]twitter.Tweet)
Expand Down
Loading

0 comments on commit 47a6abe

Please sign in to comment.