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

Added gitlab avatar support #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions person.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Person = new Lang.Class({
this.tz = params.tz;
this.avatar = params.avatar;
this.github = params.github;
this.gitlab = params.gitlab;
this.gravatar = params.gravatar;
this._githubToken = params._githubToken;

Expand Down Expand Up @@ -40,6 +41,11 @@ var Person = new Lang.Class({
return;
}

if (this.gitlab) {
this._getGitLabInfo();
return;
}

if (this.gravatar) {
this._getGravatarInfo();
return;
Expand Down Expand Up @@ -80,6 +86,40 @@ var Person = new Lang.Class({
}));
},

_getGitLabInfo: function() {
let _httpSession = new Soup.Session({user_agent: 'jwendell/gnome-shell-extension-timezone'});
let url = 'https://gitlab.com/api/v4/avatar?email=%s'.format(this.gitlab);
let message = new Soup.Message({method: 'GET', uri: new Soup.URI(url)});
if (this._gitlabToken) {
Copy link
Owner

Choose a reason for hiding this comment

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

Where does this field come from?

message.request_headers.append("Authorization", "token " + this._gitlabToken);
Copy link
Owner

Choose a reason for hiding this comment

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

According to https://docs.gitlab.com/ee/api/#oauth2-tokens should this header be Authorization: Bearer <your_access_token> instead?

}

_httpSession.queue_message(message, Lang.bind(this, function(session, message) {
if (message.status_code != Soup.KnownStatusCode.OK) {
log('Response code "%d" getting data from gitlab for user email address %s. Got: %s'.format(message.status_code, this.github, message.response_body.data));
Copy link
Owner

Choose a reason for hiding this comment

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

s/github/gitlab/

return;
}
let p;
try {
p = JSON.parse(message.response_body.data);
} catch (e) {
log('Error parsing gitlab email address response for user %s: %s'.format(this.gitlab, e));
return;
}

if (!this.avatar && p.avatar_url)
this.avatar = p.avatar_url;

if (!this.name && p.name)
Copy link
Owner

Choose a reason for hiding this comment

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

Looking at https://docs.gitlab.com/ee/api/avatar.html it seems the avatar_url is the only field in the response.
Perhaps you could use the /users endpoint and get all fields like we do with github?

this.name = p.name;

if (!this.city && p.location)
Copy link
Owner

Choose a reason for hiding this comment

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

ditto

this.city = p.location;

this.emit('changed');
}));
},

_getGravatarInfo: function() {
if (this.avatar)
return;
Expand Down
4 changes: 4 additions & 0 deletions schema-people.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"type": "string",
"description": "Gravatar ID (person's email)"
},
"gitlab": {
Copy link
Owner

Choose a reason for hiding this comment

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

perhaps we could make gitlab a struct containing host and email, with host defaulting to gitlab.com? Thus we would support different self-hosted instances of gitlab.

"type": "string",
"description": "GitLab avatar (person's email)"
},
"github": {
"type": "string",
"description": "GitHub ID"
Expand Down