Use Python to make GitHub message assistant (dry goods sharing)


There are more and more subscriptions to github, and in the end, there are no bookmarks and no subscriptions. In order to solve these problems, we need more intelligent and flexible external brain tools that can free us from the ocean of information and allow us to focus more on our own business.


In the Internet 2.0 era, engineers mainly rely on the various tools and software they have to solve business problems. With the open source wave sweeping the world, open source tools and software have also grown rapidly. Engineers need to pay attention to more and more technologies and software, the burden of learning is increasing, and their brains are becoming less and less sufficient. But engineers are also helpless, because whoever has more technology and software can solve problems more efficiently. So engineers began to use external brain tools on the Internet: especially search engines, bookmarks, github, scihub, etc. The ability of engineers to solve problems is reflected in the use of external brain tools.

However, as the problems that engineers have to solve and the accumulation of their own knowledge, the external brain tools have gradually become bloated: more and more bookmarks, more and more subscriptions to github, and in the end, it is about no bookmarks and no Subscribed. In order to solve these problems, we need more intelligent and flexible external brain tools that can free us from the ocean of information and allow us to focus more on our own business.

GitHub news issue

Have you found that your Github message Inbox will pile up if it is not processed in a few days? I believe that the numbers in some students' Inbox are even more exaggerated than this, and some students even gave up the function of Inbox in despair.

Why is this happening?

Because most of every Coder will like to collect favorite works in their hearts, and the most eye-catching position in the upper right corner of the github project homepage is always these three buttons:

I believe that when engineers see their favorite projects, they will not hesitate to click three links: watch, start, fork.

The tragedy began here.

1. Engineers like more and more projects;

2. The project will have its own life cycle, some become active, some gradually die out;

3. The engineer is getting busy and has no time to take care of Inbox.

Then, Inbox changed like this:

Looking at the pile of news, is there a feeling of collapse? So what's wrong with the function of github?

I think it's because the level of attention required by engineers for watch, star, and fork is wrong. Of course, github is also actively improving. Compared with before, we can find that there are more watch options:

But is this enough? Looking at the tens of thousands of messages from Inbox at every turn, do you want to change the items you care about to Ignore one by one?

The engineer's heart is still broken!

Is there a way to save the engineer's Inbox?

Have! Come on, do it yourself to save my inbox.

solution

Use python as a GitHub message assistant, which will automatically help engineers close and delete unnecessary messages. Isn't this the true Watch? When you look at it, you will receive its information, and you will disappear without looking at it. So think about it carefully, what kind of news is really useful to engineers?

1. Can I not pay attention to a project that has not been updated for a long time?

2. Projects that are no longer the scope of work and points of interest, can we not pay attention to it?

3. Projects that have not been reported for a long time, can we not pay attention to them?

And python has an advantage that it can easily automate user operations. Well, it seems that these zombie projects can be eliminated by python automation, just do it, let’s get started!

Code

We know that Python has a great web automation testing framework: Selenium, but Selenium is mainly used for testing, and the call is still slightly complicated. So I searched on github and finally found a suitable Python package: PyChrome project address:

https://github.com/siversalih/pyChrome-Web-Automation

Next, we will use this non-mainstream automation toolkit to complete our little assistant to see the homepage. The author is lazy and didn't update it a few years ago, but fortunately, the help is quite complete:

https://pychrome.wordpress.com/usage/

So we can assemble the robot according to the instructions happy.

0. Environmental preparation

First, you need to prepare the Python 3.8 environment, and then install Selenium according to the online instructions, and then clone the PyChrome project to the local. ok, environment preparation is complete.

1. Simulate login to github

Using PyChrome to access github has a small trouble, every time a brand new instance of the Chrome browser will be launched. This makes it impossible to reuse the cookie information stored locally, so you have to simulate login each time. Github has a feature, if the ip changes, you need to enter the verification code, if the ip does not change, you don’t need it, so we can only enter it manually for the first time.

However, the login page of github is relatively simple, you only need to find the form components corresponding to Username and password. So the login code can be very concise, as shown below:

browser.open("https://github.com/login")  
# name="login"  
name_locator = "//*[@name='login']"  
el_name = browser.findElementByXPath(name_locator)   
browser.sendTextToElement(username, el_name)   
# name="password"  
pass_locator = "//*[@name='password']"  
el_pass = browser.findElementByXPath(pass_locator)   
browser.sendTextToElement(password,el_pass)   
login_locator = "//*[@name='commit']"  
el_login = browser.findElementByXPath(login_locator)   
browser.clickElement(el_login) 

2. Simulate into Inbox

After logging in, we need to enter the inbox to see what unread messages are. The inbox is a bit small and complicated, but it can also be easily distinguished.

Find the correct xpath, I believe positioning is not difficult. Here I took another clever. We are actually troubled by projects with news. If a project does not send news, we will not be harassed. So it seems more efficient to directly select the Repositories area in the lower left corner.

code show as below:

browser.open("https://github.com/notifications")  
# 获取有消息的Repositories列表  
locator = "js-notification-sidebar-repositories"  
el_repos = browser.findElementByClass(locator)   
repos_list = browser.findElementsByTag("li", el_repos) 

3. Check zombie items

I choose the third strategy. Projects that no one has reported problems for a long time are used as the criteria for judging zombie projects (purely for convenience). First visit the issue, and then determine the update date in the issue, which happens to have a detailed date field. The purpose of the code below is very simple, that is, how long has the last issue been updated.

browser.newTab("https://github.com/" + repos_name + "/pulls?q=")  
  # 判断最近的 pull request  
  locator = "//div[@aria-label='Issues']"  
  el_pulls = browser.findElementByXPath(locator)   
  pull_list = browser.findElementsByTag("relative-time", el_pulls)  
  timedelta = 0  
  if type(pull_list)==list and len(pull_list)>0:  
      # 2020-11-10T00:55:39Z  
      # last_pull_time_str = pull_list[0].getAttribute("datetime")  
      last_pull_time_str = pull_list[0].get_attribute("datetime")  
      last_time = datetime.strptime(last_pull_time_str, "%Y-%m-%dT%H:%M:%SZ")  
      timedelta = (datetime.now() - last_time).days   
  logger.debug(repos_name + " timedelta: " + str(timedelta) + " days") 

4. Unfollow zombie items

If the issue has been more than 1 year old, you should naturally unfollow it. After all, the current information update speed is too fast.

# 取消不活跃项目的订阅(1年以上没有pull request)  
if unsubscribe and timedelta > 366:  
    el_notify_button =browser.findElementsByTag("notifications-list-subscription-form")  
    browser.clickElement(el_notify_button)   
    time.sleep(1)  
    # data-target="notifications-list-subscription-form.menu"  
    locator = "//*[@data-target='notifications-list-subscription-form.menu']"  
    el_notify_menus = browser.findElementByXPath(locator)   
    # value="ignore"  
    sub_locator = "//*[@value='ignore']"  
    el_ignore_button =browser.findElementByXPath(sub_locator, el_notify_menus)   
    browser.clickElement(el_ignore_button)   
    logger.debug(repos_name + " cancel subscribed") 

5. Delete the zombie project message

Finally, it is time to relieve the troubles. For such projects that are no longer updated, engineers will naturally not be harassed by its news.

el_repos_link = browser.findElementByTag("a", repos)  
 browser.clickElement(el_repos_link)   
 # mr-1 js-notifications-mark-all-prompt  
 time.sleep(1)  
 el_sel_all =browser.findElementByClass("js-notifications-mark-all-prompt")  
 browser.clickElement(el_sel_all)   
 time.sleep(1)  
 # title="Done"  
 done_locator = "//*[@title='Done']"  
 el_done = browser.findElementByXPath(done_locator)   
 browser.clickElement(el_done)   
 logger.debug(repos_name + " remove notifiy") 

The above code is to simulate the operation of the Done button:

At this point, all the logic of the GitHub message assistant has been completed, and the entire Inbox is finally quiet. Is it possible to have a cup of coffee and relax?

postscript

Python automation tools really bring convenience to engineers, enabling engineers to deal with various daily and different challenges. In order to facilitate the early release and fulfillment of all the engineers, the above code has been open source, the complete code address:

https://gitee.com/knifecms/puppetry/blob/master/github-agent/resp_notify.py

In addition, there are several other interesting automation assistants and tools under this project. You can also study them if you are interested.

Hope to get more good ideas from you!


Finally: a wave of software testing data sharing!

In the technology industry, you must improve your technical skills and enrich your practical experience in automation projects. This will be very helpful for your career planning in the next few years and the depth of your test technology mastery.

In the interview season of the Golden 9th and the Silver 10th, the season of job-hopping, organizing interview questions has become my habit for many years! The following is my collection and sorting in recent years, the whole is organized around [software testing], the main content includes: python automation test exclusive video, Python automation details, a full set of interview questions and other knowledge content.

May you and I meet and you will find something! If you want to exchange experience in software testing, interface testing, automated testing, and interviews. Follow WeChat public account:[Sad Spicy Strips]Receive a 216-page software test engineer interview book for free. And the corresponding video learning tutorials are free to share! Communication learning skirt:313782132

Recommend good articles:

Packaged as a test engineer with 1 year of work experience, my advice before the interview is as follows

What exactly should I learn in automated testing?

Why not consider Tencent for job-hopping? Talk about a little bit of the past between me and the goose factory

Which is more advanced, automated testing or manual testing?

Novice must see: How to write a qualified test case?

Python login interface test problem record and solution (dry goods)

Guess you like

Origin blog.csdn.net/weixin_50829653/article/details/113987338