Skip to content

Commit

Permalink
Merge pull request #1 from chaosbot/master
Browse files Browse the repository at this point in the history
Rebase
  • Loading branch information
eukaryote31 authored May 27, 2017
2 parents 52c19f7 + e3bd2d4 commit 8658ca1
Show file tree
Hide file tree
Showing 22 changed files with 199 additions and 97 deletions.
10 changes: 8 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
*.bak
*.pyc
.DS_Store
.venv
*.pyo
*.secret
*.swp
*.tmp
*~
.DS_Store
.vagrant/
.venv
__pycache__
github_pat.secret
19 changes: 6 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
# Opening a PR
Once you open a pull request, ChaosBot will give it X seconds (where X is
determined by github\_api.voting.get\_voting\_window)
before collecting votes. During this time, you should let people know about
your contribution, so that they may vote for it and ensure your hard work gets
merged in. If you do not wish for ChaosBot to consider your PR for merging just
yet, add "WIP" somewhere in your PR title. Remove it when you're ready for voting.

Once you open a pull request, ChaosBot will give it X seconds (where X is determined by github\_api.voting.get\_voting\_window) before collecting votes. During this time, you should let people know about your contribution, so that they may vote for it and ensure your hard work gets merged in. If you do not wish for ChaosBot to consider your PR for merging just yet, add "WIP" somewhere in your PR title. Remove it when you're ready for voting.

# Changing your PR
You may change your PR at any time without losing votes, but keep in mind, any
new changes will reset the vote window for additional time.

You may change your PR at any time without losing votes, but keep in mind, any new changes will reset the vote window for additional time.

# Merging your PR
At the end of the voting window, ChaosBot will review the votes, and if your PR
crosses a threshold, your changes will be merged in. To thank you for your merged
contribution, ChaosBot will then follow you on GitHub. If your changes are not
merged in, take the time to consider the feedback you received, and create a new
PR with changes you believe people will be willing to vote for.

At the end of the voting window, ChaosBot will review the votes, and if your PR crosses a threshold, your changes will be merged in. To thank you for your merged contribution, ChaosBot will then follow you on GitHub. If your changes are not merged in, take the time to consider the feedback you received, and create a new PR with changes you believe people will be willing to vote for.
File renamed without changes.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ChaosBot

[![Gitter chat](https://badges.gitter.im/chaosthebot/gitter.png)](https://gitter.im/chaosthebot/Lobby)
[![Gitter chat](https://badges.gitter.im/chaosthebot/gitter.png)](https://gitter.im/chaosthebot/Lobby) [![ChaosBot](https://img.shields.io/badge/Chaos-Bot-green.svg)](http://chaosthebot.com/)

> Chaos, the vacant and infinite space which existed according to the ancient
> cosmogonies previous to the creation of the world, and out of which the gods,
Expand Down Expand Up @@ -68,9 +68,9 @@ Emoji which count as votes are:

## Death Counter

Chaosbot has died 13 times. This counter is incremented whenever the trunk
Chaosbot has died 15 times. This counter is incremented whenever the trunk
breaks and the server must be restarted manually. Last broken by
[#230](https://github.com/chaosbot/chaos/pull/230)
[#239](https://github.com/chaosbot/chaos/pull/239)

## Rulers

Expand Down
13 changes: 5 additions & 8 deletions chaos.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
from os.path import dirname, abspath, join
import os
import time
import sys
import logging
import threading
import http.server
import random
import subprocess
import settings
import patch
import schedule

from os.path import dirname, abspath, join

import cron
import github_api as gh
import github_api.prs
Expand All @@ -30,8 +27,8 @@

def main():
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M')
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M')
logging.getLogger("requests").propagate = False
logging.getLogger("sh").propagate = False

Expand All @@ -50,7 +47,7 @@ def main():
cron.schedule_jobs()

log.info("Setting description to {desc}".format(desc=settings.REPO_DESCRIPTION))
repos.set_desc(api, settings.URN, settings.REPO_DESCRIPTION)
github_api.repos.set_desc(api, settings.URN, settings.REPO_DESCRIPTION)

while True:
# Run any scheduled jobs on the next second.
Expand Down
3 changes: 2 additions & 1 deletion dev/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ you don't lose it. Be sure not to commit this file and share it with the world
### Building the docker image

To build the docker image, navigate to `dev/docker/` and run `bash build.sh`.
For Windows, be sure to have docker started, then you can run `build.bat`.
This will run the Dockerfile and produce your docker image with the name
`chaos`.

## Running

Make sure you're in `dev/docker/` and run `bash run.sh`.
Make sure you're in `dev/docker/` and run `bash run.sh` or `run.bat`.

## Development Cycle

Expand Down
3 changes: 3 additions & 0 deletions dev/docker/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
copy ..\..\requirements.txt .
docker build -t chaos .
del requirements.txt
2 changes: 2 additions & 0 deletions dev/docker/run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd ..\..\
docker run -it --rm -v %cd%:/root/workspace/Chaos -p 8082:80 -p 8081:8081 chaos
2 changes: 1 addition & 1 deletion dev/vagrant/Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
# vim: ft=ruby :

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
Expand Down
6 changes: 2 additions & 4 deletions github_api/prs.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ def get_ready_prs(api, urn, window):
yield pr
elif mergeable is False:
label_pr(api, urn, pr_num, ["conflicts"])
last_update = max(arrow.get(pr["updated_at"]), updated)
update_delta = (now - last_update).total_seconds()
if update_delta >= 60 * 60 * settings.PR_STALE_HOURS:
if delta >= 60 * 60 * settings.PR_STALE_HOURS:
comments.leave_stale_comment(
api, urn, pr["number"], round(update_delta / 60 / 60))
api, urn, pr["number"], round(delta / 60 / 60))
close_pr(api, urn, pr)
# mergeable can also be None, in which case we just skip it for now

Expand Down
3 changes: 2 additions & 1 deletion github_api/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ def get_num_watchers(api, urn):


def set_desc(api, urn, desc):
""" Set description of repo """
""" Set description and homepage of repo """
path = "/repos/{urn}".format(urn=urn)
data = {
"name": settings.GITHUB_REPO,
"description": desc,
"homepage": settings.HOMEPAGE,
}
api("patch", path, json=data)
13 changes: 10 additions & 3 deletions misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@
import sh
from urllib.parse import urlparse

def removeDotGit(url):
""" Remove trailing `.git` from the git remote url """
if url.endswith('.git'):
return url[:-4]
return url

def get_self_urn():
""" determine the URN for the repo on github by looking at the remote named
"origin", and parsing it, or using a sensible default. this will allow
local tests on a developer's fork """

# remote is one of these:
# git@github.com:amoffat/chaos
# git@github.com:amoffat/chaos.git
# https://github.com/chaosbot/chaos
# https://github.com/chaosbot/chaos.git
remote = sh.git.config("--get", "remote.origin.url").strip()
remote = removeDotGit(sh.git.config("--get", "remote.origin.url").strip())

if remote:
if remote.startswith("git@"):
urn = remote.split(":")[1][:-4]
urn = remote.split(":")[1]
else:
parts = urlparse(remote)
urn = parts.path[1:][:-4]
urn = parts.path[1:]

# we're not in a git repo, or we have no remotes, so just assume a sensible
# default
Expand Down
31 changes: 10 additions & 21 deletions patch.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
inject some memoize caching into our github api functions. we're keeping the
caching layer separate from the api layer by doing it this way
Inject some memoize caching into our github api functions.
We're keeping the caching layer separate from the api layer by doing it this way.
"""

import inspect
import settings
from os.path import dirname, abspath, join
from functools import partial
import inspect

from memoize import memoize
from memoize.backends import json_backend

import github_api.voting
import github_api.repos

import settings


THIS_DIR = dirname(abspath(__file__))

api_backend = json_backend(join(THIS_DIR, settings.MEMOIZE_CACHE_DIRNAME))

# here we're creating a specialized memoize decorator that ignores the "api"
# argument in a function when constructing the memoize key. we do this because
# "api" is a resource that should not be considered as part of the memoize
# key
api_memoize = partial(memoize, blacklist={"api"}, backend=api_backend)

# a helper for monkey-patch-decorating functions in different modules


def decorate(fn, dec):
"""helper for monkey-patch-decorating functions in different modules"""
mod = inspect.getmodule(fn)
new_fn = dec(fn)
setattr(mod, fn.__name__, new_fn)

cache_dir = join(dirname(abspath(__file__)), settings.MEMOIZE_CACHE_DIRNAME)
api_memoize = partial(memoize, blacklist={"api"}, backend=json_backend(cache_dir))

# now let's memoize some very frequent api calls that don't change often
decorate(github_api.voting.get_vote_weight, api_memoize("1d"))
Expand Down
5 changes: 4 additions & 1 deletion redditchaosbot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import praw
from praw.models import MoreComments
import pprint
from praw.models import MoreComments


'''Authenticated instance of Reddit'''
Expand Down
28 changes: 20 additions & 8 deletions server/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,31 @@

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.4.1/css/bulma.min.css" />
<link rel="stylesheet" href="static/css/main.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<a href="https://github.com/chaosbot/chaos">
<img id="github-ribbon" src="https://camo.githubusercontent.com/652c5b9acfaddf3a9c326fa6bde407b87f7be0f4/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png">
</a>
<a href="https://github.com/chaosbot/chaos" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#fff; color:#151513; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
<div class="container">
<div class="content">
<div class="content-title">
<h1><strong>Hello, Chaos!</strong></h1>
<div class="columns">
<div class="column is-one-third"></div>
<div class="column">
<div class="content has-text-centered">
<div class="content-title header">
<h1 class="title"><strong>Hello, Chaos!</strong></h1>
</div>
<canvas id="canvas"></canvas>
<p class="subtitle">Hi, this is <strong>ChaosBot</strong>.</p>
<p class="content">ChaosBot is a social coding experiment to see what happens when the absolute direction of a software project is turned over to the open source community.</p>
<p class="content">For more information see <a href="https://github.com/chaosbot/chaos">Github</a>!</p>

<span class="icon">
<a href="https://github.com/chaosbot/chaos"><i class="fa fa-github"></i></a>
</span>
</div>
</div>
<p>Hi, this is <strong>ChaosBot</strong>.</p>
<p>For more information see <a href="https://github.com/chaosbot/chaos">Github</a>!</p>
<div class="column"></div>
</div>
</div>
<script src="static/js/chaos.js"></script>
</body>
</html>
37 changes: 21 additions & 16 deletions server/server.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import http.server
import socketserver
import socket

# set the process name to "chaos_server" so we can easily kill it with
# "pkill chaos_server"


def set_proc_name(newname):
"""Change the process name using libc.so.6"""
from ctypes import cdll, byref, create_string_buffer
libc = cdll.LoadLibrary('libc.so.6')
buff = create_string_buffer(len(newname) + 1)
buff.value = newname.encode("ascii")
libc.prctl(15, byref(buff), 0, 0, 0)


set_proc_name("chaos_server")

# start server on port 80
PORT = 80
Handler = http.server.SimpleHTTPRequestHandler


class NoTimeWaitTCPServer(socketserver.ThreadingTCPServer):
""" when a socket does is shutdown dance, it ends up in a TIME-WAIT state,
which can prevent rebinding on it quickly. here we say "shut up, socket",
let me rebind anyways even if you're in TIME-WAIT." that will teach it. """
"""When a socket does is shutdown dance, it ends up in a TIME-WAIT state,
which can prevent rebinding on it quickly. Here we say "shut up, socket",
let me rebind anyways even if you're in TIME-WAIT." That will teach it."""

def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)


httpd = NoTimeWaitTCPServer(("", PORT), Handler)
httpd.serve_forever()
def main():
# set the process name to "chaos_server" so we can easily kill it with:
# pkill chaos_server
set_proc_name("chaos_server")

port = 80
handler = http.server.SimpleHTTPRequestHandler
httpd = NoTimeWaitTCPServer(("", port), handler)

# serve HTTP on port 80
httpd.serve_forever()

if __name__ == "__main__":
main()
Loading

0 comments on commit 8658ca1

Please sign in to comment.