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

Initial Danger integration #1036

Merged
merged 4 commits into from
Dec 24, 2016
Merged

Initial Danger integration #1036

merged 4 commits into from
Dec 24, 2016

Conversation

marcelofabri
Copy link
Collaborator

Fixes #843

@jpsim We need to create a bot account and a token with it, then add it as an env var (DANGER_GITHUB_API_TOKEN)

From danger init:


Step 2: Creating a GitHub account

In order to get the most out of Danger, I'd recommend giving her the ability to post in
the code-review comment section.

IMO, it's best to do this by using the private mode of your browser. Create an account like
SwiftlintBot, and don't forget a cool robot avatar.

Here are great resources for creative commons images of robots:
 -> https://www.flickr.com/search/?text=robot&license=2%2C3%2C4%2C5%2C6%2C9
 -> https://www.google.com/search?q=robot&tbs=sur:fmc&tbm=isch&tbo=u&source=univ&sa=X&ved=0ahUKEwjgy8-f95jLAhWI7hoKHV_UD00QsAQIMQ&biw=1265&bih=1359
SwiftlintBot will need access to your repo. Simply because the code is not available for the public
to read and comment on.

Note: Holding cmd ( ⌘ ) and double clicking a link will open it in your browser.

Cool, please press return when you have your account ready (and you've verified the email...)


Step 3: Configuring a GitHub Personal Access Token

Here's the link, you should open this in the private session where you just created the new GitHub account
 -> https://github.com/settings/tokens/new

For token access rights, I need to know if this is for an Open Source or Closed Source project
? [ Open / Closed ]
> 
Using: open
For Open Source projects, I'd recommend giving the token the smallest scope possible.
This means only providing access to public_repo in the token.

This token limits Danger's abilities to just writing comments on OSS projects. I recommend
this because the token can quite easily be extracted from the environment via pull requests.

It is important that you do not store this token in your repository, as GitHub will automatically revoke it when pushed.

👍, please press return when you have your token set up...


Step 4: Add Danger for your CI

I'd recommend adding before_script: bundle exec danger to the script section of your .travis.yml file.
You shouldn't use after_success, after_failure, after_script as they cannot fail your builds.

OK, I'll give you a moment to do this...


Final step: exposing the GitHub token as an environment build variable.

As you have an Open Source repo, this token should be considered public, otherwise you cannot
run Danger on pull requests from forks, limiting its use.
In order to add an environment variable, go to:
 -> https://travis-ci.org/marcelofabri/SwiftLint/settings

The name is DANGER_GITHUB_API_TOKEN and the value is the GitHub Personal Access Token.
Make sure to have "Display value in build log" enabled.
This is the last step, I can give you a second...

@marcelofabri
Copy link
Collaborator Author

I've done this a first step towards #1025.

@jpsim
Copy link
Collaborator

jpsim commented Dec 23, 2016

I've done this a first step towards #1025.

I'm not exactly sure how this helps with #1025. You're thinking we should run OSS-check as a "Danger job" in order to piggy-back off its auto-updating GitHub comment reporting capabilities?

@marcelofabri
Copy link
Collaborator Author

Yes! My idea would be having a custom formatter as you hinted and then reading it and commenting with danger. Or we could use the JSON formatter as danger-swiftlint does and format it (with links to the files in their repos for example).

We'd need to make danger run in the end though. Or we could have two Dangerfiles - one for PR validation (in this PR), which could be run in any job; and another one that runs after the OSS-check.

@marcelofabri
Copy link
Collaborator Author

We can have something like this:

Realm Swift
🚫 Colon Rule Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals.
RealmSwift/Tests/ObjectCreationTests.swift#L463

Unfortunately, checkboxes don't work inside tables, so we'd need another way to mark a violation as legit.

Actually, even if it did danger would delete it on a new build 😬

@jpsim
Copy link
Collaborator

jpsim commented Dec 23, 2016

How hard would it be to build our own auto-updating GitHub comment reporting, which would actually allow the format we want? It doesn't seem too challenging, but I may be ignorant of the complexity involved...

@marcelofabri
Copy link
Collaborator Author

Danger does support any markdown text. Or are you thinking about something else?

@jpsim
Copy link
Collaborator

jpsim commented Dec 23, 2016

Ah, I see. I thought it was necessary to use the table format. But that still doesn't help us avoid overwriting "approved" violations.

@marcelofabri
Copy link
Collaborator Author

Yeah, we could get away with that by using GitHub API and applying our own logic in Dangerfile though.

@codecov-io
Copy link

codecov-io commented Dec 23, 2016

Current coverage is 83.15% (diff: 100%)

Merging #1036 into master will not change coverage

@@             master      #1036   diff @@
==========================================
  Files           145        145          
  Lines          7098       7098          
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
  Hits           5902       5902          
  Misses         1196       1196          
  Partials          0          0          

Powered by Codecov. Last update 82d65c3...7c07b44

@jpsim
Copy link
Collaborator

jpsim commented Dec 24, 2016

I've created @SwiftLintBot, a new token for that account and added it in Travis's DANGER_GITHUB_API_TOKEN env vars. Then I reran the SPM build that had previously failed, which has now succeeded, but I was expecting Danger to post a comment, which it did not. Although maybe that's normal because none of the code paths causing fail or warn in the Dangerfile were hit, so Danger doesn't comment in those cases?

@marcelofabri
Copy link
Collaborator Author

Yes, that's the expected behavior. You can run it locally against a PR that would fail to check:

danger pr https://github.com/realm/SwiftLint/pull/1032

markdown <<-MARKDOWN
Here's an example of your CHANGELOG entry:
```markdown
* #{github.pr_title}#{' '}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not this?

-* #{github.pr_title}#{' '}
-[#{github.pr_author}](https://github.com/#{github.pr_author})
-[#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)
+* #{github.pr_title}  
+  [#{github.pr_author}](https://github.com/#{github.pr_author})
+  [#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

because I shamelessly copied this from CocoaPods' Dangerfile 😂

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've kept the #{' '} to make sure we don't miss this if someone uses an editor that strips trailing whitespaces.

Copy link
Collaborator

Choose a reason for hiding this comment

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

that's just one space not two, though?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, why the two spaces at the end of the github.pr_title line?

Copy link
Collaborator

Choose a reason for hiding this comment

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

To force a newline.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It was taken from CocoaPods.

@@ -13,6 +13,7 @@ matrix:
env: JOB=SPM
os: osx
osx_image: xcode8.2
before_script: bundle install --jobs=3 --retry=3 --without documentation --path bundle && bundle exec danger
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe break this up into a YAML array for legibility?

before_script:
  - bundle install --jobs=3 --retry=3 --without documentation --path bundle
  - bundle exec danger

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍

@jpsim
Copy link
Collaborator

jpsim commented Dec 24, 2016

We should probably add .bundle/ and bundle/ to .gitignore?

@jpsim jpsim merged commit 9dc0868 into realm:master Dec 24, 2016
@marcelofabri marcelofabri deleted the initial-danger branch December 25, 2016 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants