Skip to content

Commit

Permalink
Merge pull request zapier#35 from ahinh43/previous-runs-url-markdown-…
Browse files Browse the repository at this point in the history
…tabling

Pretty up previous TFC runs URLs list via markdown tables instead of just a list of URLs
  • Loading branch information
davidwin93 authored Aug 10, 2023
2 parents 65b18e0 + 9e80812 commit 780416e
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 11 deletions.
3 changes: 3 additions & 0 deletions pkg/comment_formatter/tfc_status_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,7 @@ var needToApplyFullWorkSpace = `

const OLD_RUN_BLOCK = `
### Previous TFC URLS
| Run ID | Status | Created at |
| ------ | ------ | ---------- |
%s`
2 changes: 1 addition & 1 deletion pkg/terraform_plan/templates/plan_output.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{- end}}
{{ end }}

:airplane_arriving: <b>Imports:</b> {{.ImportCount}}
:inbox_tray: <b>Imports:</b> {{.ImportCount}}
<ul>
{{- range .Imports}}
<li><code>{{ . }}</code></li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


:airplane_arriving: <b>Imports:</b> 0
:inbox_tray: <b>Imports:</b> 0
<ul>
</ul>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


:airplane_arriving: <b>Imports:</b> 0
:inbox_tray: <b>Imports:</b> 0
<ul>
</ul>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


:airplane_arriving: <b>Imports:</b> 1
:inbox_tray: <b>Imports:</b> 1
<ul>
<li><code>random_integer.import</code></li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


:airplane_arriving: <b>Imports:</b> 0
:inbox_tray: <b>Imports:</b> 0
<ul>
</ul>

Expand Down
5 changes: 4 additions & 1 deletion pkg/utils/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const (
### Previous TFC Urls:
</summary>`
</summary>
| Run ID | Status | Created at |
| ------ | ------ | ---------- |`
URL_RUN_GROUP_SUFFIX = "</details>"
URL_RUN_STATUS_PREFIX = "**Status**: "
)
Expand Down
26 changes: 23 additions & 3 deletions pkg/vcs/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,34 @@ func (c *Client) GetOldRunUrls(prID int, fullName string, rootCommentID int) (st
// If this token user made the comment, and we're making a new comment, pick the TFC url out of the body and delete the comment
if comment.GetUser().GetLogin() == currentUser.GetLogin() {
runUrl := utils.CaptureSubstring(comment.GetBody(), utils.URL_RUN_PREFIX, utils.URL_RUN_SUFFIX)
// We scrape the run URLs from the previous MR comments.
// Since they are hyperlinked in markdown format, we need to extract the URL
// without the markdown artifacts.
runUrlRaw := utils.CaptureSubstring(runUrl, "[", "]")
runUrlSplit := strings.Split(runUrlRaw, "/")
// The run ID is the last part of the run URL, and it looks like run-abcd12345...
runID := ""
if len(runUrlSplit) > 0 {
runID = runUrlSplit[len(runUrlSplit)-1]
} else {
// If the URL split slice doesn't contain anything for any reason
// We set the ID and URL to the run URL as a fallback (as it was originally scraped)
// It'll appear like this in markdown
// [https://app.terraform.io/...](https://app.terraform.io/...)
log.Warn().Msg("Unable to obtain Terraform cloud run ID. The run URL(s) on the previous comments may be malformed.")
runID = runUrl
runUrlRaw = runUrl
}
runStatus := utils.CaptureSubstring(comment.GetBody(), utils.URL_RUN_STATUS_PREFIX, utils.URL_RUN_SUFFIX)
if runUrl != "" && runStatus != "" {
// Example: <tfc url> - ✅ Applied
oldRunUrls = append(oldRunUrls, fmt.Sprintf("%s - %s", runUrl, runStatus))
// Example: |[<tfc runID>](<tfc url>)|✅ Applied|2023-08-02 15:41:48.82 +0000 UTC|
oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|%s|", runID, runUrlRaw, runStatus, comment.CreatedAt))
}

// Github orders comments from earliest -> latest via ID, so we check each comment and take the last match on an "old url" block
oldRunBlockTest := utils.CaptureSubstring(comment.GetBody(), utils.URL_RUN_GROUP_PREFIX, utils.URL_RUN_GROUP_SUFFIX)
// Add a new line for the first table entry so that markdown tabling can properly begin
oldRunBlock = "\n"
if oldRunBlockTest != "" {
oldRunBlock = oldRunBlockTest
}
Expand All @@ -109,7 +129,7 @@ func (c *Client) GetOldRunUrls(prID int, fullName string, rootCommentID int) (st
// If we found any old run urls, return them formatted
if len(oldRunUrls) > 0 {
// Try and find any exisitng groupings of old urls, else make a new one
return fmt.Sprintf("%s\n%s\n%s\n%s", utils.URL_RUN_GROUP_PREFIX, oldRunBlock, strings.Join(oldRunUrls, "\n"), utils.URL_RUN_GROUP_SUFFIX), nil
return fmt.Sprintf("%s%s%s\n%s", utils.URL_RUN_GROUP_PREFIX, oldRunBlock, strings.Join(oldRunUrls, "\n"), utils.URL_RUN_GROUP_SUFFIX), nil
}
return oldRunBlock, nil
}
Expand Down
24 changes: 22 additions & 2 deletions pkg/vcs/gitlab/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,33 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int)
for _, note := range notes {
if note.Author.Username == currentUser.Username {
runUrl := utils.CaptureSubstring(note.Body, utils.URL_RUN_PREFIX, utils.URL_RUN_SUFFIX)
// We scrape the run URLs from the previous MR comments.
// Since they are hyperlinked in markdown format, we need to extract the URL
// without the markdown artifacts.
runUrlRaw := utils.CaptureSubstring(runUrl, "[", "]")
runUrlSplit := strings.Split(runUrlRaw, "/")
// The run ID is the last part of the run URL, and it looks like run-abcd12345...
runID := ""
if len(runUrlSplit) > 0 {
runID = runUrlSplit[len(runUrlSplit)-1]
} else {
// If the URL split slice doesn't contain anything for any reason
// We set the ID and URL to the run URL as a fallback (as it was originally scraped)
// It'll appear like this in markdown
// [https://app.terraform.io/...](https://app.terraform.io/...)
log.Warn().Msg("Unable to obtain Terraform cloud run ID. The run URL(s) on the previous comments may be malformed.")
runID = runUrl
runUrlRaw = runUrl
}
runStatus := utils.CaptureSubstring(note.Body, utils.URL_RUN_STATUS_PREFIX, utils.URL_RUN_SUFFIX)
if runUrl != "" && runStatus != "" {
oldRunUrls = append(oldRunUrls, fmt.Sprintf("%s - %s", runUrl, utils.FormatStatus(runStatus)))
oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|%s|", runID, runUrlRaw, utils.FormatStatus(runStatus), note.CreatedAt))
}

// Gitlab default sort is order by created by, so take the last match on this
oldRunBlockTest := utils.CaptureSubstring(note.Body, utils.URL_RUN_GROUP_PREFIX, utils.URL_RUN_GROUP_SUFFIX)
// Add a new line for the first table entry so that markdown tabling can properly begin
oldRunBlock = "\n"
if oldRunBlockTest != "" {
oldRunBlock = oldRunBlockTest
}
Expand All @@ -151,7 +171,7 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int)

// Add new urls into block
if len(oldRunUrls) > 0 {
return fmt.Sprintf("%s\n%s\n%s\n%s", utils.URL_RUN_GROUP_PREFIX, oldRunBlock, strings.Join(oldRunUrls, "\n"), utils.URL_RUN_GROUP_SUFFIX), nil
return fmt.Sprintf("%s%s%s\n%s", utils.URL_RUN_GROUP_PREFIX, oldRunBlock, strings.Join(oldRunUrls, "\n"), utils.URL_RUN_GROUP_SUFFIX), nil
}
return oldRunBlock, nil
}
Expand Down

0 comments on commit 780416e

Please sign in to comment.