Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/api/v1/connected should be publicly accessible #279

Merged
merged 3 commits into from
May 22, 2020
Merged

Conversation

hanzei
Copy link
Contributor

@hanzei hanzei commented May 19, 2020

Summary

/api/v1/connected should be publicly accessible. Otherwise, the webapp can't figure out if a user is connected or not in the login screen.

QA Steps

  1. Deploy PR
  2. Open Mattermost in an incognito window
  3. Confirm no js error message is shown

Ticket Link

Fixes #278

@hanzei hanzei added 2: Dev Review Requires review by a core committer 3: QA Review Requires review by a QA tester labels May 19, 2020
@hanzei hanzei added this to the v1.0.0 milestone May 19, 2020
@hanzei hanzei requested review from larkox and jfrerich May 19, 2020 18:35
@@ -351,6 +351,8 @@ func (p *Plugin) getConnected(w http.ResponseWriter, r *http.Request, userID str
Organization: config.GitHubOrg,
}

userID := r.Header.Get("Mattermost-User-ID")

info, _ := p.getGitHubUserInfo(userID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Not sure if necessary, but to make more clear this, I would only call getGitHubUserInfo if the userID is different to the empty string. This function will be clearer in its intent, and we will do one less call to the KVStore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point on the call to KV Store. I tried implementing it that way, but the code didn't got cleaner. By increasing the indenting, it's harder to read what the code does.

Copy link
Contributor

@larkox larkox May 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if clearer or not, but what about deferring the marshaling and writing of resp? Something like this:

resp := ...
defer func () {
    b, _ := json.Marshal(resp)
    w.Write(b)
}()

userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
    return
}

info, _ := p.getGitHubUserInfo(userID)
if info == nil || info.Token == nil {
   return
}
...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another solution would be to declare such function inside the function and use it before the returns.

resp := ...
writeResp := func () {
    b, _ := json.Marshal(resp)
    w.Write(b)
}

userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
    writeResp()
    return
}

info, _ := p.getGitHubUserInfo(userID)
if info == nil || info.Token == nil {
   writeResp()
   return
}
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the helper methods added in #252 to make the code cleaner. I will alter the code when either PR get's merged.

@jfrerich jfrerich removed the 2: Dev Review Requires review by a core committer label May 20, 2020
@hanzei hanzei requested a review from DHaussermann May 21, 2020 08:04
@codecov-commenter
Copy link

codecov-commenter commented May 21, 2020

Codecov Report

Merging #279 into master will decrease coverage by 0.04%.
The diff coverage is 2.63%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #279      +/-   ##
==========================================
- Coverage   18.13%   18.09%   -0.05%     
==========================================
  Files          10       10              
  Lines        2371     2377       +6     
==========================================
  Hits          430      430              
- Misses       1916     1922       +6     
  Partials       25       25              
Impacted Files Coverage Δ
server/api.go 5.67% <2.63%> (-0.05%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 553bdea...96de9ad. Read the comment docs.

Copy link

@DHaussermann DHaussermann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and passed

  • When logging in as a user not connected to GutHub in incognito session, the corresponding 401 error in js console no longer occurs
  • Regression tested connecting and disconnecting a user a precaution - No change in behavior

LGTM!

@DHaussermann DHaussermann added 4: Reviews Complete All reviewers have approved the pull request and removed 3: QA Review Requires review by a QA tester labels May 21, 2020
@hanzei hanzei merged commit b1e3667 into master May 22, 2020
@hanzei hanzei deleted the 278_public-connected branch May 22, 2020 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4: Reviews Complete All reviewers have approved the pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JS error in login screen
5 participants