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

Reimplement translation prs #143

Merged
merged 22 commits into from Jul 4, 2020
Merged

Reimplement translation prs #143

merged 22 commits into from Jul 4, 2020

Conversation

metanerd
Copy link
Contributor

Summary

Before the migration from pootle to weblate, we got translation PRs for review into the i18n channel. This PR implements the same logic for weblate.

Ticket Link

@hanzei hanzei added the 2: Dev Review Requires review by a core committer label Jun 25, 2020
Copy link
Contributor

@khos2ow khos2ow left a comment

Choose a reason for hiding this comment

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

LGTM, very minimal comments though.

@@ -14,7 +14,7 @@ import (
)

func (s *Server) triggerEETestsForOrgMembers(pr *model.PullRequest) {
if s.IsOrgMember(pr.Username) {
if s.IsOrgMember(pr.Username) || s.IsKnownUser(pr.Username) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this needed? I mean do we want to run enterprise tests for external known users?

Copy link
Contributor Author

@metanerd metanerd Jun 26, 2020

Choose a reason for hiding this comment

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

I renamed it to IsTrustedUser(). I am for adding this, because the translation bot only modifies translation files and otherwise I have to add the EETests label and then check in again in an hour. But if you have objections, I am fine with not whitelisting the CLAExcluded bots. mattermost/mattermost#14872

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I'm fine and there's no harm running EE tests anyway.


dataMsg := fmt.Sprintf("####[%v translations PR %v](%v)\n", pr.RepoName, time.Now().UTC().Format(time.RFC3339), pr.URL)
msg := dataMsg + s.Config.TranslationsMattermostMessage
mlog.Debug("Sending Mattermost message", mlog.String("message", msg))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest changing the message to something like Translation instead of Mattermost for clarity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why does it make it clearer?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because the payload is being sent to translation service and not Mattermost. Isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is sent to mattermost. It contains the links to the PRs for translations: https://community.mattermost.com/core/pl/sqzth7ryibgrbqj196trrs4hec


webhookRequest := &Payload{Username: "Weblate", Text: msg}
if err := s.sendToWebhook(s.Config.TranslationsMattermostWebhookURL, webhookRequest); err != nil {
mlog.Error("Unable to post to Mattermost webhook", mlog.Err(err))
Copy link
Contributor

Choose a reason for hiding this comment

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

The same, so if any error happens and one looks at the logs won't get confused.

func (s *Server) sendToWebhook(payload *Payload) error {
func (s *Server) sendToWebhook(webhookURL string, payload *Payload) error {
if webhookURL == "" {
err := errors.New("no Mattermost webhook URL set: unable to send message")
Copy link
Contributor

Choose a reason for hiding this comment

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

The same, may be drop Mattermost?

Copy link
Member

@agnivade agnivade left a comment

Choose a reason for hiding this comment

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

Pushing it out of my queue until some tests are added as discussed offline. Please re-request review when the tests are added. Thanks.

Copy link
Member

@agnivade agnivade left a comment

Choose a reason for hiding this comment

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

I also want to understand how does this change know that this is a translation PR? From what I understand, it is unconditionally posting the message to the webhook URL for every PR.

Perhaps I have missed something that I am not aware of?

server/enterprise.go Outdated Show resolved Hide resolved
server/translations.go Outdated Show resolved Hide resolved
server/translations.go Outdated Show resolved Hide resolved
server/config.go Show resolved Hide resolved
server/github.go Outdated Show resolved Hide resolved
server/webhook_integration_test.go Outdated Show resolved Hide resolved
server/webhook_integration_test.go Outdated Show resolved Hide resolved
server/webhook_integration_test.go Outdated Show resolved Hide resolved
server/webhook_integration_test.go Outdated Show resolved Hide resolved
server/webhook_integration_test.go Outdated Show resolved Hide resolved
@metanerd metanerd requested a review from agnivade July 2, 2020 12:00
Copy link
Member

@agnivade agnivade left a comment

Choose a reason for hiding this comment

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

Getting there. But needs some more work. Please take a look at my comments. Thanks!

server/cla.go Show resolved Hide resolved
server/utils.go Outdated
}
defer r.Body.Close()
Copy link
Member

Choose a reason for hiding this comment

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

I am not seeing this resolved. Please see my comment #143. We can just use closeBody here.

Comment on lines 23 to 26
badRequestR := &http.Response{
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(bytes.NewBufferString(err.Error())),
}
Copy link
Member

Choose a reason for hiding this comment

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

This does not appear to be resolved?

Comment on lines 15 to 22
type WebhookValidationError struct {
err string
}

func (e *WebhookValidationError) Error() string {
return fmt.Sprintf("%v", e.err)
}

Copy link
Member

Choose a reason for hiding this comment

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

This implements only partially what I suggested. This just wraps the error with a type, and does not do anything else. As it is, it's not very useful. I was suggesting that a field added to the struct which will indicate what is not set- url/username/text.

Please also add godocs to both the struct and the method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you please send me an example for what you mean by this?

Copy link
Member

Choose a reason for hiding this comment

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

Sure.

// WebhookValidationError contains an error in the webhook payload.
type WebhookValidationError struct {
 field string
}

// Error implements the error interface.
func (e *WebhookValidationError) Error() string {
	return "invalid" + e.field
}

func newWebhookValidationError(field string) *WebhookValidationError {
  return &WebhookValidationError{
   field: field,
  }
}

// from code
if webhookURL == "" {
	return newWebhookValidationError("webhook URL")
}

// in test
var whError *WebhookValidationError
require.True(errors.As(err, &whError))
assert.Equal(whError.field, "webhook URL")

Let me know if that helps. (Code is untested, I just wrote here directly)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This helps immensely! Thank you so much!

server/cla.go Outdated
if err != nil {
return nil, err
}
r, err := http.DefaultClient.Do(req)
if err != nil {
mlog.Error("Unable to get CLA list", mlog.Err(err))
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to this PR, but this is an anti-pattern. Logging and then returning the same error. We need to do a sweep to fix all of these.

Comment on lines 18 to 20
if testing.Short() {
t.Skip("skipping integration test")
}
Copy link
Member

Choose a reason for hiding this comment

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

Any reason for this?

Same for below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want to differentiate between running unit tests and implementation tests. I looked up on how to accomplish this. This was, what came up as a go to solution.

Copy link
Member

Choose a reason for hiding this comment

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

But this is an unit test from what I understand. There is no external dependency with any other service. It's purely inside the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, removed.

}

s := &Server{
StartTime: time.Now(),
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this in the test?

Same for below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are calling s.sendToWebhook(context.Background(), mattermost.URL, validPayload). Is there another way?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I meant the StartTime field. Is that needed?

server/webhook_test.go Outdated Show resolved Hide resolved
server/webhook_test.go Outdated Show resolved Hide resolved
server/webhook_test.go Outdated Show resolved Hide resolved
@metanerd metanerd requested a review from agnivade July 3, 2020 07:28
Copy link
Member

@agnivade agnivade left a comment

Choose a reason for hiding this comment

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

Apart from the payload validation, just some tiny things remaining.

server/webhook.go Outdated Show resolved Hide resolved
server/webhook.go Outdated Show resolved Hide resolved
server/cla.go Outdated Show resolved Hide resolved
metanerd and others added 2 commits July 3, 2020 12:40
Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in>
Copy link
Member

@agnivade agnivade left a comment

Choose a reason for hiding this comment

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

Thanks for your patience with this @metanerd. Looks great now.

@metanerd
Copy link
Contributor Author

metanerd commented Jul 3, 2020

Thanks to you, sir!

@metanerd metanerd merged commit 888c042 into master Jul 4, 2020
@metanerd metanerd deleted the reimplement-translation-prs branch July 4, 2020 09:16
@agnivade agnivade added 3: Reviews Complete All reviewers have approved the pull request and removed 2: Dev Review Requires review by a core committer labels Jul 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3: Reviews Complete All reviewers have approved the pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants