Skip to content

Commit

Permalink
Merge pull request #197 from nicolasbock/new-scope
Browse files Browse the repository at this point in the history
New Salesforce tokens
  • Loading branch information
nicolasbock authored Sep 13, 2024
2 parents 0fa3247 + c671d3e commit d1dca66
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ docker-compose: docker-build
BRANCH=$(shell git branch --show-current | sed -e 's:/:-:g') $${DOCKER_COMPOSE} up --force-recreate --build

.PHONY: devel
devel: athena-monitor athena-processor docker-build docker-compose
devel: install docker-build docker-compose

.PHONY: common-docker monitor processor
docker-build: athena-monitor docker-build-monitor athena-processor docker-build-processor docker-build-debug-container
.PHONY: docker-build monitor processor
docker-build: install docker-build-monitor docker-build-processor docker-build-debug-container

.PHONY: docker-build-monitor docker-build-processor
docker-build-monitor docker-build-processor: docker-build-%:
Expand Down Expand Up @@ -84,7 +84,7 @@ test:
install: build
rm -rf build
mkdir build
cp athena-monitor athena-processor build/
cp --verbose athena-monitor athena-processor build/

.PHONY: docs
docs:
Expand Down
20 changes: 19 additions & 1 deletion pkg/common/salesforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (sf *BaseSalesforceClient) GetCaseByNumber(number string) (*Case, error) {
}

func (sf *BaseSalesforceClient) PostComment(caseId, body string, isPublic bool) *simpleforce.SObject {
log.Debugf("Posting comment for case %s", caseId)
return sf.SObject("CaseComment").
Set("ParentId", caseId).
Set("CommentBody", html.UnescapeString(body)).
Expand All @@ -90,15 +91,32 @@ func (sf *BaseSalesforceClient) PostComment(caseId, body string, isPublic bool)
}

func (sf *BaseSalesforceClient) PostChatter(caseId, body string, isPublic bool) *simpleforce.SObject {
log.Debugf("Posting comment to chatter for case %s", caseId)
visibility := "InternalUsers"
if isPublic {
visibility = "AllUsers"
}
return sf.SObject("FeedItem").
newComment := sf.SObject("FeedItem").
Set("ParentId", caseId).
Set("Body", body).
Set("Visibility", visibility).
Create()
if newComment != nil {
log.Debugf("Successfully posted comment as FeedItem to case %s", caseId)
return newComment
}
log.Warnf("Unable to post comment as FeedItem object to case %s", caseId)
newComment = sf.SObject("CaseFeed").
Set("ParentId", caseId).
Set("Body", body).
Set("Visibility", visibility).
Create()
if newComment != nil {
log.Debugf("Successfully posted comment as CaseFeed object to case %s", caseId)
return newComment
}
log.Errorf("Unable to post comment as either FeedItem or CaseFeed object for %s", caseId)
return newComment
}

func GetCaseNumberFromFilename(filename string) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func NewProcessor() Processor {
}

type SalesForce struct {
EnableChatter bool `yaml:"enable-chatter"`
Endpoint string `yaml:"endpoint"`
Username string `yaml:"username"`
MaxCommentLength int `yaml:"max-comment-length"`
Password string `yaml:"password"`
SecurityToken string `yaml:"security-token"`
MaxCommentLength int `yaml:"max-comment-length"`
EnableChatter bool `yaml:"enable-chatter"`
Username string `yaml:"username"`
}

func NewSalesForce() SalesForce {
Expand Down
3 changes: 3 additions & 0 deletions pkg/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ func (m *Monitor) GetMatchingProcessorByFile(files []db.File) (map[string][]db.F
for _, file := range files {
var processors []string

log.Debugf("Analyzing file %s", file.Path)
caseNumber, err := common.GetCaseNumberFromFilename(file.Path)
if err == nil {
sfCase, err = salesforceClient.GetCaseByNumber(caseNumber)
if err != nil {
log.Warningf("Failed to get a case from number: '%s'", caseNumber)
} else {
log.Debugf("Found customer '%s' for case number %s", sfCase.Customer, caseNumber)
}
} else {
log.Warningf("Failed to identify case from filename '%s': %s", file.Path, err)
Expand Down
16 changes: 11 additions & 5 deletions pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ func (p *Processor) BatchSalesforceComments(ctx *context.Context, interval time.
log.Infof("Processing comment for case %s", caseId)
commentChunks := splitComment(renderedComment, p.Config.Salesforce.MaxCommentLength)

wasPosted := true
for i, chunk := range commentChunks {
var chunkHeader string
if len(commentChunks) > 1 {
Expand All @@ -523,16 +524,21 @@ func (p *Processor) BatchSalesforceComments(ctx *context.Context, interval time.
}
if comment == nil {
log.Errorf("Failed to post comment to case id: %s", caseId)
wasPosted = false
continue
}
}

log.Infof("Successfully posted comment on case %s for %d reports", caseId, len(reports))
for _, report := range reports {
report.Commented = true
p.Db.Save(report)
if wasPosted {
log.Infof("Successfully posted comment on case %s for %d reports", caseId, len(reports))
for _, report := range reports {
report.Commented = true
p.Db.Save(report)
}
reportMap = nil
} else {
log.Errorf("Could not post comment to case id: %s", caseId)
}
reportMap = nil
}
}
}
Expand Down

0 comments on commit d1dca66

Please sign in to comment.